accodeing/fortnox-api

View on GitHub
lib/fortnox/api/mappers/base.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

require_relative 'base/canonical_name_sym'
require_relative 'base/from_json'
require_relative 'base/to_json'

module Fortnox
  module API
    module Mapper
      class Base
        include FromJSON
        include ToJSON
        extend CanonicalNameSym

        def diff(entity_hash, parent_hash)
          hash_diff(entity_hash[self.class::JSON_ENTITY_WRAPPER],
                    parent_hash[self.class::JSON_ENTITY_WRAPPER])
        end

        private

        def hash_diff(hash1, hash2)
          hash1.dup
               .delete_if { |k, v| hash2[k] == v }
               .merge!(hash2.dup.delete_if { |k, _| hash1.key?(k) })
        end
      end
    end
  end
end