class CreateSend::Subscriber

Represents a subscriber and associated functionality.

Attributes

email_address[R]
list_id[R]

Public Class Methods

add(auth, list_id, email_address, name, custom_fields, resubscribe, consent_to_track, restart_subscription_based_autoresponders=false) click to toggle source

Adds a subscriber to a subscriber list.

# 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)
  options = { :body => {
    :EmailAddress => email_address,
    :Name => name,
    :CustomFields => custom_fields,
    :Resubscribe => resubscribe,
    :RestartSubscriptionBasedAutoresponders => 
      restart_subscription_based_autoresponders,
    :ConsentToTrack => consent_to_track }.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) click to toggle source

Gets a subscriber by list ID and email address.

# 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) click to toggle source

Imports subscribers into a subscriber list.

# File lib/createsend/subscriber.rb, line 41
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
new(auth, list_id, email_address) click to toggle source
Calls superclass method
# File lib/createsend/subscriber.rb, line 7
def initialize(auth, list_id, email_address)
  @list_id = list_id
  @email_address = email_address
  super
end

Public Instance Methods

delete() click to toggle source

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

Calls superclass method
# File lib/createsend/subscriber.rb, line 104
def delete
  options = { :query => { :email => @email_address } }
  super "/subscribers/#{@list_id}.json", options
end
history() click to toggle source

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

# File lib/createsend/subscriber.rb, line 97
def history
  options = { :query => { :email => @email_address } }
  response = cs_get "/subscribers/#{@list_id}/history.json", options
  response.map{|item| Hashie::Mash.new(item)}
end
unsubscribe() click to toggle source

Unsubscribes this subscriber from the associated list.

# File lib/createsend/subscriber.rb, line 90
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) click to toggle source

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

# File lib/createsend/subscriber.rb, line 72
def update(new_email_address, name, custom_fields, resubscribe, 
  consent_to_track, 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,
      :ConsentToTrack => consent_to_track }.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