Skip to content

Commit

Permalink
Add support for the OPTIONS verb, which requires us to use Faraday's …
Browse files Browse the repository at this point in the history
…run_request() method instead of the dynamically-generated method associated with the verb name (Faraday.options is a different thing - see lostisland/faraday#305)
  • Loading branch information
Ed Jones committed Nov 3, 2017
1 parent 3370e9b commit 4fb108b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions lib/her/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,26 @@ def request(opts={})
path = opts.delete(:_path)
headers = opts.delete(:_headers)
opts.delete_if { |key, value| key.to_s =~ /^_/ } # Remove all internal parameters
response = @connection.send method do |request|
# Faraday doesn't support the OPTIONS verb because of a name collision with an internal options method
# so we need to call run_request directly.
if method == :options
request.headers.merge!(headers) if headers
if method == :get
# For GET requests, treat additional parameters as querystring data
request.url path, opts
else
# For POST, PUT and DELETE requests, treat additional parameters as request body
request.url path
request.body = opts
response = @connection.run_request method, path, opts, headers
else
response = @connection.send method do |request|
request.headers.merge!(headers) if headers
if method == :get
# For GET requests, treat additional parameters as querystring data
request.url path, opts
else
# For POST, PUT and DELETE requests, treat additional parameters as request body
request.url path
request.body = opts
end
end
end

{ :parsed_data => response.env[:body], :response => response }

end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/her/model/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Model
# This module interacts with Her::API to fetch HTTP data
module HTTP
extend ActiveSupport::Concern
METHODS = [:get, :post, :put, :patch, :delete]
METHODS = [:get, :post, :put, :patch, :delete, :options]

# For each HTTP method, define these class methods:
#
Expand Down

0 comments on commit 4fb108b

Please sign in to comment.