Class: Nova::API::Base

Inherits:
Utils::BaseStruct show all
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



92
93
94
# File 'lib/nova/api/base.rb', line 92

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

.base_urlObject



15
16
17
18
19
20
21
# File 'lib/nova/api/base.rb', line 15

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



97
98
99
# File 'lib/nova/api/base.rb', line 97

def self.configuration
  Nova::API.configuration
end

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



36
37
38
39
40
# File 'lib/nova/api/base.rb', line 36

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

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

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



30
31
32
33
34
# File 'lib/nova/api/base.rb', line 30

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

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

.endpointObject



11
12
13
# File 'lib/nova/api/base.rb', line 11

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

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



82
83
84
85
86
87
88
89
90
# File 'lib/nova/api/base.rb', line 82

def self.perform_get(endpoint, query, headers = {})
  Kernel.p "[NOVA-API] Issuing GET to #{base_url}#{endpoint}, headers: #{headers.merge(authorization_header)}" if configuration.debug?

  if query
    HTTParty.get("#{base_url}#{endpoint}", query: query, headers: headers.merge(authorization_header), format: :json)
  else
    HTTParty.get("#{base_url}#{endpoint}", headers: headers.merge(authorization_header), format: :json)
  end
end

Instance Method Details

#endpointObject



24
25
26
# File 'lib/nova/api/base.rb', line 24

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