Wednesday, July 1, 2009

Domain & Server Setup


Documentation

I plan to document the process of creating this website and all of its features as a tutorial.

Buying the domain

  • google'd "google domain registration"
  • visited http://www.google.com/a/cpanel/domain/new
  • bought thewardmenu.com (planning to buy the .org as well)

Readying the Test Box

  • grabbed an older running commodity computer
  • installed ubuntu
  • apt-get install --yes apache2 apache2-mpm-worker libapache2-mod-passenger ruby-full rubygems rails vim-rails
  • OR to run rails alongside php: http://wiki.brightbox.co.uk/docs:phusion-passenger

Hello World on RAILS

Here's some really quick code just to see that rails is installed and up and running
  • mkdir -p ~/Code/
  • cd ~/Code
  • rails TheWardMenu
  • ruby scripts/generate controller hello
  • cat - > app/views/hello/index.html.erb <
    Hello World!
    EOF
  • ruby script/server
  • http://localhost:3000
  • http://localhost:3000/hello

Creating the Data Structure

Our local database will store user and ward names. It will not store passwords or any other personal information for the logged-in user.
However, it will cache the ward database for a few days. For this reason we will also log a timestamp for the user and the directory cache.
  • script/generate model Stake name:string
  • script/generate model Ward name:string stake:references
  • script/generate model User ldsaccount:string last_login:timestamp ward:references
  • script/generate model Member first:string last:string photo:binary 
  • edit Member Migration blurb:text ward:references

Logging Into LDS.org

LoginController:
  private
  def test_lds_account(username, password)
    require 'mechanize'
    #puts "Logging in..."
    # Start up our "web browser"
    agent = WWW::Mechanize.new
    agent.user_agent_alias = 'Mac Safari'


    # Start at the main LDS.org page
    begin
      page = agent.get('http://lds.org/')
    rescue Exception
      return false
      #abort "Can't connect to LDS.org. Try again later."
    end


    page = page.links.find {|l| l.text =~ /ward.*site/i}.click          # Go to the login page
    form = page.forms.find {|f| f.name =~ /log/i}                       # Find the login form
    form.fields.find {|f| f.name =~ /user/i}.value = username           # Enter the username
    form.fields.find {|f| f.name =~ /pass/i}.value = password           # Enter the password
    page = form.submit                                                  # Try to login


    # Check for a failed login
    if /login/i.match(page.title)
      #abort "Unable to login! Incorrect username and/or password?"
      return false
    end


    #puts "Navigating to the membership directory..."
    page = page.links.find {|l| l.text =~ /member.*directory/i}.click   # Go to the membership directory page
    link = page.links.find {|l| l.text =~ /photo/i}                     # Find the link to the version of the directory with the photos




    # The actual URL for the photo directory is embedded in javascript
    # in the HREF field. So we have to dig it out ourselves.
    # TODO Can we pull the base of the URI from the agent (or page)
    # instead of hard coding it?
    photo_directory_url = 'https://secure.lds.org' + /(\/.*\.html)/i.match(link.uri.to_s)[1]
    page = agent.get(photo_directory_url)




    #puts "Downloading the pictures (this can take several minutes)..."
    # Yes, this page actually has _two_ HTML title elements!
    ward_name = page.search('/html/head/title[1]').inner_text.strip
    ward_name.slice!(' Membership Directory')


    return ward_name
  end

Overriding the default authentication of Authlogic_Example
User.find_by_login(params[:user_session][:login])
ldsorg = Ldsorg(params[:user_session][:login], params[:user_session][:password])
if ldsorg.ldslogin
  UserSession.new(@user, true)
end

Updating the progress of the directory to the user requires using a generator.

Moving from the WEBrick test environment to a production apache server
http://www.modrails.com/documentation/Users%20guide.html#_deploying_a_ruby_on_rails_application

http://guides.rubyonrails.org/getting_started.html
cat config/database.yml
rake db:create

TODO.... sort out how to run passenger and php on the same system due to prefork / worker conflict

No comments:

Post a Comment