Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ doc/*
log/*
measurement/*
pkg/*
*.swp
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ cache: bundler
language: ruby

rvm:
- 2.0.0
- 2.1
- 2.2
- 2.3.3
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gem 'jruby-openssl', platforms: :jruby
gem 'rake'
gem 'yard'

group :development do
group :development, :test do
gem 'pry'
end

Expand Down
45 changes: 5 additions & 40 deletions lib/twitter/rest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
require 'json'
require 'openssl'
require 'twitter/error'
require 'twitter/headers'
require 'twitter/rate_limit'
require 'twitter/utils'
require 'twitter/rest/request_options'

module Twitter
module REST
class Request
include Twitter::Utils
include Twitter::REST::RequestOptions
BASE_URL = 'https://api.twitter.com'.freeze
attr_accessor :client, :headers, :options, :path, :rate_limit,
:request_method, :uri
:request_method, :uri, :options_key
alias verb request_method

# @param client [Twitter::Client]
Expand All @@ -25,57 +26,21 @@ class Request
def initialize(client, request_method, path, options = {})
@client = client
@uri = Addressable::URI.parse(path.start_with?('http') ? path : BASE_URL + path)
set_multipart_options!(request_method, options)
create_request_options!(request_method, options)
@path = uri.path
@options = options
end

# @return [Array, Hash]
def perform
options_key = @request_method == :get ? :params : :form
response = http_client.headers(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
response = http_client.headers(@headers).public_send(@request_method, @uri.to_s, @options_key => @options)
response_body = response.body.empty? ? '' : symbolize_keys!(response.parse)
response_headers = response.headers
fail_or_return_response_body(response.code, response_body, response_headers)
end

private

def merge_multipart_file!(options)
key = options.delete(:key)
file = options.delete(:file)

options[key] = if file.is_a?(StringIO)
HTTP::FormData::File.new(file, mime_type: 'video/mp4')
else
HTTP::FormData::File.new(file, filename: File.basename(file), mime_type: mime_type(File.basename(file)))
end
end

def set_multipart_options!(request_method, options)
if request_method == :multipart_post
merge_multipart_file!(options)
@request_method = :post
@headers = Twitter::Headers.new(@client, @request_method, @uri).request_headers
else
@request_method = request_method
@headers = Twitter::Headers.new(@client, @request_method, @uri, options).request_headers
end
end

def mime_type(basename)
case basename
when /\.gif$/i
'image/gif'
when /\.jpe?g/i
'image/jpeg'
when /\.png$/i
'image/png'
else
'application/octet-stream'
end
end

def fail_or_return_response_body(code, body, headers)
error = error(code, body, headers)
raise(error) if error
Expand Down
67 changes: 67 additions & 0 deletions lib/twitter/rest/request_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'twitter/headers'

module Twitter
module REST
module RequestOptions
# Set the request method, content type, and headers on the client
#
# @param request_method [Symbol] Desired request method
# @param options [Hash] the content body
# @return [void]
def create_request_options!(request_method, options)
if request_method == :multipart_post
create_multipart_options!(options)
elsif request_method == :json_post
create_json_options!
else
create_standard_options!(request_method, options)
end
end

private

def merge_multipart_file!(options)
key = options.delete(:key)
file = options.delete(:file)

options[key] = if file.is_a?(StringIO)
HTTP::FormData::File.new(file, mime_type: 'video/mp4')
else
HTTP::FormData::File.new(file, filename: File.basename(file), mime_type: mime_type(File.basename(file)))
end
end

def create_multipart_options!(options)
merge_multipart_file!(options)
@request_method = :post
@options_key = :form
@headers = Twitter::Headers.new(@client, @request_method, @uri).request_headers
end

def create_json_options!
@options_key = :json
@request_method = :post
@headers = Twitter::Headers.new(@client, @request_method, @uri).request_headers
end

def create_standard_options!(request_method, options)
@request_method = request_method
@options_key = @request_method == :get ? :params : :form
@headers = Twitter::Headers.new(@client, @request_method, @uri, options).request_headers
end

def mime_type(basename)
case basename
when /\.gif$/i
'image/gif'
when /\.jpe?g/i
'image/jpeg'
when /\.png$/i
'image/png'
else
'application/octet-stream'
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'tempfile'
require 'timecop'
require 'webmock/rspec'
require 'pry'

require_relative 'support/media_object_examples'

Expand Down
16 changes: 16 additions & 0 deletions spec/twitter/rest/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Update', media_ids: '470030289822314497'})).to have_been_made
end

context 'when posting JSON' do
it 'makes a proper json encoded request' do
stub_post('/1.1/json-post-endpoint.json')
.with(
body: {test_key: 'test value'},
headers: {content_type: 'application/json; charset=UTF-8'}
)
.to_return(
body: '{"success":true}',
headers: {content_type: 'application/json; charset=utf-8'}
)
Twitter::REST::Request.new(@client, :json_post, '/1.1/json-post-endpoint.json', test_key: 'test value').perform
expect(a_post('/1.1/json-post-endpoint.json').with(body: {test_key: 'test value'})).to have_been_made
end
end

context 'when using a proxy' do
before do
@client = Twitter::REST::Client.new(consumer_key: 'CK', consumer_secret: 'CS', access_token: 'AT', access_token_secret: 'AS', proxy: {host: '127.0.0.1', port: 3328})
Expand Down
2 changes: 1 addition & 1 deletion twitter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
spec.licenses = %w[MIT]
spec.name = 'twitter'
spec.require_paths = %w[lib]
spec.required_ruby_version = '>= 1.9.3'
spec.required_ruby_version = '>= 2.1.0'
spec.summary = spec.description
spec.version = Twitter::Version
end