Skip to content

Commit

Permalink
Refactor Request class
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Nov 20, 2012
1 parent 5fe1da8 commit 2d70b64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
52 changes: 16 additions & 36 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/twitter/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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>"))
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d70b64

Please sign in to comment.