Skip to content

Commit

Permalink
adding wrapper for twitter's profile image api method
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Krsek authored and sferik committed Oct 8, 2010
1 parent 5d517ee commit e664502
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions lib/twitter/unauthenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions test/twitter/unauthenticated_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e664502

Please sign in to comment.