-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'twitter/error' | ||
require 'twitter/rest/utils' | ||
|
||
module Twitter | ||
module REST | ||
module Media | ||
# Uploads media to attach to a tweet | ||
# | ||
# @see https://dev.twitter.com/docs/api/multiple-media-extended-entities | ||
# @rate_limited No | ||
# @authentication Requires user context | ||
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. | ||
# @raise [Twitter::Error::UnacceptableIO] Error when the IO object for the media argument does not have a to_io method. | ||
# @return [Integer] The uploaded media ID. | ||
# @param media [File, Hash] A File object with your picture (PNG, JPEG or GIF) | ||
# @param options [Hash] A customizable set of options. | ||
# @option options [Boolean, String, Integer] :possibly_sensitive Set to true for content which may not be suitable for every audience. | ||
def upload(media, options = {}) | ||
fail(Twitter::Error::UnacceptableIO.new) unless media.respond_to?(:to_io) | ||
url_prefix = 'https://upload.twitter.com' | ||
path = '/1.1/media/upload.json' | ||
conn = connection.dup | ||
conn.url_prefix = url_prefix | ||
headers = request_headers(:post, url_prefix + path, options) | ||
options.merge!(:media => media) | ||
conn.post(path, options) { |request| request.headers.update(headers) }.env.body[:media_id] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"image":{"w":428,"h":428,"image_type":"image\/png"},"media_id":470030289822314497,"media_id_string":"470030289822314497","size":68900} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'helper' | ||
|
||
describe Twitter::REST::Media do | ||
before do | ||
@client = Twitter::REST::Client.new(:consumer_key => 'CK', :consumer_secret => 'CS', :access_token => 'AT', :access_token_secret => 'AS') | ||
end | ||
|
||
describe '#upload' do | ||
before do | ||
stub_request(:post, 'https://upload.twitter.com/1.1/media/upload.json').to_return(:body => fixture('upload.json'), :headers => {:content_type => 'application/json; charset=utf-8'}) | ||
end | ||
context 'a gif image' do | ||
it 'requests the correct resource' do | ||
@client.upload(fixture('pbjt.gif')) | ||
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made | ||
end | ||
it 'returns an Integer' do | ||
media_id = @client.upload(fixture('pbjt.gif')) | ||
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made | ||
expect(media_id).to be_an Integer | ||
expect(media_id).to eq(470_030_289_822_314_497) | ||
end | ||
end | ||
context 'a jpe image' do | ||
it 'requests the correct resource' do | ||
@client.upload(fixture('wildcomet2.jpe')) | ||
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made | ||
end | ||
end | ||
context 'a jpeg image' do | ||
it 'requests the correct resource' do | ||
@client.upload(fixture('me.jpeg')) | ||
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made | ||
end | ||
end | ||
context 'a png image' do | ||
it 'requests the correct resource' do | ||
@client.upload(fixture('we_concept_bg2.png')) | ||
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made | ||
end | ||
end | ||
context 'a Tempfile' do | ||
it 'requests the correct resource' do | ||
@client.upload(Tempfile.new('tmp')) | ||
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made | ||
end | ||
end | ||
context 'A non IO object' do | ||
it 'raises an error' do | ||
expect { @client.upload('Unacceptable IO') }.to raise_error(Twitter::Error::UnacceptableIO) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters