Skip to content

Commit

Permalink
Add support for options! method.
Browse files Browse the repository at this point in the history
Since Faraday doesn't support the `options` request as a first class
HTTP method (see lostisland/faraday#305) we need to use the `run_request`
method instead of the `options` one.
  • Loading branch information
nhocki committed Feb 5, 2015
1 parent a3c5ffd commit 1980bd0
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 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 ])

Faraday::Connection::METHODS.each do |verb|
SUPPORTED_HTTP_METHODS.each do |verb|
define_method("#{verb}!") do |*args, &block|
response = self.send(verb, *args, &block)
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
handle_error(response)
end
end

define_method("options!") do |*args, &block|
url, params, headers = args
response = run_request(:options, url, params, headers, &block)
handle_error(response)
end

private
def handle_error(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

0 comments on commit 1980bd0

Please sign in to comment.