-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 3 commits
a3c5ffd
1980bd0
0487f3f
ed73b9e
efdc498
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ]) | ||
|
||
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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to use |
||
url, params, headers = args | ||
response = run_request(:options, url, params, headers, &block) | ||
handle_error(response) | ||
end | ||
|
||
private | ||
def handle_error(response) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be named |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,14 @@ | |
|
||
[Faraday, Faraday.new].each do |klass| | ||
|
||
Faraday::Connection::METHODS.each do |verb| | ||
Faraday::Bang::SUPPORTED_HTTP_METHODS.each do |verb| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're only testing supported methods, what about the separate |
||
|
||
it "##{verb}! passes a block" do | ||
block = Proc.new do |req| | ||
raise "dummy error #{verb}" | ||
end | ||
->{ klass.send("#{verb}!", &block) }.must_raise RuntimeError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
end | ||
|
||
describe "##{verb}!" do | ||
|
||
|
There was a problem hiding this comment.
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: