diff --git a/lib/faraday/request/multipart_with_file.rb b/lib/faraday/request/multipart_with_file.rb index d6fc2fbd6..09ef72f4b 100644 --- a/lib/faraday/request/multipart_with_file.rb +++ b/lib/faraday/request/multipart_with_file.rb @@ -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 diff --git a/lib/twitter/authentication.rb b/lib/twitter/authentication.rb index fcf02453e..af3504a99 100644 --- a/lib/twitter/authentication.rb +++ b/lib/twitter/authentication.rb @@ -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 diff --git a/lib/twitter/client/geo.rb b/lib/twitter/client/geo.rb index 30d4bb65d..4ee6a1713 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('geo/search', options)['result']['places'] + get('geo/search', options, :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('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 @@ -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 @@ -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 @@ -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 diff --git a/lib/twitter/client/trends.rb b/lib/twitter/client/trends.rb index dc16f8154..153a33f74 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('trends', options)['trends'] + get('trends', options, :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('trends/current', options)['trends'] + get('trends/current', options, :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('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 @@ -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 diff --git a/lib/twitter/connection.rb b/lib/twitter/connection.rb index e3e63c8ba..68d375f6c 100644 --- a/lib/twitter/connection.rb +++ b/lib/twitter/connection.rb @@ -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, @@ -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) diff --git a/lib/twitter/request.rb b/lib/twitter/request.rb index 552b70c41..f1b6561d3 100644 --- a/lib/twitter/request.rb +++ b/lib/twitter/request.rb @@ -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 diff --git a/lib/twitter/search.rb b/lib/twitter/search.rb index de4ade97f..eed25f155 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])) + @cache = get("search", CGI.parse(@cache["next_page"][1..-1]), :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) + @cache = get("search", options, :json) end @cache.results end diff --git a/spec/faraday/request_spec.rb b/spec/faraday/request_spec.rb index f9e83841f..a4290c963 100644 --- a/spec/faraday/request_spec.rb +++ b/spec/faraday/request_spec.rb @@ -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 diff --git a/spec/twitter/client/block_spec.rb b/spec/twitter/client/block_spec.rb index faa5cbb01..e9f958cda 100644 --- a/spec/twitter/client/block_spec.rb +++ b/spec/twitter/client/block_spec.rb @@ -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 diff --git a/spec/twitter/client/geo_spec.rb b/spec/twitter/client/geo_spec.rb index 26d947d3d..e9aeb17fd 100644 --- a/spec/twitter/client/geo_spec.rb +++ b/spec/twitter/client/geo_spec.rb @@ -1,121 +1,121 @@ require 'helper' describe Twitter::Client do - context ".new" do - before do - @client = Twitter::Client.new(:consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS') - end - - describe ".places_nearby" do - + %w(json xml).each do |format| + context ".new(:format => '#{format}')" do before do - stub_get("geo/search.json"). - with(:query => {:ip => "74.125.19.104"}). - to_return(:body => fixture("places.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + @client = Twitter::Client.new(:format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS') end - it "should get the correct resource" do - @client.places_nearby(:ip => "74.125.19.104") - a_get("geo/search.json"). - with(:query => {:ip => "74.125.19.104"}). - should have_been_made - end + describe ".places_nearby" do - it "should return nearby places" do - places = @client.places_nearby(:ip => "74.125.19.104") - places.should be_an Array - places.first.name.should == "Bernal Heights" - end + before do + stub_get("geo/search.json"). + with(:query => {:ip => "74.125.19.104"}). + to_return(:body => fixture("places.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - end + it "should get the correct resource" do + @client.places_nearby(:ip => "74.125.19.104") + a_get("geo/search.json"). + with(:query => {:ip => "74.125.19.104"}). + should have_been_made + end - describe ".places_similar" do + it "should return nearby places" do + places = @client.places_nearby(:ip => "74.125.19.104") + places.should be_an Array + places.first.name.should == "Bernal Heights" + end - before do - stub_get("geo/similar_places.json"). - with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ"}). - to_return(:body => fixture("places.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end - it "should get the correct resource" do - @client.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ") - a_get("geo/similar_places.json"). - with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ"}). - should have_been_made - end + describe ".places_similar" do - it "should return similar places" do - places = @client.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ") - places.should be_a Hash - places[:places].first.name.should == "Bernal Heights" - end + before do + stub_get("geo/similar_places.json"). + with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ"}). + to_return(:body => fixture("places.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - end + it "should get the correct resource" do + @client.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ") + a_get("geo/similar_places.json"). + with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ"}). + should have_been_made + end - describe ".reverse_geocode" do + it "should return similar places" do + places = @client.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ") + places.should be_a Hash + places[:places].first.name.should == "Bernal Heights" + end - before do - stub_get("geo/reverse_geocode.json"). - with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116"}). - to_return(:body => fixture("places.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end - it "should get the correct resource" do - @client.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116") - a_get("geo/reverse_geocode.json"). - with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116"}). - should have_been_made - end + describe ".reverse_geocode" do - it "should return places" do - places = @client.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116") - places.should be_an Array - places.first.name.should == "Bernal Heights" - end + before do + stub_get("geo/reverse_geocode.json"). + with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116"}). + to_return(:body => fixture("places.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - end + it "should get the correct resource" do + @client.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116") + a_get("geo/reverse_geocode.json"). + with(:query => {:lat => "37.7821120598956", :long => "-122.400612831116"}). + should have_been_made + end - describe ".place" do + it "should return places" do + places = @client.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116") + places.should be_an Array + places.first.name.should == "Bernal Heights" + end - before do - stub_get("geo/id/247f43d441defc03.json"). - to_return(:body => fixture("place.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end - it "should get the correct resource" do - @client.place("247f43d441defc03") - a_get("geo/id/247f43d441defc03.json"). - should have_been_made - end + describe ".place" do - it "should return a place" do - place = @client.place("247f43d441defc03") - place.name.should == "Twitter HQ" - end + before do + stub_get("geo/id/247f43d441defc03.json"). + to_return(:body => fixture("place.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - end + it "should get the correct resource" do + @client.place("247f43d441defc03") + a_get("geo/id/247f43d441defc03.json"). + should have_been_made + end - describe ".place_create" do + it "should return a place" do + place = @client.place("247f43d441defc03") + place.name.should == "Twitter HQ" + end - before do - stub_post("geo/place.json"). - with(:body => {:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581"}). - to_return(:body => fixture("place.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end - - it "should get the correct resource" do - @client.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581") - a_post("geo/place.json"). - with(:body => {:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581"}). - should have_been_made end - it "should return a place" do - place = @client.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581") - place.name.should == "Twitter HQ" + describe ".place_create" do + + before do + stub_post("geo/place.json"). + with(:body => {:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581"}). + to_return(:body => fixture("place.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + + it "should get the correct resource" do + @client.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581") + a_post("geo/place.json"). + with(:body => {:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581"}). + should have_been_made + end + + it "should return a place" do + place = @client.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581") + place.name.should == "Twitter HQ" + end end - end - end end diff --git a/spec/twitter/client/list_members_spec.rb b/spec/twitter/client/list_members_spec.rb index 814839811..89ae4543d 100644 --- a/spec/twitter/client/list_members_spec.rb +++ b/spec/twitter/client/list_members_spec.rb @@ -342,20 +342,20 @@ context "with screen name passed" do before do - stub_get("lists/members/show.#{format}"). + stub_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). - to_return(:body => fixture("list.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) - stub_get("lists/members/show.#{format}"). + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + stub_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '65493023'}). - to_return(:body => fixture("not_found.#{format}"), :status => 404, :headers => {:content_type => "application/#{format}; charset=utf-8"}) - stub_get("lists/members/show.#{format}"). + to_return(:body => fixture("not_found.json"), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"}) + stub_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '12345678'}). - to_return(:body => fixture("not_found.#{format}"), :status => 403, :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("not_found.json"), :status => 403, :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_member?("sferik", "presidents", 813286) - a_get("lists/members/show.#{format}"). + a_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). should have_been_made end @@ -380,14 +380,14 @@ context "with an Integer owner_id passed" do before do - stub_get("lists/members/show.#{format}"). + stub_get("lists/members/show.json"). with(:query => {:owner_id => '12345678', :slug => 'presidents', :user_id => '813286'}). - to_return(:body => fixture("list.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_member?(12345678, "presidents", 813286) - a_get("lists/members/show.#{format}"). + a_get("lists/members/show.json"). with(:query => {:owner_id => '12345678', :slug => 'presidents', :user_id => '813286'}). should have_been_made end @@ -397,14 +397,14 @@ context "with an Integer list_id passed" do before do - stub_get("lists/members/show.#{format}"). + stub_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => '813286'}). - to_return(:body => fixture("list.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_member?('sferik', 12345678, 813286) - a_get("lists/members/show.#{format}"). + a_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => '813286'}). should have_been_made end @@ -414,14 +414,14 @@ context "with screen name passed for user_to_check" do before do - stub_get("lists/members/show.#{format}"). + stub_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :screen_name => 'erebor'}). - to_return(:body => fixture("list.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/.json; charset=utf-8"}) end it "should get the correct resource" do @client.list_member?("sferik", "presidents", 'erebor') - a_get("lists/members/show.#{format}"). + a_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :screen_name => 'erebor'}). should have_been_made end @@ -432,20 +432,18 @@ before do @client.stub!(:get_screen_name).and_return('sferik') - stub_get("lists/members/show.#{format}"). + stub_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). - to_return(:body => fixture("list.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("list.json"), :headers => {:content_type => "application/.json; charset=utf-8"}) end it "should get the correct resource" do @client.list_member?("presidents", 813286) - a_get("lists/members/show.#{format}"). + a_get("lists/members/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). should have_been_made end - end - end end end diff --git a/spec/twitter/client/list_spec.rb b/spec/twitter/client/list_spec.rb index 2b50a7eaf..bc939c13f 100644 --- a/spec/twitter/client/list_spec.rb +++ b/spec/twitter/client/list_spec.rb @@ -549,11 +549,8 @@ with(:query => {:user_id => '12345678', :cursor => "-1"}). should have_been_made end - end - end - end end end diff --git a/spec/twitter/client/list_subscribers_spec.rb b/spec/twitter/client/list_subscribers_spec.rb index 88491071a..e36b57450 100644 --- a/spec/twitter/client/list_subscribers_spec.rb +++ b/spec/twitter/client/list_subscribers_spec.rb @@ -247,20 +247,20 @@ context "with screen name passed" do before do - stub_get("lists/subscribers/show.#{format}"). + stub_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). - to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) - stub_get("lists/subscribers/show.#{format}"). + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + stub_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '18755393'}). - to_return(:body => fixture("not_found.#{format}"), :status => 404, :headers => {:content_type => "application/#{format}; charset=utf-8"}) - stub_get("lists/subscribers/show.#{format}"). + to_return(:body => fixture("not_found.json"), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"}) + stub_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '12345678'}). - to_return(:body => fixture("not_found.#{format}"), :status => 403, :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("not_found.json"), :status => 403, :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_subscriber?("sferik", "presidents", 813286) - a_get("lists/subscribers/show.#{format}"). + a_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). should have_been_made end @@ -285,14 +285,14 @@ context "with an Integer owner_id passed" do before do - stub_get("lists/subscribers/show.#{format}"). + stub_get("lists/subscribers/show.json"). with(:query => {:owner_id => '12345678', :slug => 'presidents', :user_id => '813286'}). - to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_subscriber?(12345678, "presidents", 813286) - a_get("lists/subscribers/show.#{format}"). + a_get("lists/subscribers/show.json"). with(:query => {:owner_id => '12345678', :slug => 'presidents', :user_id => '813286'}). should have_been_made end @@ -302,14 +302,14 @@ context "with an Integer list_id passed" do before do - stub_get("lists/subscribers/show.#{format}"). + stub_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => '813286'}). - to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_subscriber?('sferik', 12345678, 813286) - a_get("lists/subscribers/show.#{format}"). + a_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => '813286'}). should have_been_made end @@ -319,14 +319,14 @@ context "with screen name passed for user_to_check" do before do - stub_get("lists/subscribers/show.#{format}"). + stub_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :screen_name => 'erebor'}). - to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_subscriber?("sferik", "presidents", 'erebor') - a_get("lists/subscribers/show.#{format}"). + a_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :screen_name => 'erebor'}). should have_been_made end @@ -337,20 +337,18 @@ before do @client.stub!(:get_screen_name).and_return('sferik') - stub_get("lists/subscribers/show.#{format}"). + stub_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). - to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end it "should get the correct resource" do @client.list_subscriber?("presidents", 813286) - a_get("lists/subscribers/show.#{format}"). + a_get("lists/subscribers/show.json"). with(:query => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => '813286'}). should have_been_made end - end - end end end diff --git a/spec/twitter/client/timeline_spec.rb b/spec/twitter/client/timeline_spec.rb index 757feaf4d..f22f6a5eb 100644 --- a/spec/twitter/client/timeline_spec.rb +++ b/spec/twitter/client/timeline_spec.rb @@ -105,7 +105,7 @@ end it "should get the correct resource" do - @client.user_timeline() + @client.user_timeline a_get("statuses/user_timeline.#{format}"). with(:query => {:screen_name => "sferik"}). should have_been_made @@ -196,9 +196,7 @@ statuses.should be_an Array statuses.first.text.should == "Ruby is the best programming language for hiding the ugly bits." end - end - end end end diff --git a/spec/twitter/client/trends_spec.rb b/spec/twitter/client/trends_spec.rb index 9d868d990..50b49446b 100644 --- a/spec/twitter/client/trends_spec.rb +++ b/spec/twitter/client/trends_spec.rb @@ -1,91 +1,93 @@ require 'helper' describe Twitter::Client do - context ".new" do - before do - @client = Twitter::Client.new(:consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS') - end - - describe ".trends" do - + %w(json xml).each do |format| + context ".new(:format => '#{format}')" do before do - stub_get("trends.json"). - to_return(:body => fixture("trends.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + @client = Twitter::Client.new(:format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS') end - it "should get the correct resource" do - @client.trends - a_get("trends.json"). - should have_been_made - end + describe ".trends" do - it "should return the top ten topics that are currently trending on Twitter" do - trends = @client.trends - trends.first.name.should == "Isaacs" - end + before do + stub_get("trends.json"). + to_return(:body => fixture("trends.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - end + it "should get the correct resource" do + @client.trends + a_get("trends.json"). + should have_been_made + end - describe ".trends_current" do + it "should return the top ten topics that are currently trending on Twitter" do + trends = @client.trends + trends.first.name.should == "Isaacs" + end - before do - stub_get("trends/current.json"). - to_return(:body => fixture("trends_current.json"), :headers => {:content_type => "application/json; charset=utf-8"}) end - it "should get the correct resource" do - @client.trends_current - a_get("trends/current.json"). - should have_been_made - end + describe ".trends_current" do - it "should return the current top 10 trending topics on Twitter" do - trends = @client.trends_current - trends["2010-10-25 16:00:00"].first.name.should == "Isaacs" - end + before do + stub_get("trends/current.json"). + to_return(:body => fixture("trends_current.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - end - - describe ".trends_daily" do + it "should get the correct resource" do + @client.trends_current + a_get("trends/current.json"). + should have_been_made + end - before do - stub_get("trends/daily.json"). - with(:query => {:date => "2010-10-24"}). - to_return(:body => fixture("trends_daily.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end + it "should return the current top 10 trending topics on Twitter" do + trends = @client.trends_current + trends["2010-10-25 16:00:00"].first.name.should == "Isaacs" + end - it "should get the correct resource" do - @client.trends_daily(Date.parse("2010-10-24")) - a_get("trends/daily.json"). - with(:query => {:date => "2010-10-24"}). - should have_been_made end - it "should return the top 20 trending topics for each hour in a given day" do - trends = @client.trends_daily(Date.parse("2010-10-24")) - trends["2010-10-24 17:00"].first.name.should == "#bigbangcomeback" - end + describe ".trends_daily" do - end + before do + stub_get("trends/daily.json"). + with(:query => {:date => "2010-10-24"}). + to_return(:body => fixture("trends_daily.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end - describe ".trends_weekly" do + it "should get the correct resource" do + @client.trends_daily(Date.parse("2010-10-24")) + a_get("trends/daily.json"). + with(:query => {:date => "2010-10-24"}). + should have_been_made + end - before do - stub_get("trends/weekly.json"). - with(:query => {:date => "2010-10-24"}). - to_return(:body => fixture("trends_weekly.json"), :headers => {:content_type => "application/json; charset=utf-8"}) - end + it "should return the top 20 trending topics for each hour in a given day" do + trends = @client.trends_daily(Date.parse("2010-10-24")) + trends["2010-10-24 17:00"].first.name.should == "#bigbangcomeback" + end - it "should get the correct resource" do - @client.trends_weekly(Date.parse("2010-10-24")) - a_get("trends/weekly.json"). - with(:query => {:date => "2010-10-24"}). - should have_been_made end - it "should return the top 30 trending topics for each day in a given week" do - trends = @client.trends_weekly(Date.parse("2010-10-24")) - trends["2010-10-23"].first.name.should == "#unfollowmeif" + describe ".trends_weekly" do + + before do + stub_get("trends/weekly.json"). + with(:query => {:date => "2010-10-24"}). + to_return(:body => fixture("trends_weekly.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + + it "should get the correct resource" do + @client.trends_weekly(Date.parse("2010-10-24")) + a_get("trends/weekly.json"). + with(:query => {:date => "2010-10-24"}). + should have_been_made + end + + it "should return the top 30 trending topics for each day in a given week" do + trends = @client.trends_weekly(Date.parse("2010-10-24")) + trends["2010-10-23"].first.name.should == "#unfollowmeif" + end end end end diff --git a/spec/twitter/client/user_spec.rb b/spec/twitter/client/user_spec.rb index d08280884..23813ed1e 100644 --- a/spec/twitter/client/user_spec.rb +++ b/spec/twitter/client/user_spec.rb @@ -92,7 +92,7 @@ end it "should get the correct resource" do - @client.user() + @client.user a_get("users/show.#{format}"). with(:query => {:screen_name => "sferik"}). should have_been_made @@ -105,17 +105,17 @@ describe ".user?" do before do - stub_get("users/show.#{format}"). + stub_get("users/show.json"). with(:query => {:screen_name => "sferik"}). - to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"}) - stub_get("users/show.#{format}"). + to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + stub_get("users/show.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.user?("sferik") - a_get("users/show.#{format}"). + a_get("users/show.json"). with(:query => {:screen_name => "sferik"}). should have_been_made end @@ -283,13 +283,13 @@ context "with screen name passed" do before do - stub_get("users/profile_image/sferik.#{format}"). + stub_get("users/profile_image/sferik.json"). to_return(fixture("profile_image.text")) end it "should redirect to the correct resource" do profile_image = @client.profile_image("sferik") - a_get("users/profile_image/sferik.#{format}"). + a_get("users/profile_image/sferik.json"). with(:status => 302). should have_been_made profile_image.should == "http://a0.twimg.com/profile_images/323331048/me_normal.jpg" @@ -301,13 +301,13 @@ before do @client.stub!(:get_screen_name).and_return('sferik') - stub_get("users/profile_image/sferik.#{format}"). + stub_get("users/profile_image/sferik.json"). to_return(fixture("profile_image.text")) end it "should redirect to the correct resource" do - profile_image = @client.profile_image() - a_get("users/profile_image/sferik.#{format}"). + profile_image = @client.profile_image + a_get("users/profile_image/sferik.json"). with(:status => 302). should have_been_made profile_image.should == "http://a0.twimg.com/profile_images/323331048/me_normal.jpg"