module Fortnox module API module Repository module JSONConvertion private def hash_to_entity( entity_json_hash ) remove_nil_values(entity_json_hash) entity_hash = convert_hash_keys_from_json_format( entity_json_hash ) instansiate( entity_hash ) end def entity_to_hash( entity ) entity_hash = entity.to_hash clean_entity_hash = sanitise( entity_hash ) entity_json_hash = convert_hash_keys_to_json_format( clean_entity_hash ) { @options.json_entity_wrapper => entity_json_hash } end def convert_hash_keys_from_json_format( hash ) hash.each_with_object( {} ) do |(key, value), json_hash| json_hash[ convert_key_from_json( key ) ] = value end end def convert_key_from_json( key ) @options.json_to_attr_map.fetch( key ){ default_key_from_json_transform( key ) } end def default_key_from_json_transform( key ) key = key.to_s return key.downcase if key.match(/\A[A-Z]+\z/) key.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z])([A-Z])/, '\1_\2'). downcase. to_sym end def convert_hash_keys_to_json_format( hash ) hash.each_with_object( {} ) do |(key, value), json_hash| json_hash[ convert_key_to_json( key ) ] = value end end def convert_key_to_json( key ) @options.attr_to_json_map.fetch( key ){ default_key_to_json_transform( key ) } end def default_key_to_json_transform( key ) key.to_s.split('_').map(&:capitalize).join('') end def sanitise( hash ) hash.select do |key, value| next false if @options.keys_filtered_on_save.include?( key ) value != nil end end def remove_nil_values( hash ) hash.delete_if{ |_, value| value.nil? } end end end end end