lib/fireeagle/client.rb in fireeagle-0.6.1 vs lib/fireeagle/client.rb in fireeagle-0.6.2
- old
+ new
@@ -74,37 +74,36 @@
def initialize(options = {})
options = {
:debug => false,
:format => FireEagle::FORMAT_XML
}.merge(options)
-
+
# symbolize keys
options.map do |k,v|
options[k.to_sym] = v
end
raise FireEagle::ArgumentError, "OAuth Consumer Key and Secret required" if options[:consumer_key].nil? || options[:consumer_secret].nil?
- @consumer = OAuth::Consumer.new(options[:consumer_key], options[:consumer_secret])
- @debug = options[:debug]
- @format = options[:format]
- @app_id = options[:app_id]
+ @consumer = OAuth::Consumer.new(options[:consumer_key], options[:consumer_secret], :site => FireEagle::API_SERVER, :authorize_url => FireEagle::AUTHORIZATION_URL)
+ @debug = options[:debug]
+ @format = options[:format]
+ @app_id = options[:app_id]
if options[:access_token] && options[:access_token_secret]
- @access_token = OAuth::Token.new(options[:access_token], options[:access_token_secret])
+ @access_token = OAuth::AccessToken.new(@consumer, options[:access_token], options[:access_token_secret])
else
@access_token = nil
end
if options[:request_token] && options[:request_token_secret]
- @request_token = OAuth::Token.new(options[:request_token], options[:request_token_secret])
+ @request_token = OAuth::RequestToken.new(@consumer, options[:request_token], options[:request_token_secret])
else
@request_token = nil
end
end
# Obtain an <strong>new</strong> unauthorized OAuth Request token
def get_request_token(force_token_regeneration = false)
if force_token_regeneration || @request_token.nil?
- response = get(FireEagle::REQUEST_TOKEN_PATH, :token => nil)
- @request_token = create_token(response)
+ @request_token = consumer.get_request_token
end
@request_token
end
# Return the Fire Eagle authorization URL for your mobile application. At this URL, the User will be prompted for their request_token.
@@ -114,139 +113,131 @@
end
# The URL the user must access to authorize this token. request_token must be called first. For use by web-based and desktop-based applications.
def authorization_url
raise FireEagle::ArgumentError, "call #get_request_token first" if @request_token.nil?
- "#{FireEagle::AUTHORIZATION_URL}?oauth_token=#{@request_token.token}"
+ request_token.authorize_url
end
#Exchange an authorized OAuth Request token for an access token. For use by desktop-based and mobile applications.
def convert_to_access_token
raise FireEagle::ArgumentError, "call #get_request_token and have user authorize the token first" if @request_token.nil?
- response = get(FireEagle::ACCESS_TOKEN_PATH, :token => @request_token)
- @access_token = create_token(response)
+ @access_token = request_token.get_access_token
end
# Disambiguates potential values for update query. Results from lookup can be passed to
# update to ensure that FireEagle will understand how to parse the Location Hash.
#
# All three Location methods (lookup, update, and within) accept a Location Hash.
#
# There is a specific order for looking up locations. For example, if you provide lat, lon, and address,
# FireEagle will use the the latitude and longitude geo-coordinates and ignore the address.
- #
+ #
# Location Hash keys, in order of priority:
- #
- # [<tt>(:lat, :lon)</tt>] both required, valid values are floats of -180 to 180 for lat and -90 to 90 for lon
- # [<tt>(:woeid)</tt>] Where on Earth ID
- # [<tt>:place_id</tt>] Place ID (via Flickr/Upcomoing); deprecated in favor of WOE IDs when possible
- # [<tt>:address</tt>] street address (may contain a full address, but will be combined with postal, city, state, and country when available)
- # [<tt>(:mnc, :mcc, :lac, :cid)</tt>] cell tower information, all required (as integers) for a valid tower location
- # [<tt>:postal</tt>] a ZIP or postal code (combined with address, city, state, and country when available)
- # [<tt>:city</tt>] city (combined with address, postal, state, and country when available)
- # [<tt>:state</tt>] state (combined with address, postal, city, and country when available)
- # [<tt>:country</tt>] country (combined with address, postal, city, and state when available)
- # [<tt>:q</tt>] Free-text fallback containing user input. Lat/lon pairs and geometries will be extracted if possible, otherwise this string will be geocoded as-is.
#
+ # [<tt>(:lat, :lon)</tt>] both required, valid values are floats of -180 to 180 for lat and -90 to 90 for lon
+ # [<tt>(:woeid)</tt>] Where on Earth ID
+ # [<tt>:place_id</tt>] Place ID (via Flickr/Upcomoing); deprecated in favor of WOE IDs when possible
+ # [<tt>:address</tt>] street address (may contain a full address, but will be combined with postal, city, state, and country when available)
+ # [<tt>(:mnc, :mcc, :lac, :cellid)</tt>] cell tower information, all required (as integers) for a valid tower location
+ # [<tt>:postal</tt>] a ZIP or postal code (combined with address, city, state, and country when available)
+ # [<tt>:city</tt>] city (combined with address, postal, state, and country when available)
+ # [<tt>:state</tt>] state (combined with address, postal, city, and country when available)
+ # [<tt>:country</tt>] country (combined with address, postal, city, and state when available)
+ # [<tt>:q</tt>] Free-text fallback containing user input. Lat/lon pairs and geometries will be extracted if possible, otherwise this string will be geocoded as-is.
+ #
# Not yet supported:
#
# * <tt>upcoming_venue_id</tt>
# * <tt>yahoo_local_id</tt>
# * <tt>plazes_id</tt>
def lookup(params)
raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
-
response = get(FireEagle::LOOKUP_API_PATH + ".#{format}", :params => params)
-
FireEagle::Response.new(response.body).locations
end
# Sets a User's current Location using using a Place ID hash or a set of Location parameters. If the User
# provides a Location unconfirmed with lookup then FireEagle makes a best guess as to the User's Location.
#
# All three Location methods (lookup, update, and within) accept a Location Hash.
#
# There is a specific order for looking up locations. For example, if you provide lat, lon, and address,
# FireEagle will use the the latitude and longitude geo-coordinates and ignore the address.
- #
+ #
# Location Hash keys, in order of priority:
- #
- # [<tt>(:lat, :lon)</tt>] both required, valid values are floats of -180 to 180 for lat and -90 to 90 for lon
- # [<tt>:place_id</tt>] Place ID - valid values decrypts to an integer value
- # [<tt>:address</tt>] street address (may contain a full address, but will be combined with postal, city, state, and country when available)
- # [<tt>(:mnc, :mcc, :lac, :cid)</tt>] cell tower information, all required (as integers) for a valid tower location
- # [<tt>:postal</tt>] a ZIP or postal code (combined with address, city, state, and country when available)
- # [<tt>:city</tt>] city (combined with address, postal, state, and country when available)
- # [<tt>:state</tt>] state (combined with address, postal, city, and country when available)
- # [<tt>:country</tt>] country (combined with address, postal, city, and state when available)
- # [<tt>:q</tt>] Free-text fallback containing user input. Lat/lon pairs and geometries will be extracted if possible, otherwise this string will be geocoded as-is.
#
+ # [<tt>(:lat, :lon)</tt>] both required, valid values are floats of -180 to 180 for lat and -90 to 90 for lon
+ # [<tt>:place_id</tt>] Place ID - valid values decrypts to an integer value
+ # [<tt>:address</tt>] street address (may contain a full address, but will be combined with postal, city, state, and country when available)
+ # [<tt>(:mnc, :mcc, :lac, :cellid)</tt>] cell tower information, all required (as integers) for a valid tower location
+ # [<tt>:postal</tt>] a ZIP or postal code (combined with address, city, state, and country when available)
+ # [<tt>:city</tt>] city (combined with address, postal, state, and country when available)
+ # [<tt>:state</tt>] state (combined with address, postal, city, and country when available)
+ # [<tt>:country</tt>] country (combined with address, postal, city, and state when available)
+ # [<tt>:q</tt>] Free-text fallback containing user input. Lat/lon pairs and geometries will be extracted if possible, otherwise this string will be geocoded as-is.
+ #
# Not yet supported:
#
# * <tt>upcoming_venue_id</tt>
# * <tt>yahoo_local_id</tt>
# * <tt>plazes_id</tt>
def update(location = {})
raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
-
location = sanitize_location_hash(location)
-
response = post(FireEagle::UPDATE_API_PATH + ".#{format}", :params => location)
-
FireEagle::Response.new(response.body)
end
# Returns the Location of a User.
def user
raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
-
response = get(FireEagle::USER_API_PATH + ".#{format}")
-
FireEagle::Response.new(response.body).users.first
end
alias_method :location, :user
- # Query for Users of an Application who have updated their Location recently. Returns a list of
+ # Query for Users of an Application who have updated their Location recently. Returns a list of
# Users for the Application with recently updated locations.
- def recent(count = 10, start = 0)
+ #
+ # == Optional parameters:
+ #
+ # <tt>count</tt> Number of users to return per page. (default: 10)
+ # <tt>start</tt> The page number at which to start returning the list of users. Pages are 0-indexed, each page contains the per_page number of users. (default: 0)
+ # <tt>time</tt> The time to start looking at recent updates from. Value is flexible, supported forms are 'now', 'yesterday', '12:00', '13:00', '1:00pm' and '2008-03-12 12:34:56'. (default: 'now')
+ def recent(count = 10, start = 0, time = 'now')
raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
-
- params = { :count => count, :start => start }
-
+ params = { :count => count, :start => start, :time => time }
response = get(FireEagle::RECENT_API_PATH + ".#{format}", :params => params)
-
FireEagle::Response.new(response.body).users
end
# Takes a Place ID or a Location and returns a list of users of your application who are within the bounding box of that Location.
#
# Location Hash keys, in order of priority:
- #
- # [<tt>(:lat, :lon)</tt>] both required, valid values are floats of -180 to 180 for lat and -90 to 90 for lon
- # [<tt>:woeid</tt>] Where on Earth ID
- # [<tt>:place_id</tt>] Place ID
- # [<tt>:address</tt>] street address (may contain a full address, but will be combined with postal, city, state, and country when available)
- # [<tt>(:mnc, :mcc, :lac, :cid)</tt>] cell tower information, all required (as integers) for a valid tower location
- # [<tt>:postal</tt>] a ZIP or postal code (combined with address, city, state, and country when available)
- # [<tt>:city</tt>] city (combined with address, postal, state, and country when available)
- # [<tt>:state</tt>] state (combined with address, postal, city, and country when available)
- # [<tt>:country</tt>] country (combined with address, postal, city, and state when available)
- # [<tt>:q</tt>] Free-text fallback containing user input. Lat/lon pairs and geometries will be extracted if possible, otherwise this string will be geocoded as-is.
#
+ # [<tt>(:lat, :lon)</tt>] both required, valid values are floats of -180 to 180 for lat and -90 to 90 for lon
+ # [<tt>:woeid</tt>] Where on Earth ID
+ # [<tt>:place_id</tt>] Place ID
+ # [<tt>:address</tt>] street address (may contain a full address, but will be combined with postal, city, state, and country when available)
+ # [<tt>(:mnc, :mcc, :lac, :cellid)</tt>] cell tower information, all required (as integers) for a valid tower location
+ # [<tt>:postal</tt>] a ZIP or postal code (combined with address, city, state, and country when available)
+ # [<tt>:city</tt>] city (combined with address, postal, state, and country when available)
+ # [<tt>:state</tt>] state (combined with address, postal, city, and country when available)
+ # [<tt>:country</tt>] country (combined with address, postal, city, and state when available)
+ # [<tt>:q</tt>] Free-text fallback containing user input. Lat/lon pairs and geometries will be extracted if possible, otherwise this string will be geocoded as-is.
+ #
# Not yet supported:
#
# * <tt>upcoming_venue_id</tt>
# * <tt>yahoo_local_id</tt>
# * <tt>plazes_id</tt>
def within(location = {}, count = 10, start = 0)
raise FireEagle::ArgumentError, "OAuth Access Token Required" unless @access_token
-
location = sanitize_location_hash(location)
params = { :count => count, :start => start }.merge(location)
-
response = get(FireEagle::WITHIN_API_PATH + ".#{format}", :params => params)
-
FireEagle::Response.new(response.body).users
end
protected
@@ -255,62 +246,36 @@
location[k.to_sym] = v
end
location = location.reject { |key, value| !FireEagle::UPDATE_PARAMS.include?(key) }
raise FireEagle::ArgumentError, "Requires all or none of :lat, :lon" unless location.has_all_or_none_keys?(:lat, :lon)
- raise FireEagle::ArgumentError, "Requires all or none of :mnc, :mcc, :lac, :cellid" unless location.has_all_or_none_keys?(:mnc, :mcc, :lac, :cid)
+ raise FireEagle::ArgumentError, "Requires all or none of :mnc, :mcc, :lac, :cellid" unless location.has_all_or_none_keys?(:mnc, :mcc, :lac, :cellid)
location
end
- def xml? #:nodoc:
- format == FireEagle::FORMAT_XML
- end
-
- def create_token(response) #:nodoc:
- token = Hash[*response.body.split("&").map { |x| x.split("=") }.flatten]
- OAuth::Token.new(token["oauth_token"], token["oauth_token_secret"])
- end
-
- # Is the Client in debug mode?
- def debug?
- @debug == true
- end
-
def get(url, options = {}) #:nodoc:
request(:get, url, options)
end
def post(url, options = {}) #:nodoc:
request(:post, url, options)
end
def request(method, url, options) #:nodoc:
- options = {
- :params => {},
- :token => @access_token
- }.merge(options)
-
- request_uri = URI.parse(FireEagle::API_SERVER + url)
- http = Net::HTTP.new(request_uri.host, request_uri.port)
- http.set_debug_output $stderr if debug?
- if FireEagle::API_SERVER =~ /https:/
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ response = case method
+ when :post
+ access_token.request(:post, url, options[:params])
+ when :get
+ qs = options[:params].collect { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
+ access_token.request(:get, "#{url}?#{qs}")
+ else
+ raise ArgumentError, "method #{method} not supported"
end
- request = nil
- if method == :post
- request = Net::HTTP::Post.new(request_uri.path)
- request.set_form_data(options[:params])
- elsif method == :get
- 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)
+ case response.code
+ when '500'; then raise FireEagle::FireEagleException, "Internal Server Error"
+ when '400'; then raise FireEagle::FireEagleException, "Method Not Implemented Yet"
+ else response
end
- request.oauth!(http, consumer, options[:token])
- response = http.request(request)
- raise FireEagle::FireEagleException, "Internal Server Error" if response.code == '500'
- raise FireEagle::FireEagleException, "Method Not Implemented Yet" if response.code == '400'
- response
end
-
end
-end
\ No newline at end of file
+end