Skip to content

Commit

Permalink
Refactor the client connection and request methods to accept an optio…
Browse files Browse the repository at this point in the history
…ns hash rather than several optional arguments, in preparation for a new option
  • Loading branch information
laserlemon committed Sep 8, 2011
1 parent 1e55df5 commit f7570de
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 56 deletions.
4 changes: 2 additions & 2 deletions lib/twitter/client/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/twitter/client/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client/list_members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client/list_subscribers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/twitter/client/trends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client/tweets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client/urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/client/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 13 additions & 14 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,32 @@ 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?
builder.use Faraday::Request::Multipart
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)
Expand Down
37 changes: 16 additions & 21 deletions lib/twitter/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f7570de

Please sign in to comment.