Class: Fortnox::Resource

Inherits:
RestEasy::Resource
  • Object
show all
Extended by:
ErrorTranslation
Includes:
ErrorTranslation, Fortnox::RailsSerialisation::ResourceMethods, Serialisation::ResourceJSON, Types
Defined in:
lib/fortnox/resource.rb

Overview

TODO: this class is still over the length limit even with error translation extracted — see todo.md. rubocop:disable Metrics/ClassLength

Direct Known Subclasses

Article, Customer, Document, Label, Project, TermsOfPayment, Unit

Constant Summary

Constants included from Types

Types::AccountNumber, Types::AccountingMethods, Types::ArticleTypes, Types::BLANK_TO_NIL, Types::CURRENT_HOUSEWORK_TYPES, Types::CoercibleBool, Types::Currencies, Types::CustomerTypes, Types::DefaultDeliveryTypeValues, Types::DefaultInvoiceDeliveryTypeValues, Types::DeliveryStates, Types::DiscountTypes, Types::Email, Types::HouseworkTypes, Types::InvoiceTypes, Types::LEGACY_HOUSEWORK_TYPES, Types::PaymentWays, Types::ProjectStatusTypes, Types::TaxReductionTypes, Types::UnsizedFloat, Types::UnsizedInteger, Types::UnsizedString, Types::VATTypes

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fortnox::RailsSerialisation::ResourceMethods

#as_json

Methods included from Serialisation::ResourceJSON

#to_json

Class Attribute Details

.registered_resourcesObject (readonly)

Returns the value of attribute registered_resources.



37
38
39
# File 'lib/fortnox/resource.rb', line 37

def registered_resources
  @registered_resources
end

Class Method Details

.find(id_or_hash) ⇒ Object



81
82
83
84
85
# File 'lib/fortnox/resource.rb', line 81

def find(id_or_hash)
  return find_all_by(id_or_hash) if id_or_hash.is_a? Hash

  find_one_by(id_or_hash)
end

.find_all_by(hash) ⇒ Object



92
93
94
95
# File 'lib/fortnox/resource.rb', line 92

def find_all_by(hash)
  response = get(path: config.path.to_s, params: hash)
  parse(response)
end

.find_one_by(id) ⇒ Object



87
88
89
90
# File 'lib/fortnox/resource.rb', line 87

def find_one_by(id)
  response = get(path: "#{config.path}/#{id}")
  parse(response)
end

.inherited(subclass) ⇒ Object



39
40
41
42
# File 'lib/fortnox/resource.rb', line 39

def inherited(subclass)
  super
  Fortnox::Resource.registered_resources << subclass
end

.new(model_data = {}, **kwargs) ⇒ Object



52
53
54
# File 'lib/fortnox/resource.rb', line 52

def new(model_data = {}, **kwargs)
  with_translated_errors { super(checked_attributes(model_data.merge(kwargs))) }
end

.only(filter) ⇒ Object



70
71
72
73
# File 'lib/fortnox/resource.rb', line 70

def only(filter)
  response = get(path: config.path, params: { filter: })
  parse(response)
end

.parse(response) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/fortnox/resource.rb', line 44

def parse(response)
  with_translated_errors do
    pagination = extract_pagination(response)
    result = super
    result.is_a?(Array) ? Collection.new(result, **pagination) : result
  end
end

.save(instance) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/fortnox/resource.rb', line 60

def save(instance)
  # A persisted record with no recorded changes has nothing to write.
  # Without this short-circuit rest-easy would PUT the entire record
  # back, re-sending every untouched attribute and risking clobbering
  # changes made elsewhere since it was loaded.
  return instance if !instance.meta.new? && instance.__changes__.empty?

  with_translated_errors { super }
end

.search(hash) ⇒ Object



75
76
77
78
79
# File 'lib/fortnox/resource.rb', line 75

def search(hash)
  attribute, value = hash.first
  response = get(path: config.path, params: { attribute => value })
  parse(response)
end

.stub(**model_data) ⇒ Object



56
57
58
# File 'lib/fortnox/resource.rb', line 56

def stub(**model_data)
  with_translated_errors { super(**checked_attributes(model_data)) }
end

Instance Method Details

#serialiseObject



131
132
133
# File 'lib/fortnox/resource.rb', line 131

def serialise(...)
  with_translated_errors { super }
end

#update(changes = {}, **kwargs) ⇒ Object



127
128
129
# File 'lib/fortnox/resource.rb', line 127

def update(changes = {}, **kwargs)
  with_translated_errors { super(self.class.send(:checked_attributes, changes.merge(kwargs))) }
end