Class: Nova::API::Base

Inherits:
Utils::BaseStruct show all
Includes:
HTTParty
Defined in:
lib/nova/api/base.rb

Constant Summary collapse

SCHEME =
'https'
PRODUCTION_HOST =
'nova.money'
STAGING_HOST =
'staging.nova.money'

Constants inherited from Utils::BaseStruct

Utils::BaseStruct::DATE_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Utils::BaseStruct

#allowed_attributes, generate_valid_value_for, initialize_empty_model_with_id, value_for_field

Class Method Details

.authorization_headerObject



104
105
106
# File 'lib/nova/api/base.rb', line 104

def self.authorization_header
  { 'Persistent-Token': configuration.api_key }.dup
end

.base_urlObject



19
20
21
22
23
24
25
# File 'lib/nova/api/base.rb', line 19

def self.base_url
  raise Nova::API::MissingSubdomainError, 'The subdomain must be informed' if configuration.subdomain.nil? || configuration.subdomain.empty?

  host = configuration.use_staging? ? STAGING_HOST : PRODUCTION_HOST

  "#{SCHEME}://#{configuration.subdomain}.#{host}"
end

.configurationObject



109
110
111
# File 'lib/nova/api/base.rb', line 109

def self.configuration
  Nova::API.configuration
end

.do_get(endpoint, query, object = self) ⇒ Object



39
40
41
42
43
# File 'lib/nova/api/base.rb', line 39

def self.do_get(endpoint, query, object = self)
  response = perform_get(endpoint, query, headers)

  Nova::API::Response.build(response, object)
end

.do_get_search(endpoint, query, object = self) ⇒ Object



33
34
35
36
37
# File 'lib/nova/api/base.rb', line 33

def self.do_get_search(endpoint, query, object = self)
  response = perform_get(endpoint, query, headers)

  Nova::API::ListResponse.build(response, object)
end

.endpointObject



15
16
17
# File 'lib/nova/api/base.rb', line 15

def self.endpoint
  raise EndpointNotConfiguredError, 'Each class must implement its own endpoint'
end

.perform_get(endpoint, query, headers = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nova/api/base.rb', line 91

def self.perform_get(endpoint, query, headers = {})
  set_base_uri

  Kernel.p "[NOVA-API] Issuing GET to #{base_uri}#{endpoint}, headers: #{headers.merge(authorization_header)}" if configuration.debug?

  response =
    if query
      self.get(endpoint, query: query, headers: headers.merge(authorization_header))
    else
      self.get(endpoint, headers: headers.merge(authorization_header))
    end
end

.set_base_uriObject



114
115
116
117
118
119
120
121
122
# File 'lib/nova/api/base.rb', line 114

def self.set_base_uri
  if configuration.debug?
    debug_output $stdout

    Kernel.p "[NOVA-API] Changing base URI from #{base_uri} to #{base_url}"
  end

  default_options[:base_uri] = base_url
end

Instance Method Details

#endpointObject



27
28
29
# File 'lib/nova/api/base.rb', line 27

def endpoint
  raise EndpointNotConfiguredError, 'Each class must implement its own endpoint'
end