whereis/0000755011752000001440000000000010764271460013335 5ustar nikhilbusers00000000000000whereis/._FireEagle.rb0000644011752000000000000000026210764271144015653 0ustar nikhilbwheel00000000000000Mac OS X  2€²ATTR>ÂJ²˜˜com.macromates.caret{column = 66; line = 14; }whereis/FireEagle.rb0000644011752000001440000000453510764271144015513 0ustar nikhilbusers00000000000000#!/usr/bin/env ruby #an early version of the fireeagle ruby gem #by Seth Fitzsimmons require 'rubygems' require 'JSON' gem 'oauth', ">= 0.2.1" require 'oauth/client/helper' require 'oauth/request_proxy/net_http' module FireEagle SERVER = "https://fireeagle.yahooapis.com" REQUEST_TOKEN_URL = "#{SERVER}/oauth/request_token" ACCESS_TOKEN_URL = "#{SERVER}/oauth/access_token" AUTHORIZATION_URL = "http://fireeagle.yahoo.net/oauth/authorize" USER_API_URL = "#{SERVER}/api/0.1/user.json" LOOKUP_API_URL = "#{SERVER}/api/0.1/lookup.json" UPDATE_API_URL = "#{SERVER}/api/0.1/update.json" class Client attr_reader :access_token, :consumer def initialize(options) @consumer = OAuth::Consumer.new(options[:consumer_key], options[:consumer_secret]) if options[:access_token] && options[:access_token_secret] @access_token = OAuth::Token.new(options[:access_token], options[:access_token_secret]) end request_uri = URI.parse(FireEagle::REQUEST_TOKEN_URL) @http = Net::HTTP.new(request_uri.host, request_uri.port) end def user get_access_token unless @access_token response = get(FireEagle::USER_API_URL) #puts response.body JSON.parse(response.body) end #use before auth def get_access_token_part1 response = get(FireEagle::REQUEST_TOKEN_URL, :token => nil) create_token(response) end #use after part 1 is complete and callback is called def get_access_token_part2(request_token) response = get(FireEagle::ACCESS_TOKEN_URL, :token => request_token) create_token(response) end protected def create_token(response) token = Hash[*response.body.split("&").map { |x| x.split("=") }.flatten] OAuth::Token.new(token["oauth_token"], token["oauth_token_secret"]) end def get(url, options = {}) options = { :params => {}, :token => access_token }.merge(options) request_uri = URI.parse(url) http = Net::HTTP.new(request_uri.host, request_uri.port) http.use_ssl = true qs = options[:params].collect { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") request = Net::HTTP::Get.new(request_uri.path + "?" + qs) request.oauth!(http, consumer, options[:token]) http.request(request) end end #module end #class whereis/._whereis.rb0000644011752000000000000000044710764271117015503 0ustar nikhilbwheel00000000000000Mac OS X  2õ'ATTR>Â4'¼k¼cm.moorcaamtes.caret×Pcom.macromates.foldings{column = 32; line = 141; }xœÓàR‚ê´¢ü\[c3k…̼”Ô¼ ÇÀZ¡$H›[+Ôê( (45@RhSh„©ÐÐÄ‹JCS ,JÍ ±Ønd` TÊ¥ 6œ*”whereis/whereis.rb0000644011752000001440000001162510764271117015334 0ustar nikhilbusers00000000000000#!ruby #whereis - a simple fireeagle demo #Nikhil Bobb (nikhilb at yahoo-inc.com) #26th Feb 08 require 'camping/session' require 'FireEagle' require 'JSON' Camping.goes :Whereis module Whereis include Camping::Session table_style = "border-width:1px; border-style:outset" def getFEClient(access_token = "", access_token_secret = "") client = FireEagle::Client.new \ :consumer_key => "", :consumer_secret => "", :access_token => access_token, :access_token_secret => access_token_secret end #gets a user's location from fireEagle and returns a nice #string def getFELocation(access_token, access_token_secret) client = getFEClient(access_token, access_token_secret) location_obj = client.user #query the JSON object to get the location #where best guess = true hierarchy = location_obj["user"]["location_hierarchy"] current_loc = hierarchy.select {|l| l["best_guess"]}.first [current_loc["name"], current_loc["located_at"]] end end module Whereis::Models class User < Base; has_one :request_token serialize :access_token end class RequestToken < Base; belongs_to :user serialize :bin end class Login < Base; end class CreateTheBasics < V 1.0 def self.up create_table :whereis_logins do |t| t.column :username, :string t.column :password, :string end create_table :whereis_users do |t| t.column :name, :string t.column :access_token, :text end create_table :whereis_request_tokens do |t| t.column :str, :string t.column :bin, :text t.column :user_id, :integer end Login.create :username => 'admin', :password => "eagle123" end def self.down drop_table :whereis_users drop_table :whereis_request_tokens drop_table :whereis_logins end end end module Whereis::Controllers class Style < R '/styles.css' def get @headers["Content-Type"] = "text/css; charset=utf-8" @body = %{ table { border-width:1px; border-style: dotted; } td { border-width: 1px 1px 1px 1px; border-style: solid; } } end end class Auth < R '/auth' def post @logged_in_user = Login.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password] if @logged_in_user @login = 'login success !' @state.user_id = @logged_in_user.id redirect R(Index) else @login = 'wrong user name or password' render :id end end def get render :id end end class Index < R '/' def get if @state.user_id.blank? redirect R(Auth) return end @users = [] User.find(:all).each do |u| t = u.access_token if t location = getFELocation(t.token, t.secret) @users << [u.name, location[0], location[1]] end end render :whereis end def post if @state.user_id.blank? redirect R(Auth) return end client = getFEClient request_token = client.get_access_token_part1 rt = RequestToken.create :bin => request_token, :str => request_token.token u = User.create :name => input.user, :request_token => rt redirect "#{FireEagle::AUTHORIZATION_URL}?oauth_token=#{request_token.token}" end end class Callback < R '/callback' def get if @state.user_id.blank? redirect R(Auth) return end client = getFEClient rt = RequestToken.find_by_str(input.oauth_token) at = client.get_access_token_part2(rt.bin) user = rt.user user.access_token = at user.save rt.destroy redirect "/" end end end module Whereis::Views def layout html do head do title { "Where is everyone?" } link :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css', :media => 'screen' end end body { self << yield } end def id p @login form :action => R(Auth), :method => 'post' do label 'Username', :for => 'username'; br input :name => 'username', :type => 'text'; br label 'Password', :for => 'password'; br input :name => 'password', :type => 'text'; br input :type => 'submit', :name => 'login', :value => 'Login' end end def whereis form :method => "post" do input :type => "text", :name => "user" input :type => "submit", :value => "add me" end table do tr do td "name" td "location" td "time" end tr do @users.each do |u| u.each{|c| td c} end end end end end def Whereis.create Camping::Models::Session.create_schema Whereis::Models.create_schema :assume => (Whereis::Models::User.table_exists? ? 1.0 : 0.0) end