Class: Fortnox::Resource

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

Direct Known Subclasses

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

Defined Under Namespace

Modules: ErrorTranslation

Constant Summary

Constants included from Types

Types::AccountNumber, Types::AccountingMethods, Types::ArticleTypes, Types::BLANK_TO_NIL, Types::CURRENT_HOUSEWORK_TYPES, 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::THE_TRUTH, Types::TaxReductionTypes, Types::UnsizedFloat, Types::UnsizedInteger, Types::UnsizedString, Types::VATTypes

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.registered_resourcesObject (readonly)

Returns the value of attribute registered_resources.



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

def registered_resources
  @registered_resources
end

Class Method Details

.find(id_or_hash) ⇒ Object



95
96
97
98
99
# File 'lib/fortnox/resource.rb', line 95

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



106
107
108
109
# File 'lib/fortnox/resource.rb', line 106

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

.find_one_by(id) ⇒ Object



101
102
103
104
# File 'lib/fortnox/resource.rb', line 101

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

.inherited(subclass) ⇒ Object



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

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

.newObject



66
67
68
# File 'lib/fortnox/resource.rb', line 66

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

.only(filter) ⇒ Object



84
85
86
87
# File 'lib/fortnox/resource.rb', line 84

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

.parse(response) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/fortnox/resource.rb', line 58

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



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

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



89
90
91
92
93
# File 'lib/fortnox/resource.rb', line 89

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

.stub(**model_data) ⇒ Object



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

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

Instance Method Details

#serialiseObject



138
139
140
# File 'lib/fortnox/resource.rb', line 138

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

#updateObject



134
135
136
# File 'lib/fortnox/resource.rb', line 134

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