Class: Fortnox::API::Repository::Base

Inherits:
Object
  • Object
show all
Includes:
Loaders, Savers, Fortnox::API::RequestHandling, HTTParty
Defined in:
lib/fortnox/api/repositories/base.rb

Direct Known Subclasses

Article, Customer, Invoice, Order, Project, TermsOfPayment, Unit

Constant Summary collapse

HTTP_METHODS =
%i[get put post delete].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Savers

#save

Methods included from Loaders

#all, #escape, #find, #find_all_by, #find_one_by, #only, #search, #to_query

Constructor Details

#initialize(keys_filtered_on_save: [:url], token_store: :default) ⇒ Base

Returns a new instance of Base.



46
47
48
49
50
# File 'lib/fortnox/api/repositories/base.rb', line 46

def initialize(keys_filtered_on_save: [:url], token_store: :default)
  @keys_filtered_on_save = keys_filtered_on_save
  @token_store = token_store
  @mapper = Registry[Mapper::Base.canonical_name_sym(self.class::MODEL)].new
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



26
27
28
# File 'lib/fortnox/api/repositories/base.rb', line 26

def headers
  @headers
end

#keys_filtered_on_saveObject (readonly)

Returns the value of attribute keys_filtered_on_save.



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

def keys_filtered_on_save
  @keys_filtered_on_save
end

#mapperObject (readonly)

Returns the value of attribute mapper.



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

def mapper
  @mapper
end

Class Method Details

.set_headers(headers = {}) ⇒ Object

rubocop:disable Naming/AccessorMethodName



29
30
31
# File 'lib/fortnox/api/repositories/base.rb', line 29

def self.set_headers(headers = {}) # rubocop:disable Naming/AccessorMethodName
  self.headers.merge!(headers)
end

Instance Method Details

#access_tokensObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/fortnox/api/repositories/base.rb', line 64

def access_tokens
  begin
    tokens = config.token_store.fetch(@token_store)
  rescue KeyError
    token_store_not_found!(@token_store.inspect)
  end

  check_access_tokens!(tokens)
  tokens
end

#check_access_tokens!(tokens) ⇒ Object



57
58
59
60
61
62
# File 'lib/fortnox/api/repositories/base.rb', line 57

def check_access_tokens!(tokens)
  tokens_present = !(tokens.nil? || tokens.empty?)
  return if tokens_present
  error_message = "You have not provided any access tokens in token store #{@token_store.inspect}."
  raise MissingConfiguration, error_message
end

#next_access_tokenObject



52
53
54
55
# File 'lib/fortnox/api/repositories/base.rb', line 52

def next_access_token
  @access_tokens ||= CircularQueue.new(*access_tokens)
  @access_tokens.next
end