Permalink
Browse files

Refactor Request class

  • Loading branch information...
1 parent 5fe1da8 commit 2d70b64674bdc204c85c47327afa571f9641e545 @sferik committed Nov 20, 2012
Showing with 21 additions and 41 deletions.
  1. +16 −36 lib/twitter/client.rb
  2. +3 −3 spec/twitter/client_spec.rb
  3. +2 −2 spec/twitter/error_spec.rb
View
@@ -67,57 +67,37 @@ def get(path, params={})
# Perform an HTTP POST request
def post(path, params={})
- request(:post, path, params)
+ signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params
+ request(:post, path, params, signature_params)
end
- # Perform an HTTP UPDATE request
+ # Perform an HTTP PUT request
def put(path, params={})
request(:put, path, params)
end
private
- # Returns a Faraday::Connection object
- #
- # @return [Faraday::Connection]
- def connection
- @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware))
- end
-
- # Perform an HTTP request
- #
- # @raise [Twitter::Error::ClientError, Twitter::Error::DecodeError]
- def request(method, path, params={})
- uri = URI(@endpoint) unless uri.respond_to?(:host)
- uri += path
- request_headers = {}
- if credentials?
- authorization = auth_header(method, uri, params)
- request_headers[:authorization] = authorization.to_s
- end
- connection.url_prefix = @endpoint
- response = connection.run_request(method.to_sym, path, nil, request_headers) do |request|
- unless params.empty?
- case request.method
- when :post, :put
- request.body = params
- else
- request.params.update(params)
- end
- end
- yield request if block_given?
+ 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
- response
rescue Faraday::Error::ClientError
raise Twitter::Error::ClientError
rescue MultiJson::DecodeError
raise Twitter::Error::DecodeError
end
- 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.respond_to?(:to_io)} ? {} : params
- SimpleOAuth::Header.new(method, uri, signature_params, credentials)
+ # Returns a Faraday::Connection object
+ #
+ # @return [Faraday::Connection]
+ def connection
+ @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware))
+ end
+
+ def auth_header(method, path, params={})
+ uri = URI(@endpoint + path)
+ SimpleOAuth::Header.new(method, uri, params, credentials)
end
end
@@ -132,8 +132,8 @@
expect(a_post("/1.1/statuses/update_with_media.json")).to have_been_made
end
it "catches Faraday errors" do
- subject.stub!(:connection).and_raise(Faraday::Error::ClientError.new("Oups"))
- expect{subject.request(:get, "/path")}.to raise_error(Twitter::Error::ClientError, "Oups")
+ subject.stub!(:connection).and_raise(Faraday::Error::ClientError.new("Oops"))
+ expect{subject.request(:get, "/path")}.to raise_error(Twitter::Error::ClientError, "Oops")
end
it "catches MultiJson::DecodeError errors" do
subject.stub!(:connection).and_raise(MultiJson::DecodeError.new("unexpected token", [], "<!DOCTYPE html>"))
@@ -143,7 +143,7 @@
describe "#auth_header" do
it "creates the correct auth headers" do
- uri = URI("https://api.twitter.com/1.1/direct_messages.json")
+ uri = "/1.1/direct_messages.json"
authorization = subject.auth_header(:get, uri)
expect(authorization.options[:signature_method]).to eq "HMAC-SHA1"
expect(authorization.options[:version]).to eq "1.0"
@@ -5,12 +5,12 @@
describe "#initialize" do
it "wraps another error class" do
begin
- raise Faraday::Error::ClientError.new("Oups")
+ raise Faraday::Error::ClientError.new("Oops")
rescue Faraday::Error::ClientError
begin
raise Twitter::Error
rescue Twitter::Error => error
- expect(error.message).to eq "Oups"
+ expect(error.message).to eq "Oops"
expect(error.wrapped_exception.class).to eq Faraday::Error::ClientError
end
end

0 comments on commit 2d70b64

Please sign in to comment.