bitfluent

Kamal Fariz Mahyuddin on Ruby, Rails, Apple and being a Dad.

Reach me by Twitter or email.
Mar
11th
Tue
permalink

Interfacing a Rails App to Fire Eagle Pt 1: Exploration via IRB

Here are my notes as I explore interfacing a Rails app to Fire Eagle. Tokens have been masked to protect the innocent :)

  1. Create a new application.
  2. Get the fireeagle gem.

    MacBook-Pro:src kamal$ git clone git://github.com/kamal/fireeagle.git fireeagle
    Initialized empty Git repository in /Users/kamal/src/fireeagle/.git/
    remote: Generating pack...
    remote: Done counting 377 objects.
    remote: Deltifying 377 objects...
    remote:  100% (377/377) done
    remote: Total 377 (delta 192), reused 0 (delta 0)
    Receiving objects: 100% (377/377), 75.91 KiB | 9 KiB/s, done.
    Resolving deltas: 100% (192/192), done.
    MacBook-Pro:src kamal$ cd fireeagle
    MacBook-Pro:fireeagle(master) kamal$ rake install_gem
    (in /Users/kamal/src/fireeagle)
    sudo gem install --local pkg/*.gem
    Successfully installed fireeagle-0.6.1
    1 gem installed
    Installing ri documentation for fireeagle-0.6.1...
    Installing RDoc documentation for fireeagle-0.6.1...
    
  3. Fire up irb and instantiate a client with the Consumer Key and Secret provided by Step 1.

    MacBook-Pro:~ kamal$ irb
    >> require 'fireeagle'
    => true
    >> client = FireEagle::Client.new(
      :consumer_key    => 'AAAAAAAAAAAA',
      :consumer_secret => 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')
    => #<FireEagle::Client:0x1bef504 ...>
    
  4. Generate a Request Token and associate it with the user because we’ll need to locate who authenticated our app.

    >> client.get_request_token
    => #<OAuth::Token:0x1bddb24 @secret="emfPurZAbYlRqNL7fSxhXOkxCJRZ2T1r", @token="mGxmGcGPNyjr">
    >> current_user.update_attributes!(
      :request_token        => client.request_token.token,
      :request_token_secret => client.request_token.secret)
    
  5. Redirect the user to the Authorization URL.

    >> client.authorization_url
    => "https://fireeagle.yahoo.net/oauth/authorize?oauth_token=mGxmGcGPNyjr"
    
  6. After the user grants us permission, Fire Eagle will redirect the user’s browser to our Callback URL (defined in Step 1) with the same oauth_token params.

    http://yourapp.example.com/your_call_back_path?oauth_token=mGxmGcGPNyjr
    
  7. Find the User by the oauth_token and instantiate a new client (client2 in our IRB session) with the request token and secret.

    >> user = User.find_by_request_token(params[:oauth_token])
    >> client2 = FireEagle::Client.new(
      :consumer_key         => 'AAAAAAAAAAAA',
      :consumer_secret      => 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
      :request_token        => user.request_token,
      :request_token_secret => user.request_token_secret)
    
  8. Convert the Request Token to a long-lived Access Token and save it. Optionally, you can nil out the Request Token because you are not going to need them anymore.

    >> client2.convert_to_access_token
    => #<OAuth::Token:0x23c4824 @token="CCCCCCCCCCCC", @secret="DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD">
    >> user.update_attributes!(
      :access_token         => client2.access_token.token,
      :access_token_secret  => client2.access_token.secret
      :request_token        => nil
      :request_token_secret => nil)
    
  9. Go to town! From here on out, you can directly instantiate a client using the user’s Access Token to read or update their location.

    >> client3 = FireEagle::Client.new(
      :consumer_key        => 'AAAAAAAAAAAA',
      :consumer_secret     => 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
      :access_token        => user.access_token,
      :access_token_secret => user.access_token_secret)
    >> client3.update(:q => 'petaling jaya')
    => #<FireEagle::Response:0x2549a8c ...>
    >> client.user.best_guess.name
    => "Petaling Jaya, Malaysia"
    

A lot of the stuff above can be extracted into a Rails plugin. First dibs on acts_as_hatchling!