Skip to content

Commit

Permalink
Refactor raw to format
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 24, 2011
1 parent 7aedce3 commit b70f879
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 272 deletions.
12 changes: 8 additions & 4 deletions lib/faraday/request/multipart_with_file.rb
Expand Up @@ -20,10 +20,14 @@ def call(env)

def mime_type(file)
case file.path
when /\.jpe?g/i then 'image/jpeg'
when /\.gif$/i then 'image/gif'
when /\.png$/i then 'image/png'
else 'application/octet-stream'
when /\.jpe?g/i
'image/jpeg'
when /\.gif$/i
'image/gif'
when /\.png$/i
'image/png'
else
'application/octet-stream'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/authentication.rb
Expand Up @@ -11,7 +11,7 @@ def authentication
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:token => oauth_token,
:token_secret => oauth_token_secret
:token_secret => oauth_token_secret,
}
end

Expand Down
10 changes: 5 additions & 5 deletions lib/twitter/client/geo.rb
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('geo/search', options)['result']['places']
get('geo/search', options, :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('geo/similar_places', options)['result']
get('geo/similar_places', options, :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('geo/reverse_geocode', options)['result']['places']
get('geo/reverse_geocode', options, :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("geo/id/#{place_id}", options)
get("geo/id/#{place_id}", options, :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('geo/place', options)
post('geo/place', options, :json)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/twitter/client/trends.rb
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('trends', options)['trends']
get('trends', options, :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('trends/current', options)['trends']
get('trends/current', options, :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('trends/daily', options.merge(:date => date.strftime('%Y-%m-%d')))['trends']
get('trends/daily', options.merge(:date => date.strftime('%Y-%m-%d')), :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('trends/weekly', options.merge(:date => date.strftime('%Y-%m-%d')))['trends']
get('trends/weekly', options.merge(:date => date.strftime('%Y-%m-%d')), :json)['trends']
end
end
end
Expand Down
22 changes: 12 additions & 10 deletions lib/twitter/connection.rb
Expand Up @@ -10,9 +10,12 @@ module Twitter
module Connection
private

def connection(raw=false)
def connection(format=format)
options = {
:headers => {'Accept' => "application/#{format}", 'User-Agent' => user_agent},
:headers => {
'Accept' => "application/#{format}",
'User-Agent' => user_agent,
},
:proxy => proxy,
:ssl => {:verify => false},
:url => api_endpoint,
Expand All @@ -25,14 +28,13 @@ def connection(raw=false)
builder.use Faraday::Request::UrlEncoded
builder.use Faraday::Request::Gateway, gateway if gateway
builder.use Faraday::Response::RaiseHttp4xx
builder.use Faraday::Response::Mashify unless raw
unless raw
case format.to_s.downcase
when 'json'
builder.use Faraday::Response::ParseJson
when 'xml'
builder.use Faraday::Response::ParseXml
end
case format.to_s.downcase
when 'json'
builder.use Faraday::Response::Mashify
builder.use Faraday::Response::ParseJson
when 'xml'
builder.use Faraday::Response::Mashify
builder.use Faraday::Response::ParseXml
end
builder.use Faraday::Response::RaiseHttp5xx
builder.adapter(adapter)
Expand Down
37 changes: 21 additions & 16 deletions lib/twitter/request.rb
Expand Up @@ -2,43 +2,48 @@ module Twitter
# Defines HTTP request methods
module Request
# Perform an HTTP GET request
def get(path, options={}, raw=false)
request(:get, path, options, raw)
def get(path, options={}, format=format)
request(:get, path, options, format)
end

# Perform an HTTP POST request
def post(path, options={}, raw=false)
request(:post, path, options, raw)
def post(path, options={}, format=format)
request(:post, path, options, format)
end

# Perform an HTTP PUT request
def put(path, options={}, raw=false)
request(:put, path, options, raw)
def put(path, options={}, format=format)
request(:put, path, options, format)
end

# Perform an HTTP DELETE request
def delete(path, options={}, raw=false)
request(:delete, path, options, raw)
def delete(path, options={}, format=format)
request(:delete, path, options, format)
end

private

# Perform an HTTP request
def request(method, path, options, raw=false)
response = connection(raw).send(method) do |request|
case method
def request(method, path, options, format)
response = connection(format).send(method) do |request|
case method.to_sym
when :get, :delete
request.url(formatted_path(path), options)
request.url(formatted_path(path, format), options)
when :post, :put
request.path = formatted_path(path)
request.path = formatted_path(path, format)
request.body = options unless options.empty?
end
end
raw ? response : response.body
'raw' == format.to_s.downcase ? response : response.body
end

def formatted_path(path)
[path, format].compact.join('.')
def formatted_path(path, format)
case format.to_s.downcase
when 'json', 'xml'
[path, format].compact.join('.')
when 'raw'
[path, Twitter.format].compact.join('.')
end
end
end
end
4 changes: 2 additions & 2 deletions lib/twitter/search.rb
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]))
@cache = get("search", CGI.parse(@cache["next_page"][1..-1]), :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)
@cache = get("search", options, :json)
end
@cache.results
end
Expand Down
26 changes: 11 additions & 15 deletions spec/faraday/request_spec.rb
Expand Up @@ -2,28 +2,24 @@

describe Faraday::Request do
before(:each) do
@oauth = Faraday::Request::TwitterOAuth.new lambda { |env| return env }, Hash.new
@req = { :method => "post", :url => "http://test.com/test.JSON", :request_headers => {}, :body => { :status => "Yar" } }
@oauth = Faraday::Request::TwitterOAuth.new(lambda{|env| env}, Hash.new)
@request = {:method => "post", :url => "http://test.com/test.json", :request_headers => {}, :body => {:status => "Test"}}
end

describe "OAuth v1" do
it "should encode the entire body when no uploaded media is present" do
res = SimpleOAuth::Header.parse(@oauth.call( @req )[:request_headers]["Authorization"])
sig = res[:signature]
res.delete(:signature)

newhead = SimpleOAuth::Header.new(:post, "http://test.com/test.JSON", { :status => "Yar" }, res)
sig.should == SimpleOAuth::Header.parse(newhead.to_s)[:signature]
response = SimpleOAuth::Header.parse(@oauth.call(@request)[:request_headers]["Authorization"])
signature = response.delete(:signature)
header = SimpleOAuth::Header.new(:post, "http://test.com/test.json", {:status => "Test"}, response)
signature.should == SimpleOAuth::Header.parse(header.to_s)[:signature]
end

it "should encode none of the body when uploaded media is present" do
@req[:body] = { :file => Faraday::UploadIO.new("Rakefile", "Test"), :status => "Yar" }
res = SimpleOAuth::Header.parse(@oauth.call( @req )[:request_headers]["Authorization"])
sig = res[:signature]
res.delete(:signature)

newhead = SimpleOAuth::Header.new(:post, "http://test.com/test.JSON", { }, res)
sig.should == SimpleOAuth::Header.parse(newhead.to_s)[:signature]
@request[:body] = {:file => Faraday::UploadIO.new("Rakefile", "Test"), :status => "Test"}
response = SimpleOAuth::Header.parse(@oauth.call( @request )[:request_headers]["Authorization"])
signature = response.delete(:signature)
header = SimpleOAuth::Header.new(:post, "http://test.com/test.json", {}, response)
signature.should == SimpleOAuth::Header.parse(header.to_s)[:signature]
end
end
end
10 changes: 5 additions & 5 deletions spec/twitter/client/block_spec.rb
Expand Up @@ -53,17 +53,17 @@
describe ".block?" do

before do
stub_get("blocks/exists.#{format}").
stub_get("blocks/exists.json").
with(:query => {:screen_name => "sferik"}).
to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
stub_get("blocks/exists.#{format}").
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("blocks/exists.json").
with(:query => {:screen_name => "pengwynn"}).
to_return(:body => fixture("not_found.#{format}"), :status => 404, :headers => {:content_type => "application/#{format}; charset=utf-8"})
to_return(:body => fixture("not_found.json"), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
end

it "should get the correct resource" do
@client.block?("sferik")
a_get("blocks/exists.#{format}").
a_get("blocks/exists.json").
with(:query => {:screen_name => "sferik"}).
should have_been_made
end
Expand Down

0 comments on commit b70f879

Please sign in to comment.