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 3 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 - Set.new([ :options ])
Copy link
Owner

Choose a reason for hiding this comment

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

This can be more simply written as:

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_error(response)
end
end

define_method("options!") do |*args, &block|
Copy link
Owner

Choose a reason for hiding this comment

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

There's no need to use defined_method here. You can just use def options!.

url, params, headers = args
response = run_request(:options, url, params, headers, &block)
handle_error(response)
end

private
def handle_error(response)
Copy link
Owner

Choose a reason for hiding this comment

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

This should probably be named handle_response as that's what it's actually doing.

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
9 changes: 8 additions & 1 deletion test/lib/faraday_bang/bang_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

[Faraday, Faraday.new].each do |klass|

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.


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

Choose a reason for hiding this comment

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

You should probably check to make sure that you're getting the correct error message back. Otherwise this might be a false positive as something else might be raising a RuntimeError.

end

describe "##{verb}!" do

Expand Down