Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Unifying the request method of Twitter::Client

- The request method is using a helper method request_setup that returns
  a Proc which sets up the correct headers for the current request.
- Each type of different authentication header is being returned by a
  separate private method and they all have tests.
  • Loading branch information...
commit 15baf20f4fd96801a3ded6851f4811ad9d4114b1 1 parent 8c7d59f
@paracycle authored
View
2  lib/twitter/api/oauth.rb
@@ -21,7 +21,7 @@ module OAuth
# client = Twitter::Client.new :consumer_key => "abc", :consumer_secret => 'def'
# bearer_token = client.token
def token
- object_from_response(Twitter::Token, :bearer_request, "/oauth2/token", :grant_type => "client_credentials")
+ object_from_response(Twitter::Token, :post, "/oauth2/token", :grant_type => "client_credentials", :bearer_token_request => true)
end
# Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials.
View
63 lib/twitter/client.rb
@@ -78,23 +78,34 @@ def put(path, params={})
end
private
-
- def request(method, path, params={}, signature_params=params)
- connection.send(method.to_sym, path, params) do |request|
- request.headers[:authorization] = auth_header(method.to_sym, path, signature_params).to_s
- end.env
- rescue Faraday::Error::ClientError
- raise Twitter::Error::ClientError
- rescue MultiJson::DecodeError
- raise Twitter::Error::DecodeError
+ # Returns a proc that can be used to setup the Faraday::Request headers
+ #
+ # @param method [Symbol]
+ # @param path [String]
+ # @param params [Hash]
+ # @return [Proc]
+ def request_setup(method, path, params)
+ if params.delete :bearer_token_request
+ Proc.new do |request|
+ request.headers[:authorization] = bearer_token_credentials_auth_header
+ request.headers[:content_type] = 'application/x-www-form-urlencoded; charset=UTF-8'
+ request.headers[:accept] = '*/*' # It is important we set this, otherwise we get an error.
+ end
+ elsif application_only_auth?
+ Proc.new do |request|
+ request.headers[:authorization] = bearer_auth_header
+ end
+ else
+ Proc.new do |request|
+ uri = URI(@endpoint + path)
+ request.headers[:authorization] = oauth_auth_header(method, path, params).to_s
+ end
+ end
end
- def bearer_request(path, params={})
- connection.send(:post, path, params) do |request|
- request.headers[:authorization] = bearer_token_credentials_auth_header
- request.headers[:content_type] = "application/x-www-form-urlencoded; charset=UTF-8"
- request.headers[:accept] = "*/*"
- end.env
+ def request(method, path, params={}, signature_params=params)
+ request_setup = request_setup(method, path, params)
+ connection.send(method.to_sym, path, params, &request_setup).env
rescue Faraday::Error::ClientError
raise Twitter::Error::ClientError
rescue MultiJson::DecodeError
@@ -116,25 +127,17 @@ def connection
#
# @return [String]
def bearer_token_credentials_auth_header
- credentials = Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}")
- "Basic #{credentials}"
+ basic_auth_token = Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}")
+ "Basic #{basic_auth_token}"
end
- # Generates authentication header for when the :bearer_token is supplied
- #
- # @return [String]
- def bearer_token_auth_header
+ def bearer_auth_header
"Bearer #{@bearer_token}"
end
- def auth_header(method, path, params={})
- if application_only_auth?
- bearer_token_auth_header
- else
- uri = URI(@endpoint + path)
- SimpleOAuth::Header.new(method, uri, params, credentials)
- end
+ def oauth_auth_header(method, path, params={})
+ uri = URI(@endpoint + path)
+ SimpleOAuth::Header.new(method, uri, params, credentials)
end
-
- end
+end
end
View
15 spec/twitter/client_spec.rb
@@ -140,10 +140,10 @@
end
end
- describe "#auth_header" do
+ describe "#oauth_auth_header" do
it "creates the correct auth headers" do
uri = "/1.1/direct_messages.json"
- authorization = subject.send(:auth_header, :get, uri)
+ authorization = subject.send(:oauth_auth_header, :get, uri)
expect(authorization.options[:signature_method]).to eq "HMAC-SHA1"
expect(authorization.options[:version]).to eq "1.0"
expect(authorization.options[:consumer_key]).to eq "CK"
@@ -153,16 +153,23 @@
end
end
- describe "#auth_header" do
+ describe "#bearer_auth_header" do
subject do
Twitter::Client.new(:bearer_token => "BT")
end
it "creates the correct auth headers with supplied bearer_token" do
uri = "/1.1/direct_messages.json"
- authorization = subject.send(:auth_header, :get, uri)
+ authorization = subject.send(:bearer_auth_header)
expect(authorization).to eq "Bearer BT"
end
end
+ describe "#bearer_token_credentials_auth_header" do
+ it "creates the correct auth header with supplied consumer_key and consumer_secret" do
+ uri = "/1.1/direct_messages.json"
+ authorization = subject.send(:bearer_token_credentials_auth_header)
+ expect(authorization).to eq "Basic #{Base64.strict_encode64("CK:CS")}"
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.