<commit_before>require 'intercom/traits/api_resource'

module Intercom
  module ApiOperations
    module Save
      PARAMS_NOT_PROVIDED = Object.new
      private_constant :PARAMS_NOT_PROVIDED

      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

      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 = @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

      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?(object)
        object.id && object.id.to_s != ''
      end

      def posted_updates?(object)
        object.respond_to?(:update_verb) && object.update_verb == 'post'
      end
    end
  end
end<commit_msg>Fix NoMethodError when updating existing records<commit_after>require 'intercom/traits/api_resource'

module Intercom
  module ApiOperations
    module Save
      PARAMS_NOT_PROVIDED = Object.new
      private_constant :PARAMS_NOT_PROVIDED

      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

      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

      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?(object)
        object.id && object.id.to_s != ''
      end

      def posted_updates?(object)
        object.respond_to?(:update_verb) && object.update_verb == 'post'
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.097678916827853, 'fraction_numerical': 0.0025789813023855577, 'mean_word_length': 3.014230271668823, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5198280', 'n_tokens_mistral': 938, 'n_tokens_neox': 923, 'n_words': 170}