Skip to content

Commit

Permalink
Add Twitter::API#profile_banner
Browse files Browse the repository at this point in the history
  • Loading branch information
stve committed Nov 12, 2012
1 parent a5dd0db commit 5879ef3
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/twitter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'twitter/list'
require 'twitter/oembed'
require 'twitter/place'
require 'twitter/profile_banner'
require 'twitter/relationship'
require 'twitter/saved_search'
require 'twitter/search_results'
Expand Down Expand Up @@ -169,6 +170,28 @@ def remove_profile_banner(options={})
post("/1.1/account/remove_profile_banner.json", options)[:body]
end

# Returns the available size variations of the specified user's profile banner.
#
# @see https://dev.twitter.com/docs/api/1.1/get/users/profile_banner
# @note If the user has not uploaded a profile banner, a HTTP 404 will be served instead.
# @rate_limited Yes
# @authentication_required Requires user context
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @return [Twitter::ProfileBanner]
# @overload profile_banner(options={})
# @example Return the authenticated user's profile banner
# Twitter.profile_banner
# @overload profile_banner(user, options={})
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @example Return the specified user's profile banner
# Twitter.profile_banner('sferik')
# Twitter.profile_banner(7505382) # Same as above
def profile_banner(*args)
options = args.extract_options!
options.merge_user!(args.pop || screen_name)
object_from_response(Twitter::ProfileBanner, :get, "/1.1/users/profile_banner.json", options)
end

# Updates the authenticating user's settings.
# Or, if no options supplied, returns settings (including current trend, geo and sleep time information) for the authenticating user.
#
Expand Down
18 changes: 18 additions & 0 deletions lib/twitter/media/profile_banner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'twitter/base'
require 'twitter/size'

module Twitter
class ProfileBanner < Twitter::Base

# Returns an array of photo sizes
#
# @return [Array<Twitter::Size>]
def sizes
@sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
object[key] = Twitter::Size.fetch_or_new(value)
object
end
end

end
end
18 changes: 18 additions & 0 deletions lib/twitter/profile_banner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'twitter/base'
require 'twitter/size'

module Twitter
class ProfileBanner < Twitter::Base

# Returns an array of photo sizes
#
# @return [Array<Twitter::Size>]
def sizes
@sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)|
object[key] = Twitter::Size.fetch_or_new(value)
object
end
end

end
end
1 change: 1 addition & 0 deletions spec/fixtures/profile_banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sizes":{"mobile_retina":{"h":320,"w":640,"url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693\/mobile_retina"},"ipad":{"h":313,"w":626,"url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693\/ipad"},"mobile":{"h":160,"w":320,"url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693\/mobile"},"ipad_retina":{"h":626,"w":1252,"url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693\/ipad_retina"},"web_retina":{"h":520,"w":1040,"url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693\/web_retina"},"web":{"h":260,"w":520,"url":"https:\/\/si0.twimg.com\/profile_banners\/7505382\/1349499693\/web"}}}
35 changes: 35 additions & 0 deletions spec/twitter/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,39 @@
end
end

describe '#profile_banner' do
context "with a screen_name passed" do
before do
stub_get("/1.1/users/profile_banner.json").with(:query => {:screen_name => "sferik"}).to_return(:body => fixture("profile_banner.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.profile_banner('sferik')
expect(a_get("/1.1/users/profile_banner.json").with(:query => {:screen_name => "sferik"})).to have_been_made
end
it "returns a user's profile banner" do
banner = @client.profile_banner('sferik')
expect(banner).to be_an Twitter::ProfileBanner
expect(banner.sizes).to be_a Hash
expect(banner.sizes[:mobile].height).to eq 160
end
end
context "without arguments passed" do
before do
stub_get("/1.1/account/verify_credentials.json").to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/users/profile_banner.json").with(:query => {:screen_name => "sferik"}).to_return(:body => fixture("profile_banner.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.profile_banner
expect(a_get("/1.1/account/verify_credentials.json")).to have_been_made
expect(a_get("/1.1/users/profile_banner.json").with(:query => {:screen_name => "sferik"})).to have_been_made
end
it "returns an array of numeric IDs for every user following the specified user" do
banner = @client.profile_banner
expect(banner).to be_an Twitter::ProfileBanner
expect(banner.sizes).to be_a Hash
expect(banner.sizes[:mobile].height).to eq 160
end
end
end

end
13 changes: 13 additions & 0 deletions spec/twitter/profile_banner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'helper'

describe Twitter::ProfileBanner do

describe "#sizes" do
it "returns a hash of Sizes when sizes is set" do
sizes = Twitter::ProfileBanner.new(:sizes => {:small => {:h => 226, :w => 340, :resize => 'fit'}, :large => {:h => 466, :w => 700, :resize => 'fit'}, :medium => {:h => 399, :w => 600, :resize => 'fit'}, :thumb => {:h => 150, :w => 150, :resize => 'crop'}}).sizes
expect(sizes).to be_a Hash
expect(sizes[:small]).to be_a Twitter::Size
end
end

end

0 comments on commit 5879ef3

Please sign in to comment.