Sha256: f95720cc5b22cef00a26caafc2ec265da3a360e9249ca00b529c3ba6a8c59a41

Contents?: true

Size: 1.32 KB

Versions: 10

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Fortnox
  module API
    module Repository
      module Savers
        def save(entity)
          return true if entity.saved?

          return save_new(entity) if entity.new?
          update_existing(entity)
        end

        private

        def execute_save(entity)
          body = get_changes_on(entity).to_json
          result = yield body
          instantiate_saved(result)
        end

        def save_new(entity)
          execute_save(entity) do |body|
            post(self.class::URI, body: body)
          end
        end

        def update_existing(entity)
          execute_save(entity) do |body|
            put(get_update_url_for(entity), body: body)
          end
        end

        def get_changes_on(entity)
          hash = @mapper.entity_to_hash(entity, @keys_filtered_on_save)
          parent_hash = @mapper.entity_to_hash(entity.parent, @keys_filtered_on_save)

          @mapper.wrap_entity_json_hash(@mapper.diff(hash, parent_hash))
        end

        def get_update_url_for(entity)
          "#{self.class::URI}#{entity.unique_id}"
        end

        def instantiate_saved(wrapped_json_hash)
          instantiate(
            @mapper.wrapped_json_hash_to_entity_hash(
              wrapped_json_hash
            )
          )
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fortnox-api-0.8.2 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.8.1 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.8.0 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.7.2 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.7.1 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.7.0 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.6.3 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.6.2 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.6.1 lib/fortnox/api/repositories/base/savers.rb
fortnox-api-0.6.0 lib/fortnox/api/repositories/base/savers.rb