Skip to content

Commit

Permalink
Allow Tempfile to be passed to Twitter::API#update_with_media
Browse files Browse the repository at this point in the history
Closes sferik#306.
  • Loading branch information
sferik committed Aug 26, 2012
1 parent 24f759b commit 79dc819
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/client.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/request/multipart_with_file.rb
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions spec/helper.rb
Expand Up @@ -7,6 +7,8 @@

require 'twitter'
require 'rspec'
require 'stringio'
require 'tempfile'
require 'timecop'
require 'webmock/rspec'

Expand Down
34 changes: 14 additions & 20 deletions spec/twitter/api/statuses_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 79dc819

Please sign in to comment.