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) ⇒ Object

Adds a subscriber to a subscriber list.



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

def self.add(list_id, email_address, name, custom_fields, resubscribe)
  options = { :body => {
    :EmailAddress => email_address,
    :Name => name,
    :CustomFields => custom_fields,
    :Resubscribe => resubscribe }.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) ⇒ Object

Imports subscribers into a subscriber list.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/createsend/subscriber.rb', line 34

def self.import(list_id, subscribers, resubscribe)
  options = { :body => {
    :Subscribers => subscribers,
    :Resubscribe => resubscribe }.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'd 
    # 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

#historyObject

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



77
78
79
80
81
# File 'lib/createsend/subscriber.rb', line 77

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.



70
71
72
73
74
# File 'lib/createsend/subscriber.rb', line 70

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) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/createsend/subscriber.rb', line 56

def update(new_email_address, name, custom_fields, resubscribe)
  options = {
    :query => { :email => @email_address },
    :body => {
      :EmailAddress => new_email_address,
      :Name => name,
      :CustomFields => custom_fields,
      :Resubscribe => resubscribe }.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