module Intercom::ApiOperations::Save

Constants

PARAMS_NOT_PROVIDED

Public Instance Methods

create(params = PARAMS_NOT_PROVIDED) click to toggle source
# File lib/intercom/api_operations/save.rb, line 12
def create(params = PARAMS_NOT_PROVIDED)
  if collection_class.ancestors.include?(Intercom::Lead) && params == PARAMS_NOT_PROVIDED
    params = {}
  elsif params == PARAMS_NOT_PROVIDED
    raise ArgumentError, '.create requires 1 parameter'
  end

  instance = collection_class.new(params)
  instance.mark_fields_as_changed!(params.keys)
  save(instance)
end
identity_hash(object) click to toggle source
# File lib/intercom/api_operations/save.rb, line 34
def identity_hash(object)
  object.respond_to?(:identity_vars) ? SliceableHash.new(object.to_hash).slice(*object.identity_vars.map(&:to_s)) : {}
end
save(object) click to toggle source
# File lib/intercom/api_operations/save.rb, line 24
def save(object)
  if id_present?(object) && !posted_updates?(object)
    response = @client.put("/#{collection_name}/#{object.id}", object.to_submittable_hash)
  else
    response = @client.post("/#{collection_name}", object.to_submittable_hash.merge(identity_hash(object)))
  end
  object.client = @client
  object.from_response(response) if response # may be nil we received back a 202
end

Private Instance Methods

id_present?(object) click to toggle source
# File lib/intercom/api_operations/save.rb, line 40
def id_present?(object)
  object.id && object.id.to_s != ''
end
posted_updates?(object) click to toggle source
# File lib/intercom/api_operations/save.rb, line 44
def posted_updates?(object)
  object.respond_to?(:update_verb) && object.update_verb == 'post'
end