Module: Fortnox::AttributeKeys

Defined in:
lib/fortnox/attribute_keys.rb

Overview

Checks the attribute names a caller passes to new, stub and update.

Both rest-easy and Dry::Struct build from the attributes they recognise and ignore everything else, so a typo or a string key used to vanish without a word: the request went out missing that field and Fortnox accepted it. The bigger the payload, the easier that is to miss.

This only guards data a caller supplies. Parsing an API response stays tolerant of fields the gem doesn't declare — Fortnox adds them over time, and a response must not fail to parse because of one.

Class Method Summary collapse

Class Method Details

.check(data, known:, subject:) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/fortnox/attribute_keys.rb', line 17

def check(data, known:, subject:)
  normalised = normalise(data)
  unknown = normalised.keys - known
  raise Fortnox::UnknownAttributeError.new(unknown, subject) unless unknown.empty?

  normalised
end

.normalise(data) ⇒ Object

Rails hands params through with string keys. Accept them rather than reporting every attribute in the hash as unknown.



27
28
29
# File 'lib/fortnox/attribute_keys.rb', line 27

def normalise(data)
  data.to_h { |key, value| [symbolise(key), value] }
end

.symbolise(key) ⇒ Object

Left alone if it can't be a symbol, so it falls through to the unknown check and is reported as an attribute error rather than a NoMethodError.



33
34
35
# File 'lib/fortnox/attribute_keys.rb', line 33

def symbolise(key)
  key.respond_to?(:to_sym) ? key.to_sym : key
end