Class: Starling::ApiService

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

Constant Summary collapse

DEFAULT_ADAPTER =
:net_http
BASE_PATH =
'/api/v1'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url, options = {}) ⇒ ApiService

Returns a new instance of ApiService.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/starling/api_service.rb', line 8

def initialize(base_url, options = {})
  @access_token = options.fetch(:access_token)

  http_adapter = options[:http_adapter] || [DEFAULT_ADAPTER]
  connection_options = options[:connection_options]

  @connection = Faraday.new(base_url, connection_options) do |faraday|
    faraday.response(:raise_starling_errors)
    faraday.adapter(*http_adapter)
  end

  user_provided_default_headers = options.fetch(:default_headers, {})
  @headers = default_headers.merge(user_provided_default_headers)
end

Instance Method Details

#make_request(method, path, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/starling/api_service.rb', line 23

def make_request(method, path, options = {})
  raise ArgumentError, 'options must be a hash' unless options.is_a?(Hash)
  options[:headers] ||= {}
  options[:headers] = @headers.merge(options[:headers])

  Request.new(@connection, method, build_path(path), options)
         .make_request
end