Skip to content

Commit

Permalink
Merge pull request #1 from nhocki/master
Browse files Browse the repository at this point in the history
Allow blocks on Faraday connections
  • Loading branch information
markbates committed Feb 6, 2015
2 parents 3f51352 + efdc498 commit 0df637c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
37 changes: 24 additions & 13 deletions lib/faraday_bang/bang.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
module Faraday::Bang
ERROR_CODES = [(400..417).to_a, (500..505).to_a].flatten
SUPPORTED_HTTP_METHODS = Faraday::Connection::METHODS - [ :options ]

Faraday::Connection::METHODS.each do |verb|
define_method("#{verb}!") do |*args|
response = self.send(verb, *args)
if response.status >= 400
err_name = "Response#{response.status}Error"
if Faraday::Bang.const_defined?(err_name)
klass = Faraday::Bang.const_get(err_name)
raise klass.new(response)
else
raise Faraday::Bang::ResponseError.new(response)
end
end
return response
SUPPORTED_HTTP_METHODS.each do |verb|
define_method("#{verb}!") do |*args, &block|
response = self.send(verb, *args, &block)
handle_response(response)
end
end

def options!(*args, &block)
url, body, headers = args
response = run_request(:options, url, body, headers, &block)
handle_response(response)
end

private
def handle_response(response)
if response.status >= 400
err_name = "Response#{response.status}Error"
if Faraday::Bang.const_defined?(err_name)
klass = Faraday::Bang.const_get(err_name)
raise klass.new(response)
else
raise Faraday::Bang::ResponseError.new(response)
end
end
response
end
end

Faraday.extend(Faraday::Bang)
Expand Down
60 changes: 57 additions & 3 deletions test/lib/faraday_bang/bang_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
let(:body) { {foo: 'bar'}.to_json }

[Faraday, Faraday.new].each do |klass|
let(:response) { mock(body: body, status: 200) }

Faraday::Connection::METHODS.each do |verb|
Faraday::Bang::SUPPORTED_HTTP_METHODS.each do |verb|

describe "##{verb}!" do
it "##{verb}! passes a block" do
block = Proc.new do |req|
raise "dummy error #{verb}"
end
error = ->{ klass.send("#{verb}!", &block) }.must_raise RuntimeError
error.message.must_equal "dummy error #{verb}"
end

let(:response) { mock(body: body, status: 200) }
describe "##{verb}!" do

before do
klass.expects(verb).with(url, args).returns(response)
Expand Down Expand Up @@ -56,6 +63,53 @@

end

describe "options!" do
it "passes a block" do
block = Proc.new do |req|
raise "dummy error options"
end
error = ->{ klass.send("options!", &block) }.must_raise RuntimeError
error.message.must_equal "dummy error options"
end

describe 'successful response' do
before do
klass.expects(:run_request).with(:options, url, args, nil).returns(response)
end

it 'passes the call onto Faraday' do
res = klass.send("options!", url, args)
res.body.must_equal body
end
end

describe 'unsuccessful response' do
let(:env) { OpenStruct.new(url: 'http://example.com') }
let(:response) { OpenStruct.new(body: body, status: 506, env: env) }

before do
klass.expects(:run_request).with(:options, url, args, nil).returns(response)
end

it 'raises an error if its a non-200 response without an explicit exception' do
->{klass.send("options!", url, args)}.must_raise Faraday::Bang::ResponseError, body
end

Faraday::Bang::ERROR_CODES.each do |code|

context code do

let(:response) { OpenStruct.new(body: body, status: code, env: env) }

it "raises a Faraday::Bang::Response#{code}Error" do
->{klass.send("options!", url, args)}.must_raise Faraday::Bang.const_get("Response#{code}Error")
end

end
end
end
end

end

end

0 comments on commit 0df637c

Please sign in to comment.