Permalink
Browse files

Merged patch for update_profile_background

  • Loading branch information...
2 parents 2a50465 + d489f6f commit 3eeed693180d15ba4ca2370c41bd5547f715fc88 Wynn Netherland committed Nov 29, 2009
View
@@ -11,7 +11,7 @@ Jeweler::Tasks.new do |gem|
gem.rubyforge_project = "twitter"
gem.files = FileList["[A-Z]*", "{examples,lib,test}/**/*"]
- gem.add_dependency('oauth', '~> 0.3.5')
+ gem.add_dependency('oauth', '~> 0.3.6')
gem.add_dependency('hashie', '~> 0.1.3')
gem.add_dependency('httparty', '~> 0.4.3')
View
@@ -1,7 +1,7 @@
require 'forwardable'
require 'rubygems'
-gem 'oauth', '~> 0.3.5'
+gem 'oauth', '~> 0.3.6'
require 'oauth'
gem 'hashie', '~> 0.1.3'
View
@@ -132,7 +132,12 @@ def update_delivery_device(device)
def update_profile_colors(colors={})
perform_post('/account/update_profile_colors.json', :body => colors)
end
-
+
+ # file should respond to #read and #path
+ def update_profile_background(file, tile = false)
+ perform_post('/account/update_profile_background_image.json', build_multipart_bodies(:image => file).merge(:tile => tile))
+ end
+
def rate_limit_status
perform_get('/account/rate_limit_status.json')
end
@@ -243,7 +248,42 @@ def list_subscriptions(list_owner_username)
def blocked_ids
perform_get("/blocks/blocking/ids.json", :mash => false)
end
-
+
+ protected
+ def self.mime_type(file)
+ case
+ when file =~ /\.jpg/ then 'image/jpg'
+ when file =~ /\.gif$/ then 'image/gif'
+ when file =~ /\.png$/ then 'image/png'
+ else 'application/octet-stream'
+ end
+ end
+ def mime_type(f) self.class.mime_type(f) end
+
+ CRLF = "\r\n"
+ def self.build_multipart_bodies(parts)
+ boundary = Time.now.to_i.to_s(16)
+ body = ""
+ parts.each do |key, value|
+ esc_key = CGI.escape(key.to_s)
+ body << "--#{boundary}#{CRLF}"
+ if value.respond_to?(:read)
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{CRLF}"
+ body << "Content-Type: #{mime_type(value.path)}#{CRLF*2}"
+ body << value.read
+ else
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}"
+ end
+ body << CRLF
+ end
+ body << "--#{boundary}--#{CRLF*2}"
+ {
+ :body => body,
+ :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"}
+ }
+ end
+ def build_multipart_bodies(parts) self.class.build_multipart_bodies(parts) end
+
private
def perform_get(path, options={})
Twitter::Request.get(self, path, options)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1 @@
+{"profile_background_tile":false,"friends_count":159,"description":"Loves his wife, ruby, notre dame football and iu basketball","utc_offset":-18000,"notifications":null,"favourites_count":79,"profile_background_color":"1A1B1F","following":null,"profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/61024905\/black250_normal.jpg","statuses_count":4752,"profile_background_image_url":"http:\/\/static.twitter.com\/images\/themes\/theme9\/bg.gif","profile_text_color":"666666","url":"http:\/\/railstips.org\/about","name":"John Nunemaker","time_zone":"Indiana (East)","protected":false,"screen_name":"jnunemaker","status":{"in_reply_to_status_id":null,"in_reply_to_user_id":null,"text":"224! http:\/\/flightcontrolled.com\/ Holy crap. There are some really high flight control scorers out there. *gives up* :D","favorited":false,"in_reply_to_screen_name":null,"created_at":"Tue Apr 14 20:38:49 +0000 2009","truncated":false,"id":1519558635,"source":"<a href=\"http:\/\/twitter.rubyforge.org\">Twitter App<\/a>"},"profile_link_color":"2FC2EF","created_at":"Sun Aug 13 22:56:06 +0000 2006","profile_sidebar_fill_color":"252429","followers_count":1248,"location":"Mishawaka, Indiana","id":4243,"profile_sidebar_border_color":"181A1E"}
View
@@ -20,6 +20,10 @@
class Test::Unit::TestCase
end
+def sample_image(filename)
+ File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
+end
+
def fixture_file(filename)
return '' if filename == ''
file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
@@ -126,6 +126,11 @@ class BaseTest < Test::Unit::TestCase
blocked.should == @twitter.blocked_ids
end
+ should "upload a background image" do
+ stub_post('/account/update_profile_background_image.json', 'update_profile_background_image.json')
+ user = @twitter.update_profile_background(File.new(sample_image('sample-image.png')))
+ user.name.should == 'John Nunemaker' # update_profile_background responds with the user
+ end
end
context "when using lists" do
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John Nunemaker", "Wynn Netherland"]
- s.date = %q{2009-11-25}
+ s.date = %q{2009-11-29}
s.email = %q{nunemaker@gmail.com}
s.extra_rdoc_files = [
"README.rdoc"
@@ -42,10 +42,12 @@ Gem::Specification.new do |s|
"lib/twitter/trends.rb",
"test/fixtures/firehose.json",
"test/fixtures/follower_ids.json",
+ "test/fixtures/followers.json",
"test/fixtures/friend_ids.json",
"test/fixtures/friends_timeline.json",
"test/fixtures/friendship.json",
"test/fixtures/home_timeline.json",
+ "test/fixtures/ids.json",
"test/fixtures/list.json",
"test/fixtures/list_statuses.json",
"test/fixtures/list_subscriptions.json",
@@ -55,6 +57,7 @@ Gem::Specification.new do |s|
"test/fixtures/mentions.json",
"test/fixtures/people_search.json",
"test/fixtures/rate_limit_exceeded.json",
+ "test/fixtures/sample-image.png",
"test/fixtures/search.json",
"test/fixtures/search_from_jnunemaker.json",
"test/fixtures/status.json",
@@ -67,6 +70,7 @@ Gem::Specification.new do |s|
"test/fixtures/trends_weekly.json",
"test/fixtures/trends_weekly_date.json",
"test/fixtures/trends_weekly_exclude.json",
+ "test/fixtures/update_profile_background_image.json",
"test/fixtures/user.json",
"test/fixtures/user_timeline.json",
"test/test_helper.rb",
@@ -112,15 +116,15 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<oauth>, ["~> 0.3.5"])
+ s.add_runtime_dependency(%q<oauth>, ["~> 0.3.6"])
s.add_runtime_dependency(%q<hashie>, ["~> 0.1.3"])
s.add_runtime_dependency(%q<httparty>, ["~> 0.4.3"])
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 2.10.1"])
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
s.add_development_dependency(%q<fakeweb>, [">= 1.2.5"])
else
- s.add_dependency(%q<oauth>, ["~> 0.3.5"])
+ s.add_dependency(%q<oauth>, ["~> 0.3.6"])
s.add_dependency(%q<hashie>, ["~> 0.1.3"])
s.add_dependency(%q<httparty>, ["~> 0.4.3"])
s.add_dependency(%q<thoughtbot-shoulda>, [">= 2.10.1"])
@@ -129,7 +133,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
end
else
- s.add_dependency(%q<oauth>, ["~> 0.3.5"])
+ s.add_dependency(%q<oauth>, ["~> 0.3.6"])
s.add_dependency(%q<hashie>, ["~> 0.1.3"])
s.add_dependency(%q<httparty>, ["~> 0.4.3"])
s.add_dependency(%q<thoughtbot-shoulda>, [">= 2.10.1"])

0 comments on commit 3eeed69

Please sign in to comment.