Class: CreateSend::Subscriber

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

Overview

Represents a subscriber and associated functionality.

Instance Attribute Summary collapse

Attributes inherited from CreateSend

#auth_details

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CreateSend

#add_auth_details_to_options, #administrators, #auth, authorize_url, #billing_details, #clients, #countries, exchange_token, #external_session_url, #get, #get_primary_contact, #handle_response, #post, #put, refresh_access_token, #refresh_token, #set_primary_contact, #systemdate, #timezones, user_agent

Constructor Details

#initialize(auth, list_id, email_address) ⇒ Subscriber

Returns a new instance of Subscriber.



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

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

Instance Attribute Details

#email_addressObject (readonly)

Returns the value of attribute email_address.



5
6
7
# File 'lib/createsend/subscriber.rb', line 5

def email_address
  @email_address
end

#list_idObject (readonly)

Returns the value of attribute list_id.



4
5
6
# File 'lib/createsend/subscriber.rb', line 4

def list_id
  @list_id
end

Class Method Details

.add(auth, list_id, email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders = false, mobile_number = nil, consent_to_send_sms = nil) ⇒ Object

Adds a subscriber to a subscriber list.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/createsend/subscriber.rb', line 25

def self.add(auth, list_id, email_address, name, custom_fields, resubscribe, 
  consent_to_track, restart_subscription_based_autoresponders=false, mobile_number=nil, consent_to_send_sms=nil)
  options = { :body => {
    :EmailAddress => email_address,
    :Name => name,
    :MobileNumber => mobile_number,
    :CustomFields => custom_fields,
    :Resubscribe => resubscribe,
    :RestartSubscriptionBasedAutoresponders => 
      restart_subscription_based_autoresponders,
    :ConsentToTrack => consent_to_track,
    :ConsentToSendSms => consent_to_send_sms }.to_json }
  cs = CreateSend.new auth
  response = cs.post "/subscribers/#{list_id}.json", options
  response.parsed_response
end

.get(auth, list_id, email_address, include_tracking_preference = false) ⇒ Object

Gets a subscriber by list ID and email address.



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

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

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

Imports subscribers into a subscriber list.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/createsend/subscriber.rb', line 43

def self.import(auth, 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
    cs = CreateSend.new auth
    response = cs.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 # Just raise other Bad Request errors
    end
  end
  Hashie::Mash.new(response)
end

Instance Method Details

#deleteObject

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



108
109
110
111
# File 'lib/createsend/subscriber.rb', line 108

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

#historyObject

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



101
102
103
104
105
# File 'lib/createsend/subscriber.rb', line 101

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

#unsubscribeObject

Unsubscribes this subscriber from the associated list.



94
95
96
97
98
# File 'lib/createsend/subscriber.rb', line 94

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

#update(new_email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders = false, mobile_number = nil, consent_to_send_sms = nil) ⇒ Object

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/createsend/subscriber.rb', line 74

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