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

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json',
  'Accept' => 'application/json',
}.freeze
HTTP_METHODS =
[ :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.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fortnox/api/repositories/base.rb', line 40

def initialize( keys_filtered_on_save: [ :url ], token_store: :default )
  self.class.base_uri( get_base_url )

  self.headers = DEFAULT_HEADERS.merge({
    'Client-Secret' => get_client_secret,
  })

  @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.



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

def headers
  @headers
end

#keys_filtered_on_saveObject (readonly)

Returns the value of attribute keys_filtered_on_save.



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

def keys_filtered_on_save
  @keys_filtered_on_save
end

#mapperObject (readonly)

Returns the value of attribute mapper.



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

def mapper
  @mapper
end

Class Method Details

.set_headers(headers = {}) ⇒ Object



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

def self.set_headers( headers = {} )
  self.headers.merge!( headers )
end

Instance Method Details

#check_access_tokens!(tokens) ⇒ Object



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

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

#get_access_tokensObject



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

def get_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

#next_access_tokenObject



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

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