Class: ExactTargetRest::TriggeredSend

Inherits:
Object
  • Object
show all
Defined in:
lib/exact_target_rest/triggered_send.rb

Instance Method Summary collapse

Constructor Details

#initialize(authorization, external_key, snake_to_camel: true) ⇒ TriggeredSend

Execute TriggeredSends to one or several subscribers.

Parameters:

  • authorization (Authorization)
  • external_key (String)

    The string that identifies the TriggeredSend

  • snake_to_camel (Boolean) (defaults to: true)

    Attributes should be converted to CamelCase? (default true)



8
9
10
11
12
# 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

Instance Method Details

#deliverObject

TriggeredSend with loaded attributes.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

TriggeredSend for just one subscriber.

Parameters:

  • to_address (String)

    Email to send.

  • subscriber_key (String) (defaults to: email_address)

    SubscriberKey (it uses Email if not set).

  • 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).



20
21
22
23
24
25
26
27
28
29
30
# 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: {}) ⇒ Object

Load attributes and return “self”. To send using async methods.

> it uses Email if not set

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

Parameters:

  • request_type (String) (defaults to: "ASYNC")

    ASYNC or SYNC.

  • to_address (String)

    Email to send.

  • subscriber_key (String) (defaults to: email_address)

    SubscriberKey.

  • from_address (String) (defaults to: "")

    Sender email address.

  • from_name (String) (defaults to: "")

    Sender name.

  • subscriber_attributes ({String => String, ...}) (defaults to: {})

    List of attributes



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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