<commit_before>module ExternalTracker
  def self.track_new_user(user)
    if user && user.external_email_allowed?
      Worker.schedule(ExternalTracker, :persist_new_user, user.global_id)
    end
  end
  
  def self.persist_new_user(user_id)
    user = User.find_by_path(user_id)
    return false unless user && user.external_email_allowed?
    return false unless ENV['HUBSPOT_KEY']
    return false unless user.settings && user.settings['email']

    d = user.devices[0]
    ip = d && d.settings['ip_address']
    location = nil
    if ip && ENV['IPSTACK_KEY']
      url = "http://api.ipstack.com/#{ip}?access_key=#{ENV['IPSTACK_KEY']}"
      begin
        res = Typhoeus.get(url, timeout: 5)
        location = JSON.parse(res.body)
      rescue => e
      end
    end
    email = user.settings['email']
    city = nil
    state = nil
    if location && (location['country_code'] == 'USA' || location['country_code'] == 'US')
      city = location['city']
      state = location['region_name']
    end


    acct = "Communicator Account"
    # "Supervisor Parent Account", "AT Specialist/Lending Library", "Eval"
    # "Therapist", "Teacher", "AAC Grad Course", "Org Added Supervisor", 
    # "Org added Communicator", "Supervisor other", "Communicator other"
    if user.supporter_registration?
      if user.registration_type == 'eval'
        acct = 'AT Specialist/Lending Library'
      elsif user.registration_type == 'parent'
        acct = 'Supervisor Parent Account'
      elsif user.registration_type == 'teacher'
        acct = 'Teacher'
      elsif user.registration_type == 'manually-added-supervisor'
        acct = 'Org Added Supervisor'
      elsif user.registration_type == 'other'
        acct = 'Supervisor other'
      else
        acct = 'Therapist'
      end
    elsif user.registration_type == 'other'
      acct = 'Communicator other'
    elsif user.registration_type == 'manually-added-org-user'
      acct = 'Org added Communicator'
    end

    name = (user.settings['name'] || '').split(/\s/, 2)
    json = {
      properties: [
        {property: 'email', value: email },
        {property: 'firstname', value: name[0]},
        {property: 'lastname', value: name[1]},
        {property: 'city', value: city},
        {property: 'username', value: user.user_name},
        # {property: 'hs_language', value: 'en'},
        {property: 'state', value: state},
        {property: 'account_type', value: acct},
        {property: 'hs_legal_basis', value: 'Legitimate interest – prospect/lead'}
      ]
    }
    # push to external system
    url = "https://api.hubapi.com/contacts/v1/contact/?hapikey=#{ENV['HUBSPOT_KEY']}"
    res = Typhoeus.post(url, {body: json.to_json, headers: {'Content-Type' => 'application/json'}})
    res.code
  end
end<commit_msg>update hubspot api process<commit_after>module ExternalTracker
  def self.track_new_user(user)
    if user && user.external_email_allowed?
      Worker.schedule(ExternalTracker, :persist_new_user, user.global_id)
    end
  end
  
  def self.persist_new_user(user_id)
    user = User.find_by_path(user_id)
    return false unless user && user.external_email_allowed?
    return false unless ENV['HUBSPOT_TOKEN']
    return false unless user.settings && user.settings['email']

    d = user.devices[0]
    ip = d && d.settings['ip_address']
    location = nil
    if ip && ENV['IPSTACK_KEY']
      url = "http://api.ipstack.com/#{ip}?access_key=#{ENV['IPSTACK_KEY']}"
      begin
        res = Typhoeus.get(url, timeout: 5)
        location = JSON.parse(res.body)
      rescue => e
      end
    end
    email = user.settings['email']
    city = nil
    state = nil
    if location && (location['country_code'] == 'USA' || location['country_code'] == 'US')
      city = location['city']
      state = location['region_name']
    end


    acct = "Communicator Account"
    # "Supervisor Parent Account", "AT Specialist/Lending Library", "Eval"
    # "Therapist", "Teacher", "AAC Grad Course", "Org Added Supervisor", 
    # "Org added Communicator", "Supervisor other", "Communicator other"
    if user.supporter_registration?
      if user.registration_type == 'eval'
        acct = 'AT Specialist/Lending Library'
      elsif user.registration_type == 'parent'
        acct = 'Supervisor Parent Account'
      elsif user.registration_type == 'teacher'
        acct = 'Teacher'
      elsif user.registration_type == 'manually-added-supervisor'
        acct = 'Org Added Supervisor'
      elsif user.registration_type == 'other'
        acct = 'Supervisor other'
      else
        acct = 'Therapist'
      end
    elsif user.registration_type == 'other'
      acct = 'Communicator other'
    elsif user.registration_type == 'manually-added-org-user'
      acct = 'Org added Communicator'
    end

    name = (user.settings['name'] || '').split(/\s/, 2)
    json = {
      properties: [
        {property: 'email', value: email },
        {property: 'firstname', value: name[0]},
        {property: 'lastname', value: name[1]},
        {property: 'city', value: city},
        {property: 'username', value: user.user_name},
        # {property: 'hs_language', value: 'en'},
        {property: 'state', value: state},
        {property: 'account_type', value: acct},
        {property: 'hs_legal_basis', value: 'Legitimate interest – prospect/lead'}
      ]
    }
    url = "https://api.hubapi.com/contacts/v1/contact/"
    res = Typhoeus.post(url, {body: json.to_json, headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{ENV['HUBSPOT_TOKEN']}"
      }})
    # if res.code > 299
    #   puts res.body
    # end
    res.code
  end
end
{'fraction_non_alphanumeric': 0.1387119603680113, 'fraction_numerical': 0.002653927813163482, 'mean_word_length': 2.912110726643599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '2229907', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1790, 'n_words': 423}