module Fortnox::API::Repository::Savers

Public Instance Methods

save(entity) click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 7
def save(entity)
  return true if entity.saved?

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

Private Instance Methods

execute_save(entity) { |body| ... } click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 16
def execute_save(entity)
  body = get_changes_on(entity).to_json
  result = yield body
  instantiate_saved(result)
end
get_changes_on(entity) click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 34
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
get_update_url_for(entity) click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 41
def get_update_url_for(entity)
  "#{self.class::URI}#{entity.unique_id}"
end
instantiate_saved(wrapped_json_hash) click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 45
def instantiate_saved(wrapped_json_hash)
  instantiate(
    @mapper.wrapped_json_hash_to_entity_hash(
      wrapped_json_hash
    )
  )
end
save_new(entity) click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 22
def save_new(entity)
  execute_save(entity) do |body|
    post(self.class::URI, body: body)
  end
end
update_existing(entity) click to toggle source
# File lib/fortnox/api/repositories/base/savers.rb, line 28
def update_existing(entity)
  execute_save(entity) do |body|
    put(get_update_url_for(entity), body: body)
  end
end