diff --git a/lib/twitter/client/activity.rb b/lib/twitter/client/activity.rb index 98c3485f9..305a3a9fc 100644 --- a/lib/twitter/client/activity.rb +++ b/lib/twitter/client/activity.rb @@ -16,7 +16,7 @@ module Activity # @example Return activity about me # Twitter.about_me def about_me(options={}) - get("i/activity/about_me", options, :json) + get("i/activity/about_me", options, :format => :json) end # Returns activity by friends @@ -33,7 +33,7 @@ def about_me(options={}) # @example Return activity by friends # Twitter.by_friends def by_friends(options={}) - get("i/activity/by_friends", options, :json) + get("i/activity/by_friends", options, :format => :json) end end end diff --git a/lib/twitter/client/block.rb b/lib/twitter/client/block.rb index 684dbacbe..cfb0875b6 100644 --- a/lib/twitter/client/block.rb +++ b/lib/twitter/client/block.rb @@ -57,7 +57,7 @@ def unblock(user, options={}) # Twitter.block?(7505382) # Same as above def block?(user, options={}) merge_user_into_options!(user, options) - get('1/blocks/exists', options, :raw) + get('1/blocks/exists', options, :format => :json, :raw => true) true rescue Twitter::NotFound false diff --git a/lib/twitter/client/geo.rb b/lib/twitter/client/geo.rb index cc419ffeb..7ca4f7f98 100644 --- a/lib/twitter/client/geo.rb +++ b/lib/twitter/client/geo.rb @@ -23,7 +23,7 @@ module Geo # @example Return an array of places near the IP address 74.125.19.104 # Twitter.places_nearby(:ip => "74.125.19.104") def places_nearby(options={}) - get('1/geo/search', options, :json)['result']['places'] + get('1/geo/search', options, :format => :json)['result']['places'] end alias :geo_search :places_nearby @@ -44,7 +44,7 @@ def places_nearby(options={}) # @example Return an array of places similar to Twitter HQ # Twitter.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ") def places_similar(options={}) - get('1/geo/similar_places', options, :json)['result'] + get('1/geo/similar_places', options, :format => :json)['result'] end # Searches for up to 20 places that can be used as a place_id @@ -64,7 +64,7 @@ def places_similar(options={}) # @example Return an array of places within the specified region # Twitter.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116") def reverse_geocode(options={}) - get('1/geo/reverse_geocode', options, :json)['result']['places'] + get('1/geo/reverse_geocode', options, :format => :json)['result']['places'] end # Returns all the information about a known place @@ -79,7 +79,7 @@ def reverse_geocode(options={}) # @example Return all the information about Twitter HQ # Twitter.place("247f43d441defc03") def place(place_id, options={}) - get("1/geo/id/#{place_id}", options, :json) + get("1/geo/id/#{place_id}", options, :format => :json) end # Creates a new place at the given latitude and longitude @@ -99,7 +99,7 @@ def place(place_id, options={}) # @example Create a new place # Twitter.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581") def place_create(options={}) - post('1/geo/place', options, :json) + post('1/geo/place', options, :format => :json) end end end diff --git a/lib/twitter/client/list_members.rb b/lib/twitter/client/list_members.rb index aeae35651..78d0c5261 100644 --- a/lib/twitter/client/list_members.rb +++ b/lib/twitter/client/list_members.rb @@ -193,7 +193,7 @@ def list_member?(*args) merge_list_into_options!(list, options) merge_owner_into_options!(user, options) merge_user_into_options!(user_to_check, options) - get("1/lists/members/show", options, :raw) + get("1/lists/members/show", options, :format => :json, :raw => true) true rescue Twitter::NotFound, Twitter::Forbidden false diff --git a/lib/twitter/client/list_subscribers.rb b/lib/twitter/client/list_subscribers.rb index 23a68e8af..8414b99ea 100644 --- a/lib/twitter/client/list_subscribers.rb +++ b/lib/twitter/client/list_subscribers.rb @@ -145,7 +145,7 @@ def list_subscriber?(*args) merge_list_into_options!(list, options) merge_owner_into_options!(user, options) merge_user_into_options!(user_to_check, options) - get("1/lists/subscribers/show", options, :raw) + get("1/lists/subscribers/show", options, :format => :json, :raw => true) true rescue Twitter::NotFound, Twitter::Forbidden false diff --git a/lib/twitter/client/search.rb b/lib/twitter/client/search.rb index ab0969665..f70e99b32 100644 --- a/lib/twitter/client/search.rb +++ b/lib/twitter/client/search.rb @@ -56,7 +56,7 @@ def videos(q, options={}) # @example Return recent statuses related to twitter with images and videos embedded # Twitter.search('twitter') def search(q, options={}) - response = get('phoenix_search', options.merge(:q => q), :phoenix)['statuses'] + response = get('phoenix_search', options.merge(:q => q), :format => :phoenix)['statuses'] end end end diff --git a/lib/twitter/client/trends.rb b/lib/twitter/client/trends.rb index abfd10591..be8a3505a 100644 --- a/lib/twitter/client/trends.rb +++ b/lib/twitter/client/trends.rb @@ -14,7 +14,7 @@ module Trends # @example Return the top ten topics that are currently trending on Twitter # Twitter.trends def trends(options={}) - get('1/trends', options, :json)['trends'] + get('1/trends', options, :format => :json)['trends'] end # Returns the current top 10 trending topics on Twitter @@ -29,7 +29,7 @@ def trends(options={}) # @example Return the current top 10 trending topics on Twitter # Twitter.trends_current def trends_current(options={}) - get('1/trends/current', options, :json)['trends'] + get('1/trends/current', options, :format => :json)['trends'] end # Returns the top 20 trending topics for each hour in a given day @@ -45,7 +45,7 @@ def trends_current(options={}) # @example Return the top 20 trending topics for each hour of October 24, 2010 # Twitter.trends_daily(Date.parse("2010-10-24")) def trends_daily(date=Date.today, options={}) - get('1/trends/daily', options.merge(:date => date.strftime('%Y-%m-%d')), :json)['trends'] + get('1/trends/daily', options.merge(:date => date.strftime('%Y-%m-%d')), :format => :json)['trends'] end # Returns the top 30 trending topics for each day in a given week @@ -61,7 +61,7 @@ def trends_daily(date=Date.today, options={}) # @example Return the top ten topics that are currently trending on Twitter # Twitter.trends_weekly(Date.parse("2010-10-24")) def trends_weekly(date=Date.today, options={}) - get('1/trends/weekly', options.merge(:date => date.strftime('%Y-%m-%d')), :json)['trends'] + get('1/trends/weekly', options.merge(:date => date.strftime('%Y-%m-%d')), :format => :json)['trends'] end end end diff --git a/lib/twitter/client/tweets.rb b/lib/twitter/client/tweets.rb index d3c98b1c9..df4a49deb 100644 --- a/lib/twitter/client/tweets.rb +++ b/lib/twitter/client/tweets.rb @@ -72,7 +72,7 @@ def update(status, options={}) # download the pic and put the response in a StringIO object # Twitter.update("I just posted a status update with a pic via the Twitter Ruby Gem!", {'io' => StringIO.new(pic), 'type' => 'jpg'}) def update_with_media(status, image, options={}) - response = post('1/statuses/update_with_media', options.merge('media[]' => image, 'status' => status), format, media_endpoint) + response = post('1/statuses/update_with_media', options.merge('media[]' => image, 'status' => status), :endpoint => media_endpoint) format.to_s.downcase == 'xml' ? response['status'] : response end diff --git a/lib/twitter/client/urls.rb b/lib/twitter/client/urls.rb index 378110551..d33a5603e 100644 --- a/lib/twitter/client/urls.rb +++ b/lib/twitter/client/urls.rb @@ -17,7 +17,7 @@ module Urls # Twitter.resolve(['http://t.co/uw5bn1w', 'http://t.co/dXvMz9i']) # Same as above def resolve(*args) options = args.last.is_a?(Hash) ? args.pop : {} - get("1/urls/resolve", options.merge("urls[]" => args), :json) + get("1/urls/resolve", options.merge("urls[]" => args), :format => :json) end end end diff --git a/lib/twitter/client/user.rb b/lib/twitter/client/user.rb index 368d95f91..d24e85214 100644 --- a/lib/twitter/client/user.rb +++ b/lib/twitter/client/user.rb @@ -36,7 +36,7 @@ def user(*args) # @rate_limited Yes def user?(user, options={}) merge_user_into_options!(user, options) - get('1/users/show', options, :raw) + get('1/users/show', options, :format => :json, :raw => true) true rescue Twitter::NotFound false @@ -153,7 +153,7 @@ def suggest_users(slug, options={}) def profile_image(*args) options = args.last.is_a?(Hash) ? args.pop : {} screen_name = args.first || get_screen_name - get("1/users/profile_image/#{screen_name}", options, :raw).headers['location'] + get("1/users/profile_image/#{screen_name}", options, :format => :json, :raw => true).headers['location'] end # Returns a user's friends diff --git a/lib/twitter/connection.rb b/lib/twitter/connection.rb index fa5995f8f..ac7af6aa5 100644 --- a/lib/twitter/connection.rb +++ b/lib/twitter/connection.rb @@ -11,19 +11,16 @@ module Twitter module Connection private - def connection(format=format, temp_api_endpoint=nil) - options = { + def connection(options={}) + Faraday.new( :headers => { 'Accept' => "application/#{format}", 'User-Agent' => user_agent }, :proxy => proxy, :ssl => {:verify => false}, - } - - options[:url] = temp_api_endpoint ? temp_api_endpoint : api_endpoint - - Faraday.new(options) do |builder| + :url => options.fetch(:endpoint, api_endpoint) + ) do |builder| builder.use Faraday::Request::Phoenix builder.use Faraday::Request::MultipartWithFile builder.use Faraday::Request::TwitterOAuth, authentication if authenticated? @@ -31,13 +28,15 @@ def connection(format=format, temp_api_endpoint=nil) builder.use Faraday::Request::UrlEncoded builder.use Faraday::Request::Gateway, gateway if gateway builder.use Faraday::Response::RaiseHttp4xx - case format.to_s.downcase - when 'json', 'phoenix' - builder.use Faraday::Response::Mashify - builder.use Faraday::Response::ParseJson - when 'xml' - builder.use Faraday::Response::Mashify - builder.use Faraday::Response::ParseXml + unless options[:raw] + case options.fetch(:format, format).to_s.downcase + when 'json', 'phoenix' + builder.use Faraday::Response::Mashify + builder.use Faraday::Response::ParseJson + when 'xml' + builder.use Faraday::Response::Mashify + builder.use Faraday::Response::ParseXml + end end builder.use Faraday::Response::RaiseHttp5xx builder.adapter(adapter) diff --git a/lib/twitter/request.rb b/lib/twitter/request.rb index 321a07c0e..443709687 100644 --- a/lib/twitter/request.rb +++ b/lib/twitter/request.rb @@ -2,47 +2,42 @@ module Twitter # Defines HTTP request methods module Request # Perform an HTTP GET request - def get(path, options={}, format=format) - request(:get, path, options, format) + def get(path, params={}, options={}) + request(:get, path, params, options) end - def post(path, options={}, format=format, temp_api_endpoint=nil) - request(:post, path, options, format, temp_api_endpoint) + def post(path, params={}, options={}) + request(:post, path, params, options) end # Perform an HTTP PUT request - def put(path, options={}, format=format) - request(:put, path, options, format) + def put(path, params={}, options={}) + request(:put, path, params, options) end # Perform an HTTP DELETE request - def delete(path, options={}, format=format) - request(:delete, path, options, format) + def delete(path, params={}, options={}) + request(:delete, path, params, options) end private # Perform an HTTP request - def request(method, path, options, format, temp_api_endpoint=nil) - response = connection(format, temp_api_endpoint).send(method) do |request| + def request(method, path, params, options) + response = connection(options).send(method) do |request| case method.to_sym when :get, :delete - request.url(formatted_path(path, format), options) + request.url(formatted_path(path, options), params) when :post, :put - request.path = formatted_path(path, format) - request.body = options unless options.empty? + request.path = formatted_path(path, options) + request.body = params unless params.empty? end end - 'raw' == format.to_s.downcase ? response : response.body + options[:raw] ? response : response.body end - def formatted_path(path, format) - case format.to_s.downcase - when 'json', 'phoenix', 'xml' - [path, format].compact.join('.') - when 'raw' - [path, Twitter.format].compact.join('.') - end + def formatted_path(path, options={}) + [path, options.fetch(:format, format)].compact.join('.') end end end diff --git a/lib/twitter/search.rb b/lib/twitter/search.rb index eed25f155..4cd3dec8c 100644 --- a/lib/twitter/search.rb +++ b/lib/twitter/search.rb @@ -442,7 +442,7 @@ def next_page? # search.fetch_next_page def fetch_next_page if next_page? - @cache = get("search", CGI.parse(@cache["next_page"][1..-1]), :json) + @cache = get("search", CGI.parse(@cache["next_page"][1..-1]), :format => :json) @cache.results end end @@ -457,7 +457,7 @@ def fetch(force=false) if @cache.nil? || force options = query.dup options[:q] = options[:q].join(" ") - @cache = get("search", options, :json) + @cache = get("search", options, :format => :json) end @cache.results end