lib/intercom/api_operations/save.rb in intercom-2.5.4 vs lib/intercom/api_operations/save.rb in intercom-3.0.0b1
- old
+ new
@@ -1,51 +1,46 @@
require 'intercom/traits/api_resource'
module Intercom
module ApiOperations
module Save
+ PARAMS_NOT_PROVIDED = Object.new
+ private_constant :PARAMS_NOT_PROVIDED
- module ClassMethods
- PARAMS_NOT_PROVIDED = Object.new
- def create(params = PARAMS_NOT_PROVIDED)
- if self.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
- params = Hash.new
- elsif params == PARAMS_NOT_PROVIDED
- raise ArgumentError, '.create requires 1 parameter'
- end
-
- instance = self.new(params)
- instance.mark_fields_as_changed!(params.keys)
- instance.save
+ def create(params = PARAMS_NOT_PROVIDED)
+ if collection_class.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
+ params = Hash.new
+ elsif params == PARAMS_NOT_PROVIDED
+ raise ArgumentError, '.create requires 1 parameter'
end
- end
- def self.included(base)
- base.extend(ClassMethods)
+ instance = collection_class.new(params)
+ instance.mark_fields_as_changed!(params.keys)
+ save(instance)
end
- def save
- collection_name = Utils.resource_class_to_collection_name(self.class)
- if id_present? && !posted_updates?
- response = Intercom.put("/#{collection_name}/#{id}", to_submittable_hash)
+ def save(object)
+ collection_name = Utils.resource_class_to_collection_name(collection_class)
+ if id_present?(object) && !posted_updates?(object)
+ response = @client.put("/#{collection_name}/#{id}", object.to_submittable_hash)
else
- response = Intercom.post("/#{collection_name}", to_submittable_hash.merge(identity_hash))
+ response = @client.post("/#{collection_name}", object.to_submittable_hash.merge(identity_hash(object)))
end
- from_response(response) if response # may be nil we received back a 202
+ object.from_response(response) if response # may be nil we received back a 202
end
- def identity_hash
- respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
+ def identity_hash(object)
+ object.respond_to?(:identity_vars) ? SliceableHash.new(object.to_hash).slice(*(object.identity_vars.map(&:to_s))) : {}
end
private
- def id_present?
- id && id.to_s != ''
+ def id_present?(object)
+ object.id && object.id.to_s != ''
end
- def posted_updates?
- respond_to?(:update_verb) && update_verb == 'post'
+ def posted_updates?(object)
+ object.respond_to?(:update_verb) && object.update_verb == 'post'
end
end
end
end