Class: Starling::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/starling/request.rb

Overview

The interface between Starling and Faraday, which is used under the hood to make HTTP requests

Instance Method Summary collapse

Constructor Details

#initialize(connection, method, path, params: {}, headers: {}) ⇒ Request

Returns a new instance of Request.

Parameters:

  • connection (Faraday)

    A Faraday connection

  • method (Symbol)

    The HTTP method for the request

  • path (String)

    The path of the API endpoint, which will be added to the base URL (from Client::ENVIRONMENT_BASE_URLS) and the API version-specific base path (ApiService::BASE_PATH)

  • params (Hash) (defaults to: {})

    The parameters which will be included in the request, either in the URL or the body, depending on the method

  • headers (Hash) (defaults to: {})

    The headers to be included in the request



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/starling/request.rb', line 13

def initialize(connection, method, path, params: {}, headers: {})
  @connection = connection
  @method = method
  @path = path
  @headers = headers
  @request_body = params if %i[post put delete].include?(method)
  @request_query = method == :get ? params : {}

  return unless @request_body.is_a?(Hash)
  @request_body = @request_body.to_json
  @headers['Content-Type'] ||= 'application/json'
end

Instance Method Details

#make_requestFaraday::Request

Dispatch the configured HTTP request

Returns:

  • (Faraday::Request)

    The response from the HTTP request



29
30
31
32
33
34
35
36
# File 'lib/starling/request.rb', line 29

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