Class: Bento::Subscribers

Inherits:
Object
  • Object
show all
Defined in:
lib/bento/resources/subscribers.rb

Class Method Summary collapse

Class Method Details

.add_field(email:, key:, value:) ⇒ Object

Add a field to a subscriber Usage: Bento::Subscribers.add_field(email: ‘test@bentonow.com’, key: ‘company’, value: ‘Acme Inc’)

[View source]

83
84
85
# File 'lib/bento/resources/subscribers.rb', line 83

def add_field(email:, key:, value:)
  run_command(command: 'add_field', email: email, query: { key: key, value: value })
end

.add_tag(email:, tag:) ⇒ Object

Add a tag to a subscriber Usage: Bento::Subscribers.add_tag(email: ‘test@bentonow.com’, tag: ‘new_tag’)

[View source]

65
66
67
# File 'lib/bento/resources/subscribers.rb', line 65

def add_tag(email:, tag:)
  run_command(command: 'add_tag', email: email, query: tag)
end

.add_tag_via_event(email:, tag:) ⇒ Object

Add a tag to a subscriber via an event Usage: Bento::Subscribers.add_tag_via_event(email: ‘test@bentonow.com’, tag: ‘event_tag’)

[View source]

71
72
73
# File 'lib/bento/resources/subscribers.rb', line 71

def add_tag_via_event(email:, tag:)
  run_command(command: 'add_tag_via_event', email: email, query: tag)
end

.change_email(old_email:, new_email:) ⇒ Object

Change a subscriber’s email Usage: Bento::Subscribers.change_email(old_email: ‘old@example.com’, new_email: ‘new@example.com’)

[View source]

107
108
109
# File 'lib/bento/resources/subscribers.rb', line 107

def change_email(old_email:, new_email:)
  run_command(command: 'change_email', email: old_email, query: new_email)
end

.find_by(email: nil, uuid: nil) ⇒ Object

Find a subscriber by email or uuid Usage: Bento::Subscribers.find_by(email: ‘test@bentonow.com’) or: Bento::Subscribers.find_by(uuid: ‘subscriber-uuid’)

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bento/resources/subscribers.rb', line 7

def find_by(email: nil, uuid: nil)
  params = default_params
  params[:email] = email if email
  params[:uuid] = uuid if uuid
  response = client.get("api/v1/fetch/subscribers?#{URI.encode_www_form(params)}")
  
  if response['data'].nil?
    raise StandardError, 'Bento Error: No user found with the given email or uuid'
  else
    Subscriber.new(response['data'])
  end
end

.find_or_create_by(email: nil) ⇒ Object

Find or create a subscriber by email or uuid Usage: Bento::Subscribers.find_or_create_by(email: ‘test@bentonow.com’) or: Bento::Subscribers.find_or_create_by(uuid: ‘subscriber-uuid’)

[View source]

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bento/resources/subscribers.rb', line 23

def find_or_create_by(email: nil)
  params = default_params
  payload = {
    subscriber: {
      email: email
    }.compact
  }.to_json

  response = client.post("api/v1/fetch/subscribers?#{URI.encode_www_form(params)}", payload)
  
  if response['data'].nil?
    raise StandardError, 'Bento Error: No user found with the given email or uuid'
  else
    Subscriber.new(response['data'])
  end
end

.import(subscribers) ⇒ Object

Import or update subscribers in bulk Usage: Bento::Subscribers.import([‘user1@example.com’, first_name: ‘John’, ‘user2@example.com’, last_name: ‘Doe’])

[View source]

42
43
44
45
46
# File 'lib/bento/resources/subscribers.rb', line 42

def import(subscribers)
  payload = { subscribers: subscribers }.to_json
  response = client.post("api/v1/batch/subscribers?#{URI.encode_www_form(default_params)}", payload)
  Bento::Response.new(response)
end

.remove_field(email:, field:) ⇒ Object

Remove a field from a subscriber Usage: Bento::Subscribers.remove_field(email: ‘test@bentonow.com’, field: ‘company’)

[View source]

89
90
91
# File 'lib/bento/resources/subscribers.rb', line 89

def remove_field(email:, field:)
  run_command(command: 'remove_field', email: email, query: field)
end

.remove_tag(email:, tag:) ⇒ Object

Remove a tag from a subscriber Usage: Bento::Subscribers.remove_tag(email: ‘test@bentonow.com’, tag: ‘old_tag’)

[View source]

77
78
79
# File 'lib/bento/resources/subscribers.rb', line 77

def remove_tag(email:, tag:)
  run_command(command: 'remove_tag', email: email, query: tag)
end

.run_command(command:, email:, query: nil) ⇒ Object

Run a command to change a subscriber’s data Usage: Bento::Subscribers.run_command(command: ‘add_tag’, email: ‘test@bentonow.com’, query: ‘new_tag’)

[View source]

50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bento/resources/subscribers.rb', line 50

def run_command(command:, email:, query: nil)
  payload = {
    command: [{
      command: command,
      email: email,
      query: query
    }]
  }.to_json
  
  response = client.post("api/v1/fetch/commands?#{URI.encode_www_form(default_params)}", payload)
  Bento::Response.new(response)
end

.subscribe(email:) ⇒ Object

Subscribe a user Usage: Bento::Subscribers.subscribe(email: ‘test@bentonow.com’)

[View source]

95
96
97
# File 'lib/bento/resources/subscribers.rb', line 95

def subscribe(email:)
  run_command(command: 'subscribe', email: email)
end

.unsubscribe(email:) ⇒ Object

Unsubscribe a user Usage: Bento::Subscribers.unsubscribe(email: ‘test@bentonow.com’)

[View source]

101
102
103
# File 'lib/bento/resources/subscribers.rb', line 101

def unsubscribe(email:)
  run_command(command: 'unsubscribe', email: email)
end