<commit_before>module Hubspot
  #
  # HubSpot Contacts API
  #
  # {https://developers.hubspot.com/docs/methods/contacts/contacts-overview}
  #
  # TODO: work on all endpoints that can specify contact properties, property mode etc... as params. cf pending specs
  class Contact
    CREATE_CONTACT_PATH        = "/contacts/v1/contact"
    GET_CONTACT_BY_EMAIL_PATH  = "/contacts/v1/contact/email/:contact_email/profile"
    GET_CONTACTS_BY_EMAIL_PATH = "/contacts/v1/contact/emails/batch"
    GET_CONTACT_BY_ID_PATH     = "/contacts/v1/contact/vid/:contact_id/profile"
    CONTACT_BATCH_PATH         = '/contacts/v1/contact/vids/batch'
    GET_CONTACT_BY_UTK_PATH    = "/contacts/v1/contact/utk/:contact_utk/profile"
    GET_CONTACTS_BY_UTK_PATH   = '/contacts/v1/contact/utks/batch'
    UPDATE_CONTACT_PATH        = "/contacts/v1/contact/vid/:contact_id/profile"
    DESTROY_CONTACT_PATH       = "/contacts/v1/contact/vid/:contact_id"
    CONTACTS_PATH              = "/contacts/v1/lists/all/contacts/all"
    RECENT_CONTACTS_PATH       = '/contacts/v1/lists/recently_updated/contacts/recent'

    class << self
      # {https://developers.hubspot.com/docs/methods/contacts/create_contact}
      def create!(email, params={})
        params_with_email = params.stringify_keys.merge("email" => email)
        post_data = {properties: Hubspot::Utils.hash_to_properties(params_with_email)}
        response = Hubspot::Connection.post_json(CREATE_CONTACT_PATH, params: {}, body: post_data )
        new(response)
      end

      # {https://developers.hubspot.com/docs/methods/contacts/get_contacts}
      # {https://developers.hubspot.com/docs/methods/contacts/get_recently_updated_contacts}
      def all(opts={})
        recent = opts.delete(:recent) { false } 
        path, opts = 
        if recent 
          [RECENT_CONTACTS_PATH, Hubspot::ContactProperties.add_default_parameters(opts)] 
        else 
          [CONTACTS_PATH, opts]
        end

        response = Hubspot::Connection.get_json(path, opts)
        response['contacts'].map { |c| new(c) }
      end

      # TODO: create or update a contact
      # PATH /contacts/v1/contact/createOrUpdate/email/:contact_email
      # API endpoint: https://developers.hubspot.com/docs/methods/contacts/create_or_update
      # + batch mode: https://developers.hubspot.com/docs/methods/contacts/batch_create_or_update

      # NOTE: problem with batch api endpoint
      # {https://developers.hubspot.com/docs/methods/contacts/get_contact}
      # {https://developers.hubspot.com/docs/methods/contacts/get_batch_by_vid}
      def find_by_id(vids)
        batch_mode, path, params = case vids
        when Integer then [false, GET_CONTACT_BY_ID_PATH, { contact_id: vids }]
        when Array then [true, CONTACT_BATCH_PATH, { batch_vid: vids }]
        else raise Hubspot::InvalidParams, 'expecting Integer or Array of Integers parameter'
        end

        response = Hubspot::Connection.get_json(path, params)
        raise Hubspot::ApiError if batch_mode<commit_msg>testing gem change<commit_after>module Hubspot
  #
  # HubSpot Contacts API
  #
  # {https://developers.hubspot.com/docs/methods/contacts/contacts-overview}
  #
  # TODO: work on all endpoints that can specify contact properties, property mode etc... as params. cf pending specs
  class Contact
    CREATE_CONTACT_PATH        = "/contacts/v1/contact"
    GET_CONTACT_BY_EMAIL_PATH  = "/contacts/v1/contact/email/:contact_email/profile"
    GET_CONTACTS_BY_EMAIL_PATH = "/contacts/v1/contact/emails/batch"
    GET_CONTACT_BY_ID_PATH     = "/contacts/v1/contact/vid/:contact_id/profile"
    CONTACT_BATCH_PATH         = '/contacts/v1/contact/vids/batch'
    GET_CONTACT_BY_UTK_PATH    = "/contacts/v1/contact/utk/:contact_utk/profile"
    GET_CONTACTS_BY_UTK_PATH   = '/contacts/v1/contact/utks/batch'
    UPDATE_CONTACT_PATH        = "/contacts/v1/contact/vid/:contact_id/profile"
    DESTROY_CONTACT_PATH       = "/contacts/v1/contact/vid/:contact_id"
    CONTACTS_PATH              = "/contacts/v1/lists/all/contacts/all"
    RECENT_CONTACTS_PATH       = '/contacts/v1/lists/recently_updated/contacts/recent'

    class << self
      # {https://developers.hubspot.com/docs/methods/contacts/create_contact}
      def create!(email, params={})
        params_with_email = params.stringify_keys.merge("email" => email)
        post_data = {properties: Hubspot::Utils.hash_to_properties(params_with_email)}
        response = Hubspot::Connection.post_json(CREATE_CONTACT_PATH, params: {}, body: post_data )
        new(response)
      end

      # {https://developers.hubspot.com/docs/methods/contacts/get_contacts}
      # {https://developers.hubspot.com/docs/methods/contacts/get_recently_updated_contacts}
      def all(opts={})
        recent = opts.delete(:recent) { false } 
        path, opts = 
        if recent 
          [RECENT_CONTACTS_PATH, Hubspot::ContactProperties.add_default_parameters(opts)] 
        else 
          [CONTACTS_PATH, opts]
        end

        response = Hubspot::Connection.get_json(path, opts)
        response['contacts'].map { |c| new(c) }
      end

      # TODO: create or update a contact
      # PATH /contacts/v1/contact/createOrUpdate/email/:contact_email
      # API endpoint: https://developers.hubspot.com/docs/methods/contacts/create_or_update
      # + batch mode: https://developers.hubspot.com/docs/methods/contacts/batch_create_or_update

      # NOTE: problem with batch api endpoint
      # {https://developers.hubspot.com/docs/methods/contacts/get_contact}
      # {https://developers.hubspot.com/docs/methods/contacts/get_batch_by_vid}
      def find_by_id(vids)
        batch_mode, path, params = case vids
        when Integer then [false, GET_CONTACT_BY_ID_PATH, { contact_id: vids }]
        when Array then [true, CONTACT_BATCH_PATH, { batch_vid: vids }]
        else raise Hubspot::InvalidParams, 'expecting Integer or Array of Integers parameter'
        end

        response = Hubspot::Connection.get_json(path, params)
        raise Hubspot::ApiError if batch_mode
        new(response)
      end

      def find_by_vid(vids)
        batch_mode, path, params = case vids
                                     when Integer then [false, GET_CONTACT_BY_ID_PATH, { contact_id: vids }]
                                     when Array then [true, CONTACT_BATCH_PATH, { batch_vid: vids }]
                                     else raise Hubspot::InvalidParams, 'expecting Integer or Array of Integers parameter'
                                   end

        response = Hubspot::Connection.get_json(path, params)
        raise Hubspot::ApiError if batch_mode
{'fraction_non_alphanumeric': 0.10647590361445783, 'fraction_numerical': 0.0036144578313253013, 'mean_word_length': 2.9179941002949854, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5683251', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2124, 'n_words': 346}