class DuffelAPI::Request

An internal class used within the library that represents a request to be made to the Duffel API, and which is able to dispatch that request

Public Class Methods

new(connection, method, path, headers: {}, params: {}, query_params: {}) click to toggle source

Initialize a request class, which makes calls to the API

@param connection [Faraday] a Faraday connection @param method [Symbol] the HTTP method to make the request with @param path [String] the path to make the request to @param headers [Hash] the HTTP request headers to send with the request @param params [Hash] Any paramters to include in the HTTP body @param query_params [Hash] Any parameters to include in the HTTP querystring

# File lib/duffel_api/request.rb, line 17
def initialize(connection, method, path, headers: {}, params: {}, query_params: {})
  @connection = connection
  @method = method
  @path = path
  @headers = headers.transform_keys(&:to_s)
  @option = params
  @query_params = query_params

  @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

call() click to toggle source

Dispatches the request and returns the response

@return [Response] the response from the request

# File lib/duffel_api/request.rb, line 36
def call
  Response.new(make_request)
end

Private Instance Methods

make_request() click to toggle source

Actually makes the request to the API

@return [Faraday::Response]

# File lib/duffel_api/request.rb, line 45
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_body() click to toggle source

Fetches the appropriate parameters to put into the request body, based on the HTTP method

@return [Hash] the parameters to put into the request body, not yet JSON-encoded

# File lib/duffel_api/request.rb, line 58
def request_body
  if @method == :get
    nil
  elsif %i[post put delete patch].include?(@method)
    @option
  else
    raise "Unknown request method #{@method}"
  end
end
request_query() click to toggle source

Fetches the appropriate parameters to put into the request querystring, based on the HTTP method

@return [Hash] the parameters to put into the request querystring

# File lib/duffel_api/request.rb, line 72
def request_query
  if @method == :get
    @option
  else
    @query_params
  end
end