<commit_before>require 'agilecrm-wrapper/error'
require 'hashie'

module AgileCRMWrapper
  class Contact < Hashie::Mash
    SYSTEM_PROPERTIES = %w(first_name last_name company title email)
    CONTACT_FIELDS = %w(id type tags lead_score star_value)

    class << self
      def all
        response = AgileCRMWrapper.connection.get('contacts')
        if response.status == 200
          return response.body.map { |body| new body }
        else
          return response
        end
      end

      def find(id)
        response = AgileCRMWrapper.connection.get("contacts/#{id}")
        if response.status == 200
          new(response.body)
        elsif response.status == 204
          fail(AgileCRMWrapper::NotFound.new(response))
        end
      end

      def search_by_email(*emails)
        emails = emails.flatten.compact.uniq
        response = AgileCRMWrapper.connection.post(
          'contacts/search/email', "email_ids=#{emails}",
          'content-type' => 'application/x-www-form-urlencoded'
        )
        if response.body.size > 1
          response.body.reject(&:nil?).map { |body| new body }
        else
          res = new(response.body.first)
          res.empty? ? nil : res
        end
      end

      def create(options = {})
        payload = parse_contact_fields(options)
        AgileCRMWrapper.connection.post('contacts', payload)
      end

      def delete(arg)
        AgileCRMWrapper.connection.delete("contacts/#{arg}")
      end

      def parse_contact_fields(options)
        payload = { 'properties' => [] }
        options.each do |key, value|
          if contact_field?(key)
            payload[key.to_s] = value
          else
            payload['properties'] << parse_property(key, value)
          end<commit_msg>Fix contact creation<commit_after>require 'agilecrm-wrapper/error'
require 'hashie'

module AgileCRMWrapper
  class Contact < Hashie::Mash
    SYSTEM_PROPERTIES = %w(first_name last_name company title email)
    CONTACT_FIELDS = %w(id type tags lead_score star_value)

    class << self
      def all
        response = AgileCRMWrapper.connection.get('contacts')
        if response.status == 200
          return response.body.map { |body| new body }
        else
          return response
        end
      end

      def find(id)
        response = AgileCRMWrapper.connection.get("contacts/#{id}")
        if response.status == 200
          new(response.body)
        elsif response.status == 204
          fail(AgileCRMWrapper::NotFound.new(response))
        end
      end

      def search_by_email(*emails)
        emails = emails.flatten.compact.uniq
        response = AgileCRMWrapper.connection.post(
          'contacts/search/email', "email_ids=#{emails}",
          'content-type' => 'application/x-www-form-urlencoded'
        )
        if response.body.size > 1
          response.body.reject(&:nil?).map { |body| new body }
        else
          res = new(response.body.first)
          res.empty? ? nil : res
        end
      end

      def create(options = {})
        contact = nil
        payload = parse_contact_fields(options)
        response = AgileCRMWrapper.connection.post('contacts', payload)
        if response && response.status == 200
          contact = new(response.body)
        end

        contact
      end

      def delete(arg)
        AgileCRMWrapper.connection.delete("contacts/#{arg}")
      end

      def parse_contact_fields(options)
        payload = { 'properties' => [] }
        options.each do |key, value|
          if contact_field?(key)
            payload[key.to_s] = value
          else
            payload['properties'] << parse_property(key, value)
          end
{'fraction_non_alphanumeric': 0.10259880887926368, 'fraction_numerical': 0.006226312939902545, 'mean_word_length': 2.115514333895447, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3286864', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1059, 'n_words': 226}