Sha256: 1ebabce898bcdd978fcd45bcd2f2b809ac00dfbb358b30a75889dc499f41d2d8

Contents?: true

Size: 1.1 KB

Versions: 14

Compression:

Stored size: 1.1 KB

Contents

require 'intercom/traits/api_resource'

module Intercom
  module ApiOperations
    module Save

      module ClassMethods
        def create(params)
          instance = self.new(params)
          instance.mark_fields_as_changed!(params.keys)
          instance.save
        end
      end

      def self.included(base)
        base.extend(ClassMethods)
      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)
        else
          response = Intercom.post("/#{collection_name}", to_submittable_hash.merge(identity_hash))
        end
        from_response(response) if response # may be nil we received back a 202
      end

      private

      def id_present?
        id && id.to_s != ''
      end

      def posted_updates?
        respond_to?(:update_verb) && update_verb == 'post'
      end

      def identity_hash
        respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
intercom-2.4.4 lib/intercom/api_operations/save.rb
intercom-2.4.3 lib/intercom/api_operations/save.rb
intercom-2.4.2 lib/intercom/api_operations/save.rb
intercom-2.4.1 lib/intercom/api_operations/save.rb
intercom-2.4.0 lib/intercom/api_operations/save.rb
intercom-2.3.0 lib/intercom/api_operations/save.rb
intercom-2.2.4 lib/intercom/api_operations/save.rb
intercom-2.2.3 lib/intercom/api_operations/save.rb
intercom-2.2.2 lib/intercom/api_operations/save.rb
intercom-2.2.1 lib/intercom/api_operations/save.rb
intercom-2.2.0 lib/intercom/api_operations/save.rb
intercom-2.1.6 lib/intercom/api_operations/save.rb
intercom-2.1.5 lib/intercom/api_operations/save.rb
intercom-2.1.4 lib/intercom/api_operations/save.rb