lib/gowalla/client.rb in gowalla-0.2.0 vs lib/gowalla/client.rb in gowalla-0.2.1

- old
+ new

@@ -13,27 +13,28 @@ @api_secret = options[:api_secret] || Gowalla.api_secret @username = options[:username] || Gowalla.username @access_token = options[:access_token] password = options[:password] || Gowalla.password connection.basic_auth(@username, password) unless @api_secret + connection.token_auth(@access_token) if @access_token end # Retrieve information about a specific user # # @param [String] user_id (authenticated basic auth user) User ID (screen name) # @return [Hashie::Mash] User info def user(user_id=nil) user_id ||= username - handle_response(connection.get("/users/#{user_id}")) + connection.get("/users/#{user_id}").body end # Retrieve information about a specific item # # @param [Integer] id Item ID # @return [Hashie::Mash] item info def item(id) - handle_response(connection.get("/items/#{id}")) + connection.get("/items/#{id}").body end # Retrieve a list of the stamps the user has collected # # @param [String] user_id (authenticated basic auth user) User ID (screen name) @@ -41,51 +42,51 @@ # @return [Hashie::Mash] stamps info def stamps(user_id=self.username, limit=20) response = connection.get do |req| req.url "/users/#{user_id}/stamps", :limit => limit end - handle_response(response).stamps + response.body.stamps end # Retrieve a list of spots the user has visited most often # # @param [String] user_id (authenticated basic auth user) User ID (screen name) # @return [Hashie::Mash] item info def top_spots(user_id=self.username) - handle_response(connection.get("/users/#{user_id}/top_spots")).top_spots + connection.get("/users/#{user_id}/top_spots").body.top_spots end # Retrieve information about a specific trip # # @param [Integer] trip_id Trip ID # @return [Hashie::Mash] trip info def trip(trip_id) - handle_response(connection.get("/trips/#{trip_id}")) + connection.get("/trips/#{trip_id}").body end # Retrieve information about a specific spot # # @param [Integer] spot_id Spot ID # @return [Hashie::Mash] Spot info def spot(spot_id) - handle_response(connection.get("/spots/#{spot_id}")) + connection.get("/spots/#{spot_id}").body end # Retrieve a list of check-ins at a particular spot. Shows only the activity that is visible to a given user. # # @param [Integer] spot_id Spot ID # @return [Hashie::Mash] Spot info def spot_events(spot_id) - handle_response(connection.get("/spots/#{spot_id}/events")).activity + connection.get("/spots/#{spot_id}/events").body.activity end # Retrieve a list of items available at a particular spot # # @param [Integer] spot_id Spot ID # @return [Hashie::Mash] Spot info def spot_items(spot_id) - handle_response(connection.get("/spots/#{spot_id}/items")).items + connection.get("/spots/#{spot_id}/items").body.items end # Retrieve a list of spots within a specified distance of a location # # @option options [Float] :latitude Latitude of search location @@ -95,11 +96,11 @@ def list_spots(options={}) query = format_geo_options(options) response = connection.get do |req| req.url "/spots", query end - handle_response(response).spots + response.body.spots end # List of trips # # @return [<Hashie::Mash>] trip info @@ -109,49 +110,48 @@ end query = format_geo_options(options) response = connection.get do |req| req.url "/trips", query end - handle_response(response).trips + response.body.trips end # Lists all spot categories # # @return [<Hashie::Mash>] category info def categories - handle_response(connection.get("/categories")).spot_categories + connection.get("/categories").body.spot_categories end # Retrieve information about a specific category # # @param [Integer] id Category ID # @return [Hashie::Mash] category info def category(id) - handle_response(connection.get("/categories/#{id}")) + connection.get("/categories/#{id}").body end # Check for missing access token # # @return [Boolean] whether or not to redirect to get an access token def needs_access? @api_secret and @access_token.to_s == '' end - # Raw HTTP connection, either Faraday::Connection or OAuth2::AccessToken + # Raw HTTP connection, either Faraday::Connection # - # @return [OAuth2::Client] + # @return [Faraday::Connection] def connection - - if api_secret - @connection ||= OAuth2::AccessToken.new(oauth_client, @access_token) - else - headers = default_headers - headers['X-Gowalla-API-Key'] = api_key if api_key - @connection ||= Faraday::Connection.new \ - :url => "http://api.gowalla.com", - :headers => headers + url = @access_token ? "https://api.gowalla.com" : "http://api.gowalla.com" + params = {} + params[:access_token] = @access_token if @access_token + @connection ||= Faraday::Connection.new(:url => url, :params => params, :headers => default_headers) do |builder| + builder.adapter Faraday.default_adapter + builder.use Faraday::Response::MultiJson + builder.use Faraday::Response::Mashify end + end # Provides raw access to the OAuth2 Client # # @return [OAuth2::Client] @@ -187,29 +187,12 @@ # @private def default_headers headers = { :accept => 'application/json', - :user_agent => 'Ruby gem' + :user_agent => 'Ruby gem', + 'X-Gowalla-API-Key' => api_key } - end - - # @private - def handle_response(response) - case response.status - when 200 - body = response.respond_to?(:body) ? response.body : response - data = MultiJson.decode(body) - if data.is_a?(Hash) - Hashie::Mash.new(data) - else - if data.first.is_a?(Hash) - data.map{|item| Hashie::Mash.new(item)} - else - data - end - end - end end end \ No newline at end of file