From 5879ef3fcc486ac3849426ef0d44ee0288ed9599 Mon Sep 17 00:00:00 2001 From: Steve Agalloco Date: Mon, 12 Nov 2012 00:54:22 -0500 Subject: [PATCH] Add Twitter::API#profile_banner --- lib/twitter/api.rb | 23 +++++++++++++++++++ lib/twitter/media/profile_banner.rb | 18 +++++++++++++++ lib/twitter/profile_banner.rb | 18 +++++++++++++++ spec/fixtures/profile_banner.json | 1 + spec/twitter/api/users_spec.rb | 35 +++++++++++++++++++++++++++++ spec/twitter/profile_banner_spec.rb | 13 +++++++++++ 6 files changed, 108 insertions(+) create mode 100644 lib/twitter/media/profile_banner.rb create mode 100644 lib/twitter/profile_banner.rb create mode 100644 spec/fixtures/profile_banner.json create mode 100644 spec/twitter/profile_banner_spec.rb diff --git a/lib/twitter/api.rb b/lib/twitter/api.rb index 7e2ac912a..54b0ad692 100644 --- a/lib/twitter/api.rb +++ b/lib/twitter/api.rb @@ -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' @@ -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. # diff --git a/lib/twitter/media/profile_banner.rb b/lib/twitter/media/profile_banner.rb new file mode 100644 index 000000000..75a38d0a4 --- /dev/null +++ b/lib/twitter/media/profile_banner.rb @@ -0,0 +1,18 @@ +require 'twitter/base' +require 'twitter/size' + +module Twitter + class ProfileBanner < Twitter::Base + + # Returns an array of photo sizes + # + # @return [Array] + def sizes + @sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)| + object[key] = Twitter::Size.fetch_or_new(value) + object + end + end + + end +end diff --git a/lib/twitter/profile_banner.rb b/lib/twitter/profile_banner.rb new file mode 100644 index 000000000..75a38d0a4 --- /dev/null +++ b/lib/twitter/profile_banner.rb @@ -0,0 +1,18 @@ +require 'twitter/base' +require 'twitter/size' + +module Twitter + class ProfileBanner < Twitter::Base + + # Returns an array of photo sizes + # + # @return [Array] + def sizes + @sizes ||= Array(@attrs[:sizes]).inject({}) do |object, (key, value)| + object[key] = Twitter::Size.fetch_or_new(value) + object + end + end + + end +end diff --git a/spec/fixtures/profile_banner.json b/spec/fixtures/profile_banner.json new file mode 100644 index 000000000..9d863933e --- /dev/null +++ b/spec/fixtures/profile_banner.json @@ -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"}}} \ No newline at end of file diff --git a/spec/twitter/api/users_spec.rb b/spec/twitter/api/users_spec.rb index eb9529c62..7505e2786 100644 --- a/spec/twitter/api/users_spec.rb +++ b/spec/twitter/api/users_spec.rb @@ -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 diff --git a/spec/twitter/profile_banner_spec.rb b/spec/twitter/profile_banner_spec.rb new file mode 100644 index 000000000..7a3f8a514 --- /dev/null +++ b/spec/twitter/profile_banner_spec.rb @@ -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