class ExactTargetRest::TriggeredSend

Public Class Methods

new(authorization, external_key, snake_to_camel: true) click to toggle source

Execute TriggeredSends to one or several subscribers.

@param authorization [Authorization] @param external_key [String] The string that identifies the TriggeredSend @param snake_to_camel [Boolean] Attributes should be converted to CamelCase? (default true)

# File lib/exact_target_rest/triggered_send.rb, line 8
def initialize(authorization, external_key, snake_to_camel: true)
  @authorization = authorization
  @external_key = external_key
  @snake_to_camel = snake_to_camel
end

Public Instance Methods

deliver() click to toggle source

TriggeredSend with loaded attributes.

# File lib/exact_target_rest/triggered_send.rb, line 60
def deliver
  tries ||= 1

  @authorization.with_authorization do |access_token|
    resp = endpoint.post do |p|
      p.url(format(TRIGGERED_SEND_PATH, URI.encode(@external_key)))
      p.headers['Authorization'] = "Bearer #{access_token}"
      p.body = {
        From: {
          Address: @from_address,
          Name: @from_name
        },
        To: {
          Address: @email_address,
          SubscriberKey: @subscriber_key,
          ContactAttributes: {
              SubscriberAttributes: @subscriber_attributes
          }
        },
        OPTIONS: {
          RequestType: @request_type
        }
      }
    end
    raise NotAuthorizedError if resp.status == 401
    resp
  end
rescue NotAuthorizedError
  tries -= 1
  retry if tries >= 0
  raise NotAuthorizedError
end
send_one( email_address:, subscriber_key: email_address, ** data_extension_attributes ) click to toggle source

TriggeredSend for just one subscriber. @param to_address [String] Email to send. @param subscriber_key [String] SubscriberKey (it uses Email if not set). @param data_extension_attributes [{Symbol => Object}] List of attributes (in snake_case)

that will be used in TriggeredSend and will be saved in related DataExtension
(in CamelCase).
# File lib/exact_target_rest/triggered_send.rb, line 20
def send_one(
  email_address:,
  subscriber_key: email_address,
  ** data_extension_attributes
  )
  with_options(
    email_address: email_address,
    subscriber_key: subscriber_key,
    subscriber_attributes: prepare_attributes(data_extension_attributes)
  ).deliver
end
with_options( request_type: "ASYNC", email_address:, subscriber_key: email_address, from_address: "", from_name: "", subscriber_attributes: {} ) click to toggle source

Load attributes and return “self”. To send using async methods. @param request_type [String] ASYNC or SYNC. @param to_address [String] Email to send. @param subscriber_key [String] SubscriberKey.

> it uses Email if not set

@param from_address [String] Sender email address. @param from_name [String] Sender name. @param subscriber_attributes [{String => String, …}] List of attributes

> Keys as Strings (when your ExactTarget's fields doesn't have a pattern)

# File lib/exact_target_rest/triggered_send.rb, line 41
def with_options(
  request_type: "ASYNC",
  email_address:,
  subscriber_key: email_address,
  from_address: "",
  from_name: "",
  subscriber_attributes: {}
  )
  @request_type = request_type
  @email_address = email_address
  @subscriber_key = subscriber_key
  @from_address = from_address
  @from_name = from_name
  @subscriber_attributes = subscriber_attributes

  self
end

Protected Instance Methods

endpoint() click to toggle source
# File lib/exact_target_rest/triggered_send.rb, line 95
def endpoint
  Faraday.new(url: TRIGGERED_SEND_URL) do |f|
    f.request :json
    f.response :json, content_type: /\bjson$/
    f.adapter FARADAY_ADAPTER
  end
end
prepare_attributes(attributes) click to toggle source
# File lib/exact_target_rest/triggered_send.rb, line 103
def prepare_attributes(attributes)
  @snake_to_camel ? attributes.snake_to_camel : attributes
end