Permalink
Browse files

Allow Tempfile to be passed to Twitter::API#update_with_media

Closes #306.
  • Loading branch information...
1 parent 24f759b commit 79dc8197250f0416a9a44524be0aaea9d3f31d83 @sferik committed Aug 26, 2012
Showing with 19 additions and 23 deletions.
  1. +1 −1 README.md
  2. +1 −1 lib/twitter/client.rb
  3. +1 −1 lib/twitter/request/multipart_with_file.rb
  4. +2 −0 spec/helper.rb
  5. +14 −20 spec/twitter/api/statuses_spec.rb
View
2 README.md
@@ -273,7 +273,7 @@ Here are some fun facts about the 3.0 release:
* The entire library is implemented in just 2,000 lines of code
* With over 5,000 lines of specs, the spec-to-code ratio is over 2.5:1
-* The spec suite contains 674 examples and runs in under 2 seconds on a MacBook
+* The spec suite contains 673 examples and runs in under 2 seconds on a MacBook
* This project has 100% C0 code coverage (the tests execute every line of
source code at least once)
* At the time of release, this library is comprehensive: you can request all
View
2 lib/twitter/client.rb
@@ -92,7 +92,7 @@ def request(method, path, params={}, options={})
def auth_header(method, uri, params={})
# When posting a file, don't sign any params
- signature_params = [:post, :put].include?(method.to_sym) && params.values.any?{|value| value.is_a?(File) || (value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO)))} ? {} : params
+ signature_params = [:post, :put].include?(method.to_sym) && params.values.any?{|value| value.respond_to?(:to_io) || (value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO)))} ? {} : params
SimpleOAuth::Header.new(method, uri, signature_params, credentials)
end
View
2 lib/twitter/request/multipart_with_file.rb
@@ -12,7 +12,7 @@ class << self
def call(env)
if env[:body].is_a?(Hash)
env[:body].each do |key, value|
- if value.is_a?(File)
+ if value.respond_to?(:to_io)
env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
env[:request_headers][CONTENT_TYPE] = self.class.mime_type
elsif value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO))
View
2 spec/helper.rb
@@ -7,6 +7,8 @@
require 'twitter'
require 'rspec'
+require 'stringio'
+require 'tempfile'
require 'timecop'
require 'webmock/rspec'
View
34 spec/twitter/api/statuses_spec.rb
@@ -506,11 +506,7 @@
it "returns a Tweet" do
tweet = @client.update_with_media("You always have options", fixture("pbjt.gif"))
tweet.should be_a Twitter::Tweet
- tweet.text.should include("You always have options")
- end
- it 'returns the media as an entity' do
- tweet = @client.update_with_media("You always have options", fixture("pbjt.gif"))
- tweet.media.should be
+ tweet.text.should eq "You always have options http://t.co/CBYa7Ri"
end
end
context "a jpe image" do
@@ -519,42 +515,40 @@
a_post("/1/statuses/update_with_media.json", "https://upload.twitter.com").
should have_been_made
end
- it 'returns the media as an entity' do
- tweet = @client.update_with_media("You always have options", fixture("wildcomet2.jpe"))
- tweet.media.should be
- end
end
context "a jpeg image" do
it "requests the correct resource" do
@client.update_with_media("You always have options", fixture("me.jpeg"))
a_post("/1/statuses/update_with_media.json", "https://upload.twitter.com").
should have_been_made
end
- it 'returns the media as an entity' do
- tweet = @client.update_with_media("You always have options", fixture("me.jpeg"))
- tweet.media.should be
- end
end
context "a png image" do
it "requests the correct resource" do
@client.update_with_media("You always have options", fixture("we_concept_bg2.png"))
a_post("/1/statuses/update_with_media.json", "https://upload.twitter.com").
should have_been_made
end
- it 'returns the media as an entity' do
- tweet = @client.update_with_media("You always have options", fixture("we_concept_bg2.png"))
- tweet.media.should be
+ end
+ context "a Tempfile" do
+ it "requests the correct resource" do
+ @client.update_with_media("You always have options", Tempfile.new("tmp"))
+ a_post("/1/statuses/update_with_media.json", "https://upload.twitter.com").
+ should have_been_made
end
end
context "an IO" do
it "requests the correct resource" do
- @client.update_with_media("You always have options", {:io => StringIO.new, :type => 'gif'})
+ @client.update_with_media("You always have options", {:io => IO.sysopen(fixture_path + "/we_concept_bg2.png"), :type => 'png'})
a_post("/1/statuses/update_with_media.json", "https://upload.twitter.com").
should have_been_made
end
- it 'returns the media as an entity' do
- tweet = @client.update_with_media("You always have options", {:io => StringIO.new, :type => 'gif'})
- tweet.media.should be
+ end
+ context "a StringIO" do
+ it "requests the correct resource" do
+ @client.update_with_media("You always have options", {:io => StringIO.new, :type => 'png'})
+ a_post("/1/statuses/update_with_media.json", "https://upload.twitter.com").
+ should have_been_made
end
end
end

0 comments on commit 79dc819

Please sign in to comment.