class Nova::API::Base

Constants

HOST
SCHEME

Public Class Methods

base_url() click to toggle source
# 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?

  "#{SCHEME}://#{configuration.subdomain}.#{HOST}"
end
endpoint() click to toggle source
# File lib/nova/api/base.rb, line 15
def self.endpoint
  raise EndpointNotConfiguredError, 'Each class must implement its own endpoint'
end

Protected Class Methods

do_get(endpoint, query, object = self) click to toggle source
# File lib/nova/api/base.rb, line 47
def self.do_get(endpoint, query, object = self)
  response = perform_get(endpoint, query, headers)

  Nova::API::Response.build(response, object)
end
initialize_empty_model_with_id(klass, id, additional_attributes = {}) click to toggle source
# File lib/nova/api/base.rb, line 31
def self.initialize_empty_model_with_id(klass, id, additional_attributes = {})
  data = klass.schema.type.keys.map do |field|
    name = field.name

    value_for_field(name, additional_attributes[name], field)
  end

  klass.new(Hash[*data.flatten].merge(id: id))
end

Private Class Methods

authorization_header() click to toggle source
# File lib/nova/api/base.rb, line 112
def self.authorization_header
  { 'Persistent-Token': configuration.api_key }
end
configuration() click to toggle source
# File lib/nova/api/base.rb, line 117
def self.configuration
  Nova::API.configuration
end
generate_valid_value_for(type) click to toggle source
# File lib/nova/api/base.rb, line 127
def self.generate_valid_value_for(type)
  case type.name
  when Dry::Types['integer'].name, Dry::Types['float'].name, Dry::Types['decimal'].name
    0
  when Dry::Types['bool'].name
    false
  else
    nil
  end
end
perform_get(endpoint, query, headers = {}) click to toggle source
# File lib/nova/api/base.rb, line 101
def self.perform_get(endpoint, query, headers = {})
  set_base_uri

  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_uri() click to toggle source
# File lib/nova/api/base.rb, line 122
def self.set_base_uri
  base_uri base_url
end
value_for_field(name, override_value, field) click to toggle source
# File lib/nova/api/base.rb, line 93
def self.value_for_field(name, override_value, field)
  return [name, override_value] if override_value
    
  type = field.type

  type.optional? ? [name, nil] :  [name, generate_valid_value_for(type)]
end

Public Instance Methods

endpoint() click to toggle source
# File lib/nova/api/base.rb, line 25
def endpoint
  raise EndpointNotConfiguredError, 'Each class must implement its own endpoint'
end

Protected Instance Methods

do_delete(endpoint) click to toggle source
# File lib/nova/api/base.rb, line 54
def do_delete(endpoint)
  set_base_uri

  response = self.class.delete(endpoint, headers: authorization_header)

  Nova::API::Response.build(response)
end
do_patch(endpoint, data) click to toggle source
# File lib/nova/api/base.rb, line 62
def do_patch(endpoint, data)
  set_base_uri

  if data.nil? || data.empty?
    response = self.class.patch(endpoint, headers: authorization_header)

    Nova::API::Response.build(response)
  else
    payload = data.dup
    payload.delete(:id)

    response = self.class.patch(endpoint, body: payload, headers: authorization_header)

    Nova::API::Response.build(response, self)
  end
end
do_post(endpoint, data) click to toggle source
# File lib/nova/api/base.rb, line 79
def do_post(endpoint, data)
  set_base_uri

  response = self.class.post(endpoint, body: data, headers: authorization_header)

  Nova::API::Response.build(response, self)
end
protect_operation_from_missing_value(attribute = :id) click to toggle source
# File lib/nova/api/base.rb, line 87
def protect_operation_from_missing_value(attribute = :id)
  raise Nova::API::MissingIdError, 'This operation requires an ID to be set' if send(attribute).nil?
end