class GoCardless::Request
A class that wraps an API request
Public Class Methods
new(connection, method, path, options, headers)
click to toggle source
Initialize a request class, which makes calls to the API @param connection @param method [Symbol] the method to make the request with @param path [String] the path to make the request to @param options [hash] options for the request @param headers [hash] headers to send with the request
# File lib/gocardless-pro/request.rb, line 10 def initialize(connection, method, path, options, headers) @connection = connection @method = method @path = path @headers = headers || {} @envelope_name = options.delete(:envelope_key) @given_options = options @request_body = request_body if @request_body.is_a?(Hash) @request_body = @request_body.to_json @headers['Content-Type'] ||= 'application/json' end end
Public Instance Methods
make_request()
click to toggle source
Make the API request
# File lib/gocardless-pro/request.rb, line 32 def make_request @connection.send(@method) do |request| request.url @path request.body = @request_body request.params = request_query request.headers.merge!(@headers) end end
request()
click to toggle source
Make the request and wrap it in a Response
object
# File lib/gocardless-pro/request.rb, line 27 def request Response.new(make_request) end
request_body()
click to toggle source
Fetch the body to send with the request
# File lib/gocardless-pro/request.rb, line 42 def request_body if @method == :get nil elsif @method == :post || @method == :put @given_options else fail "unknown method #{@method}" end end
request_query()
click to toggle source
Get the query params to send with the request
# File lib/gocardless-pro/request.rb, line 53 def request_query if @method == :get @given_options elsif @method == :post || @method == :put {} else fail "unknown method #{@method}" end end
Private Instance Methods
options()
click to toggle source
# File lib/gocardless-pro/request.rb, line 65 def options { headers: @headers, body: @body, query: @query } end