Class: Customerio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/customerio/client.rb

Defined Under Namespace

Classes: MissingIdAttributeError, ParamError

Constant Summary collapse

PUSH_OPENED =
'opened'
PUSH_CONVERTED =
'converted'
PUSH_DELIVERED =
'delivered'
VALID_PUSH_EVENTS =
[PUSH_OPENED, PUSH_CONVERTED, PUSH_DELIVERED]

Instance Method Summary collapse

Constructor Details

#initialize(site_id, api_key, options = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
# File 'lib/customerio/client.rb', line 20

def initialize(site_id, api_key, options = {})
  options[:region] = Customerio::Regions::US if options[:region].nil?
  raise "region must be an instance of Customerio::Regions::Region" unless options[:region].is_a?(Customerio::Regions::Region)

  options[:url] = options[:region].track_url if options[:url].nil? || options[:url].empty?
  @client = Customerio::BaseClient.new({ site_id: site_id, api_key: api_key }, options)
end

Instance Method Details

#add_device(customer_id, device_id, platform, data = {}) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/customerio/client.rb', line 67

def add_device(customer_id, device_id, platform, data={})
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  raise ParamError.new("device_id must be a non-empty string") if is_empty?(device_id)
  raise ParamError.new("platform must be a non-empty string") if is_empty?(platform)

  if data.nil?
    data = {}
  end

  raise ParamError.new("data parameter must be a hash") unless data.is_a?(Hash)

  @client.request_and_verify_response(:put, device_path(customer_id), {
    :device => data.update({
      :id => device_id,
      :platform => platform,
    })
  })
end

#delete(customer_id) ⇒ Object

Raises:



32
33
34
35
# File 'lib/customerio/client.rb', line 32

def delete(customer_id)
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  @client.request_and_verify_response(:delete, customer_path(customer_id))
end

#delete_device(customer_id, device_id) ⇒ Object

Raises:



86
87
88
89
90
91
# File 'lib/customerio/client.rb', line 86

def delete_device(customer_id, device_id)
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  raise ParamError.new("device_id must be a non-empty string") if is_empty?(device_id)

  @client.request_and_verify_response(:delete, device_id_path(customer_id, device_id))
end

#identify(attributes) ⇒ Object



28
29
30
# File 'lib/customerio/client.rb', line 28

def identify(attributes)
  create_or_update(attributes)
end

#merge_customers(primary_id_type, primary_id, secondary_id_type, secondary_id) ⇒ Object

Raises:



106
107
108
109
110
111
112
113
114
115
# File 'lib/customerio/client.rb', line 106

def merge_customers(primary_id_type, primary_id, secondary_id_type, secondary_id)
  raise ParamError.new("invalid primary_id_type") if !is_valid_id_type?(primary_id_type)
  raise ParamError.new("primary_id must be a non-empty string") if is_empty?(primary_id)
  raise ParamError.new("invalid secondary_id_type") if !is_valid_id_type?(secondary_id_type)
  raise ParamError.new("secondary_id must be a non-empty string") if is_empty?(secondary_id)

  body = { :primary => {primary_id_type => primary_id}, :secondary => {secondary_id_type => secondary_id} }

  @client.request_and_verify_response(:post, merge_customers_path, body)
end

#pageview(customer_id, page, attributes = {}) ⇒ Object

Raises:



54
55
56
57
58
59
# File 'lib/customerio/client.rb', line 54

def pageview(customer_id, page, attributes = {})
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  raise ParamError.new("page must be a non-empty string") if is_empty?(page)

  create_pageview_event(customer_id, page, attributes)
end

#suppress(customer_id) ⇒ Object

Raises:



37
38
39
40
# File 'lib/customerio/client.rb', line 37

def suppress(customer_id)
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  @client.request_and_verify_response(:post, suppress_path(customer_id))
end

#track(customer_id, event_name, attributes = {}) ⇒ Object

Raises:



47
48
49
50
51
52
# File 'lib/customerio/client.rb', line 47

def track(customer_id, event_name, attributes = {})
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  raise ParamError.new("event_name must be a non-empty string") if is_empty?(event_name)

  create_customer_event(customer_id, event_name, attributes)
end

#track_anonymous(anonymous_id, event_name, attributes = {}) ⇒ Object

Raises:



61
62
63
64
65
# File 'lib/customerio/client.rb', line 61

def track_anonymous(anonymous_id, event_name, attributes = {})
  raise ParamError.new("event_name must be a non-empty string") if is_empty?(event_name)

  create_anonymous_event(anonymous_id, event_name, attributes)
end

#track_push_notification_event(event_name, attributes = {}) ⇒ Object

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/customerio/client.rb', line 93

def track_push_notification_event(event_name, attributes = {})
    keys = [:delivery_id, :device_id, :timestamp]
    attributes = Hash[attributes.map { |(k,v)| [ k.to_sym, v ] }].
        select { |k, v| keys.include?(k) }

    raise ParamError.new('event_name must be one of opened, converted, or delivered') unless VALID_PUSH_EVENTS.include?(event_name)
    raise ParamError.new('delivery_id must be a non-empty string') unless attributes[:delivery_id] != "" and !attributes[:delivery_id].nil?
    raise ParamError.new('device_id must be a non-empty string') unless attributes[:device_id] != "" and !attributes[:device_id].nil?
    raise ParamError.new('timestamp must be a valid timestamp') unless valid_timestamp?(attributes[:timestamp])

    @client.request_and_verify_response(:post, track_push_notification_event_path, attributes.merge(event: event_name))
end

#unsuppress(customer_id) ⇒ Object

Raises:



42
43
44
45
# File 'lib/customerio/client.rb', line 42

def unsuppress(customer_id)
  raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id)
  @client.request_and_verify_response(:post, unsuppress_path(customer_id))
end