Module: Fortnox::API::Mapper::ToJSON::ClassMethods

Defined in:
lib/fortnox/api/mappers/base/to_json.rb

Instance Method Summary collapse

Instance Method Details

#call(entity, keys_to_filter = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/fortnox/api/mappers/base/to_json.rb', line 18

def call(entity, keys_to_filter = {})
  entity_hash = entity.to_hash
  clean_entity_hash = sanitise(entity_hash, keys_to_filter)
  clean_entity_hash = convert_hash_keys_to_json_format(clean_entity_hash)
  Registry[:hash].call(clean_entity_hash)
end

#convert_hash_keys_to_json_format(hash) ⇒ Object

PRIVATE



27
28
29
30
31
# File 'lib/fortnox/api/mappers/base/to_json.rb', line 27

def convert_hash_keys_to_json_format(hash)
  hash.transform_keys do |key|
    convert_key_to_json(key)
  end
end

#convert_key_to_json(key) ⇒ Object



33
34
35
# File 'lib/fortnox/api/mappers/base/to_json.rb', line 33

def convert_key_to_json(key)
  self::KEY_MAP.fetch(key) { default_key_to_json_transform(key) }
end

#default_key_to_json_transform(key) ⇒ Object



37
38
39
# File 'lib/fortnox/api/mappers/base/to_json.rb', line 37

def default_key_to_json_transform(key)
  key.to_s.split('_').map(&:capitalize).join
end

#sanitise(hash, keys_to_filter) ⇒ Object



41
42
43
44
45
# File 'lib/fortnox/api/mappers/base/to_json.rb', line 41

def sanitise(hash, keys_to_filter)
  hash.reject do |key, value|
    keys_to_filter.include?(key) || value.nil?
  end
end