Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow blocks on Faraday connections #1

Merged
merged 5 commits into from
Feb 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're only testing supported methods, what about the separate options! method? There needs to be a test for that.


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