Module: Intercom::ApiOperations::Save
- Included in:
- Service::Company, Service::Contact, Service::Conversation, Service::Event, Service::Job, Service::Message, Service::Note, Service::Subscription, Service::Tag, Service::User, Service::Visitor
- Defined in:
- lib/intercom/api_operations/save.rb
Instance Method Summary collapse
- #create(params = PARAMS_NOT_PROVIDED) ⇒ Object
- #identity_hash(object) ⇒ Object
- #save(object) ⇒ Object
Instance Method Details
#create(params = PARAMS_NOT_PROVIDED) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/intercom/api_operations/save.rb', line 9 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 instance = collection_class.new(params) instance.mark_fields_as_changed!(params.keys) save(instance) end |
#identity_hash(object) ⇒ Object
31 32 33 |
# File 'lib/intercom/api_operations/save.rb', line 31 def identity_hash(object) object.respond_to?(:identity_vars) ? SliceableHash.new(object.to_hash).slice(*(object.identity_vars.map(&:to_s))) : {} end |
#save(object) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/intercom/api_operations/save.rb', line 21 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}/#{object.id}", object.to_submittable_hash) else response = @client.post("/#{collection_name}", object.to_submittable_hash.merge(identity_hash(object))) end object.from_response(response) if response # may be nil we received back a 202 end |