Class: TrakioClient::Identify

Inherits:
EndPoint
  • Object
show all
Defined in:
lib/trakio_client/identify.rb

Instance Attribute Summary

Attributes inherited from EndPoint

#trakio

Instance Method Summary collapse

Methods inherited from EndPoint

#initialize

Constructor Details

This class inherits a constructor from TrakioClient::EndPoint

Instance Method Details

#check_companies(companies) ⇒ Object



59
60
61
62
63
# File 'lib/trakio_client/identify.rb', line 59

def check_companies companies
  unless companies.all?{ |x| x.is_a?(Hash) } && companies.all?{ |x| x.include? :company_id }
    raise Exceptions::InvalidProperty.new('The `companies` property must be an array of hashes each with a value for `company_id`')
  end
end

#check_parameters(distinct_id, properties) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/trakio_client/identify.rb', line 50

def check_parameters distinct_id, properties
  unless properties.is_a?(Hash)
    raise Exceptions::InvalidParameter.new("The `properties` parameter must be a hash.")
  end
  unless distinct_id
    raise Exceptions::MissingParameter.new('The `distinct_id` parameter must be provided.')
  end
end

#process_companies(company_id, properties) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/trakio_client/identify.rb', line 19

def process_companies company_id, properties

  # String company should be moved to company_name
  if properties[:company].is_a? String
    properties[:company_name] = properties.delete :company
  end

  # Company must be an array
  [:company, :companies].each do |x|
    properties[x] ||= []
    unless properties[x].is_a?(Array)
      properties[x] = [properties[x]]
    end
  end

  # Merge companies and company
  properties[:company] += properties.delete(:companies) || []

  check_companies properties[:company]

  # Inject current company
  if company_id && properties[:company].none?{ |x| x[:company_id] == company_id }
    properties[:company] << { company_id: company_id }
  end

  # Clean up company
  properties.delete(:company) if properties[:company].empty?

  properties
end

#run(p = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/trakio_client/identify.rb', line 4

def run p = {}
  properties = p[:properties] || {}
  distinct_id = p[:distinct_id] || self.distinct_id
  company_id = p[:company_id] || self.company_id
  check_parameters distinct_id, properties
  properties = process_companies company_id, properties

  params = {
    distinct_id: distinct_id,
    properties: properties
  }

  send_request 'identify', params
end