From 73e2b9be19acf1403f324e5be48a550d3756d822 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Thu, 6 Nov 2014 17:11:04 +0100 Subject: [PATCH] Rename URL_PREFIX to BASE_URL --- lib/twitter/rest/client.rb | 4 ++-- lib/twitter/rest/media.rb | 6 +++--- lib/twitter/rest/request.rb | 8 +++++--- spec/helper.rb | 16 ++++++++-------- spec/twitter/headers_spec.rb | 6 +++--- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/twitter/rest/client.rb b/lib/twitter/rest/client.rb index 495c47ba4..208852202 100644 --- a/lib/twitter/rest/client.rb +++ b/lib/twitter/rest/client.rb @@ -12,7 +12,7 @@ module Twitter module REST class Client < Twitter::Client include Twitter::REST::API - URL_PREFIX = 'https://api.twitter.com' + BASE_URL = 'https://api.twitter.com' attr_accessor :bearer_token # @param connection_options [Hash] @@ -92,7 +92,7 @@ def credentials? # # @return [Faraday::Connection] def connection - @connection ||= Faraday.new(URL_PREFIX, connection_options) + @connection ||= Faraday.new(BASE_URL, connection_options) end end end diff --git a/lib/twitter/rest/media.rb b/lib/twitter/rest/media.rb index ef2aaf9f1..f9ea0fa0e 100644 --- a/lib/twitter/rest/media.rb +++ b/lib/twitter/rest/media.rb @@ -17,11 +17,11 @@ module Media # @param options [Hash] A customizable set of options. def upload(media, options = {}) fail(Twitter::Error::UnacceptableIO.new) unless media.respond_to?(:to_io) - url_prefix = 'https://upload.twitter.com' + base_url = 'https://upload.twitter.com' path = '/1.1/media/upload.json' conn = connection.dup - conn.url_prefix = url_prefix - headers = Twitter::Headers.new(self, :post, url_prefix + path, options).request_headers + conn.url_prefix = base_url + headers = Twitter::Headers.new(self, :post, base_url + path, options).request_headers options.merge!(:media => media) conn.post(path, options) { |request| request.headers.update(headers) }.env.body[:media_id] end diff --git a/lib/twitter/rest/request.rb b/lib/twitter/rest/request.rb index b7584ec48..a5452defb 100644 --- a/lib/twitter/rest/request.rb +++ b/lib/twitter/rest/request.rb @@ -1,3 +1,4 @@ +require 'addressable/uri' require 'faraday' require 'json' require 'timeout' @@ -8,9 +9,9 @@ module Twitter module REST class Request - attr_accessor :client, :headers, :rate_limit, :request_method, :path, :options + attr_accessor :client, :headers, :options, :rate_limit, :request_method, + :path, :uri alias_method :verb, :request_method - URL_PREFIX = 'https://api.twitter.com' # @param client [Twitter::Client] # @param request_method [String, Symbol] @@ -21,12 +22,13 @@ def initialize(client, request_method, path, options = {}) @client = client @request_method = request_method.to_sym @path = path + @uri = Addressable::URI.parse(client.connection.url_prefix + path) @options = options end # @return [Array, Hash] def perform - @headers = Twitter::Headers.new(@client, @request_method, URL_PREFIX + @path, @options).request_headers + @headers = Twitter::Headers.new(@client, @request_method, @uri.to_s, @options).request_headers begin response = @client.connection.send(@request_method, @path, @options) { |request| request.headers.update(@headers) }.env rescue Faraday::Error::TimeoutError, Timeout::Error => error diff --git a/spec/helper.rb b/spec/helper.rb index 85b532fe1..3a5561577 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -26,35 +26,35 @@ end def a_delete(path) - a_request(:delete, Twitter::REST::Request::URL_PREFIX + path) + a_request(:delete, Twitter::REST::Client::BASE_URL + path) end def a_get(path) - a_request(:get, Twitter::REST::Request::URL_PREFIX + path) + a_request(:get, Twitter::REST::Client::BASE_URL + path) end def a_post(path) - a_request(:post, Twitter::REST::Request::URL_PREFIX + path) + a_request(:post, Twitter::REST::Client::BASE_URL + path) end def a_put(path) - a_request(:put, Twitter::REST::Request::URL_PREFIX + path) + a_request(:put, Twitter::REST::Client::BASE_URL + path) end def stub_delete(path) - stub_request(:delete, Twitter::REST::Request::URL_PREFIX + path) + stub_request(:delete, Twitter::REST::Client::BASE_URL + path) end def stub_get(path) - stub_request(:get, Twitter::REST::Request::URL_PREFIX + path) + stub_request(:get, Twitter::REST::Client::BASE_URL + path) end def stub_post(path) - stub_request(:post, Twitter::REST::Request::URL_PREFIX + path) + stub_request(:post, Twitter::REST::Client::BASE_URL + path) end def stub_put(path) - stub_request(:put, Twitter::REST::Request::URL_PREFIX + path) + stub_request(:put, Twitter::REST::Client::BASE_URL + path) end def fixture_path diff --git a/spec/twitter/headers_spec.rb b/spec/twitter/headers_spec.rb index 025ece01c..a340603c8 100644 --- a/spec/twitter/headers_spec.rb +++ b/spec/twitter/headers_spec.rb @@ -4,7 +4,7 @@ before do @client = Twitter::REST::Client.new(:consumer_key => 'CK', :consumer_secret => 'CS', :access_token => 'AT', :access_token_secret => 'AS') - @headers = Twitter::Headers.new(@client, :get, Twitter::REST::Request::URL_PREFIX + '/path') + @headers = Twitter::Headers.new(@client, :get, Twitter::REST::Client::BASE_URL + '/path') end describe '#oauth_auth_header' do @@ -40,14 +40,14 @@ describe '#bearer_auth_header' do it 'creates the correct auth headers with supplied bearer token' do client = Twitter::REST::Client.new(:bearer_token => 'BT') - headers = Twitter::Headers.new(client, :get, Twitter::REST::Request::URL_PREFIX + '/path') + headers = Twitter::Headers.new(client, :get, Twitter::REST::Client::BASE_URL + '/path') authorization = headers.send(:bearer_auth_header) expect(authorization).to eq('Bearer BT') end it 'creates the correct auth headers with supplied Twitter::Token' do token = Twitter::Token.new(:token_type => 'bearer', :access_token => 'BT') client = Twitter::REST::Client.new(:bearer_token => token) - headers = Twitter::Headers.new(client, :get, Twitter::REST::Request::URL_PREFIX + '/path') + headers = Twitter::Headers.new(client, :get, Twitter::REST::Client::BASE_URL + '/path') authorization = headers.send(:bearer_auth_header) expect(authorization).to eq('Bearer BT') end