Class: CreateSend::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/createsend/subscriber.rb

Overview

Represents a subscriber and associated functionality.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_id, email_address) ⇒ Subscriber

Returns a new instance of Subscriber.



10
11
12
13
# File 'lib/createsend/subscriber.rb', line 10

def initialize(list_id, email_address)
  @list_id = list_id
  @email_address = email_address
end

Instance Attribute Details

#email_addressObject (readonly)

Returns the value of attribute email_address.



8
9
10
# File 'lib/createsend/subscriber.rb', line 8

def email_address
  @email_address
end

#list_idObject (readonly)

Returns the value of attribute list_id.



7
8
9
# File 'lib/createsend/subscriber.rb', line 7

def list_id
  @list_id
end

Class Method Details

.add(list_id, email_address, name, custom_fields, resubscribe, restart_subscription_based_autoresponders = false) ⇒ Object

Adds a subscriber to a subscriber list.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/createsend/subscriber.rb', line 23

def self.add(list_id, email_address, name, custom_fields, resubscribe,
  restart_subscription_based_autoresponders=false)
  options = { :body => {
    :EmailAddress => email_address,
    :Name => name,
    :CustomFields => custom_fields,
    :Resubscribe => resubscribe,
    :RestartSubscriptionBasedAutoresponders =>
      restart_subscription_based_autoresponders }.to_json }
  response = CreateSend.post "/subscribers/#{list_id}.json", options
  response.parsed_response
end

.get(list_id, email_address) ⇒ Object

Gets a subscriber by list ID and email address.



16
17
18
19
20
# File 'lib/createsend/subscriber.rb', line 16

def self.get(list_id, email_address)
  options = { :query => { :email => email_address } }
  response = CreateSend.get "/subscribers/#{list_id}.json", options
  Hashie::Mash.new(response)
end

.import(list_id, subscribers, resubscribe, queue_subscription_based_autoresponders = false, restart_subscription_based_autoresponders = false) ⇒ Object

Imports subscribers into a subscriber list.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/createsend/subscriber.rb', line 37

def self.import(list_id, subscribers, resubscribe,
  queue_subscription_based_autoresponders=false,
  restart_subscription_based_autoresponders=false)
  options = { :body => {
    :Subscribers => subscribers,
    :Resubscribe => resubscribe,
    :QueueSubscriptionBasedAutoresponders =>
      queue_subscription_based_autoresponders,
    :RestartSubscriptionBasedAutoresponders =>
      restart_subscription_based_autoresponders }.to_json }
  begin
    response = CreateSend.post(
      "/subscribers/#{list_id}/import.json", options)
  rescue BadRequest => br
    # Subscriber import will throw BadRequest if some subscribers are not
    # imported successfully. If this occurs, we want to return the
    # ResultData property of the BadRequest exception (which is of the
    # same "form" as the response we would receive upon a completely
    # successful import).
    if br.data.ResultData
      return br.data.ResultData
    else
      raise br
    end
  end
  Hashie::Mash.new(response)
end

Instance Method Details

#deleteObject

Moves this subscriber to the Deleted state in the associated list.



98
99
100
101
# File 'lib/createsend/subscriber.rb', line 98

def delete
  options = { :query => { :email => @email_address } }
  CreateSend.delete "/subscribers/#{@list_id}.json", options
end

#historyObject

Gets the historical record of this subscriber’s trackable actions.



91
92
93
94
95
# File 'lib/createsend/subscriber.rb', line 91

def history
  options = { :query => { :email => @email_address } }
  response = CreateSend.get "/subscribers/#{@list_id}/history.json", options
  response.map{|item| Hashie::Mash.new(item)}
end

#unsubscribeObject

Unsubscribes this subscriber from the associated list.



84
85
86
87
88
# File 'lib/createsend/subscriber.rb', line 84

def unsubscribe
  options = { :body => {
    :EmailAddress => @email_address }.to_json }
  CreateSend.post "/subscribers/#{@list_id}/unsubscribe.json", options
end

#update(new_email_address, name, custom_fields, resubscribe, restart_subscription_based_autoresponders = false) ⇒ Object

Updates any aspect of a subscriber, including email address, name, and custom field data if supplied.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/createsend/subscriber.rb', line 67

def update(new_email_address, name, custom_fields, resubscribe,
  restart_subscription_based_autoresponders=false)
  options = {
    :query => { :email => @email_address },
    :body => {
      :EmailAddress => new_email_address,
      :Name => name,
      :CustomFields => custom_fields,
      :Resubscribe => resubscribe,
      :RestartSubscriptionBasedAutoresponders =>
        restart_subscription_based_autoresponders }.to_json }
  CreateSend.put "/subscribers/#{@list_id}.json", options
  # Update @email_address, so this object can continue to be used reliably
  @email_address = new_email_address
end