Class: Exactly::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/exactly/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



28
29
30
31
# File 'lib/exactly/client.rb', line 28

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#clientObject



33
34
35
36
37
38
# File 'lib/exactly/client.rb', line 33

def client
  @client ||= ::Savon.client(:wsse_auth => [@username,@password]) do
    wsdl "https://webservice.s6.exacttarget.com/etframework.wsdl"
    namespace "http://exacttarget.com/wsdl/partnerAPI"
  end
end

#delete_from_data_extension(customer_key, properties) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/exactly/client.rb', line 126

def delete_from_data_extension(customer_key, properties)
  response = client.call(
    :delete,
    :soap_action => "Delete",
    :message => {
      "DeleteOptions" => {},
      "Objects" => {
        "CustomerKey" => customer_key,
        "Keys" => {
          "Key" => properties.map do |k, v|
            { "Name" => k, "Value" => v }
          end
        }
      },
      :attributes! => { "Objects" => { "xsi:type" => "tns:DataExtensionObject" }}
    })
  if response.to_hash[:delete_response][:overall_status] != 'OK'
    raise Exactly::TriggeredSendFailed.new(response)
  end
end

#triggered_send(customer_key, attributes) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/exactly/client.rb', line 147

def triggered_send(customer_key, attributes)
  attributes_without_email = attributes.reject{|k,v| k == :email}
  response = client.call(
    :create,
    :soap_action => "Create",
    :message => {
      "Objects" => {
        "TriggeredSendDefinition" => {
          "CustomerKey" => customer_key
        },
        "Subscribers" => {
          "EmailAddress"  => attributes[:email],
          "SubscriberKey" => attributes[:subscriber_key],
          "Attributes"    => attributes_without_email.map do
            |k, v| { "Name" => k, "Value" => v }
          end
        }
      },
      :attributes! => { "Objects" => { "xsi:type" => "tns:TriggeredSend" }}
    })
  if response.to_hash[:create_response][:overall_status] != 'OK'
    raise Exactly::TriggeredSendFailed.new(response)
  end
end

#unsubscribe_subscriber(customer_key, email, lists = []) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/exactly/client.rb', line 69

def unsubscribe_subscriber(customer_key, email, lists = [])
  response = client.call(
    :create,
    :soap_action => "Create",
    :message => {
      "Options" => {
        "SaveOptions" => [
          "SaveOption" => {
            "PropertyName" => "*",
            "SaveAction" => "UpdateAdd"
          }
        ]
      },
      "Objects" => {
        "CustomerKey" => customer_key,
        "EmailAddress" => email,
        "Lists" => Array(lists).map{|list_id|
          { "ID" => list_id, "Status" => "Unsubscribed" }
        }
      },
      :attributes! => {"Objects" =>{ "xsi:type" => "tns:Subscriber" }}
    })
  if response.to_hash[:create_response][:overall_status] != 'OK'
    raise Exactly::UpsertSubscriberFailed.new(response)
  end
rescue Savon::SOAPFault => ex
  raise Exactly::SoapFaultError, "Error: Could not upsert subscriber #{customer_key}/#{email} to ExactTarget: #{ex.message}"
end

#upsert_data_extension(customer_key, properties) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/exactly/client.rb', line 98

def upsert_data_extension(customer_key, properties)
  response = client.call(
    :update,
    :soap_action => "Update",
    :message => {
      "Options" => {
        "SaveOptions" => [
          "SaveOption" => {
            "PropertyName" => "*",
            "SaveAction" => "UpdateAdd"
          }
        ]
      },
      "Objects" => {
        "CustomerKey" => customer_key,
        "Properties" => {
          "Property" => properties.map do |k, v|
            { "Name" => k, "Value" => v }
          end
        }
      },
      :attributes! => { "Objects" => { "xsi:type" => "tns:DataExtensionObject" }}
    })
  if response.to_hash[:update_response][:overall_status] != 'OK'
    raise Exactly::UpsertDataExtensionFailed.new(response)
  end
end

#upsert_subscriber(customer_key, email, lists = []) ⇒ Object



40
41
42
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
# File 'lib/exactly/client.rb', line 40

def upsert_subscriber(customer_key, email, lists = [])
  response = client.call(
    :create,
    :soap_action => "Create",
    :message => {
      "Options" => {
        "SaveOptions" => [
          "SaveOption" => {
            "PropertyName" => "*",
            "SaveAction" => "UpdateAdd"
          }
        ]
      },
      "Objects" => {
        "CustomerKey" => customer_key,
        "EmailAddress" => email,
        "Lists" => Array(lists).map{|list_id|
          { "ID" => list_id, "Status" => "Active" }
        }
      },
      :attributes! => {"Objects" =>{ "xsi:type" => "tns:Subscriber" }}
    })
  if response.to_hash[:create_response][:overall_status] != 'OK'
    raise Exactly::UpsertSubscriberFailed.new(response)
  end
rescue Savon::SOAPFault => ex
  raise Exactly::SoapFaultError, "Error: Could not upsert subscriber #{customer_key}/#{email} to ExactTarget: #{ex.message}"
end