diff --git a/lib/twitter.rb b/lib/twitter.rb index 860fdc5cf..285abd60e 100644 --- a/lib/twitter.rb +++ b/lib/twitter.rb @@ -10,7 +10,7 @@ module Twitter def self.client; Twitter::Unauthenticated.new end - def_delegators :client, :firehose, :user, :suggestions, :retweeted_to_user, :retweeted_by_user, :status, :friend_ids, :follower_ids, :timeline, :lists_subscribed, :list_timeline + def_delegators :client, :firehose, :user, :suggestions, :retweeted_to_user, :retweeted_by_user, :status, :friend_ids, :follower_ids, :timeline, :lists_subscribed, :list_timeline, :profile_image def self.adapter @adapter ||= Faraday.default_adapter diff --git a/lib/twitter/unauthenticated.rb b/lib/twitter/unauthenticated.rb index f8f63498f..ad5b2e3a0 100644 --- a/lib/twitter/unauthenticated.rb +++ b/lib/twitter/unauthenticated.rb @@ -92,16 +92,18 @@ def list_timeline(list_owner_screen_name, slug, options = {}) end.body end + def profile_image(screen_name) + connection_with_unparsed_response.get do |request| + request.url "users/profile_image/#{screen_name}.json" + end.headers["location"] + end + def connection - headers = { - :user_agent => Twitter.user_agent - } - @connection ||= Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder| - builder.adapter(@adapter || Faraday.default_adapter) - builder.use Faraday::Response::RaiseErrors - builder.use Faraday::Response::ParseJson - builder.use Faraday::Response::Mashify - end + connection_with_builders(Faraday::Response::RaiseErrors, Faraday::Response::ParseJson, Faraday::Response::Mashify) + end + + def connection_with_unparsed_response + connection_with_builders(Faraday::Response::RaiseErrors, Faraday::Response::Mashify) end private @@ -115,6 +117,16 @@ def merge_user_into_options!(user_id_or_screen_name, options={}) end options end + + def connection_with_builders(*builders) + headers = { + :user_agent => Twitter.user_agent + } + Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder| + builder.adapter(@adapter || Faraday.default_adapter) + builders.each do |b| builder.use b end + end + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index b8a973e50..df1b3b2be 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -26,9 +26,10 @@ def twitter_url(url) url =~ /^http/ ? url : "http://api.twitter.com#{url}" end -def stub_get(url, filename, status=nil) +def stub_get(url, filename, status=nil, location=nil) options = {:body => fixture_file(filename)} options.merge!({:status => status}) unless status.nil? + options.merge!({:location => location}) unless location.nil? FakeWeb.register_uri(:get, twitter_url(url), options) end diff --git a/test/twitter/unauthenticated_test.rb b/test/twitter/unauthenticated_test.rb index 7d3a55453..76239b03e 100644 --- a/test/twitter/unauthenticated_test.rb +++ b/test/twitter/unauthenticated_test.rb @@ -134,5 +134,10 @@ class UnauthenticatedTest < Test::Unit::TestCase assert Twitter.list_timeline('pengwynn', 'rubyists', {:page => 2, :per_page => 1}) end end + + should "get a user's profile image" do + stub_get('/1/users/profile_image/ratherchad.json', 'array.json', nil, 'http://a3.twimg.com/profile_images/1107413683/n605431196_2079896_558_normal.jpg') + assert_equal 'http://a3.twimg.com/profile_images/1107413683/n605431196_2079896_558_normal.jpg', Twitter.profile_image( 'ratherchad' ) + end end