Class: Exactly::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client



26
27
28
# File 'lib/exactly/client.rb', line 26

def initialize(username, password)
  client.wsse.credentials username, password
end

Instance Method Details

#clientObject



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

def client
  @client ||= ::Savon::Client.new("https://webservice.s6.exacttarget.com/etframework.wsdl")
end

#delete_from_data_extension(customer_key, properties) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/exactly/client.rb', line 91

def delete_from_data_extension(customer_key, properties)
  client.request "DeleteRequest", :xmlns => "http://exacttarget.com/wsdl/partnerAPI" do
    http.headers['SOAPAction'] = 'Delete'
    soap.body = {
      "DeleteOptions" => {},
      "Objects" => {
        "CustomerKey" => customer_key,
        "Keys" => {
          "Key" => properties.map do
            |k, v| { "Name" => k, "Value" => v }
          end
        }
      },
      :attributes! => { "Objects" => { "xsi:type" => "DataExtensionObject" }}
    }
  end
end

#triggered_send(customer_key, attributes) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/exactly/client.rb', line 109

def triggered_send(customer_key, attributes)
  attributes_without_email = attributes.reject{|k,v| k == :email}
  response = client.request "CreateRequest", :xmlns => "http://exacttarget.com/wsdl/partnerAPI" do
    http.headers['SOAPAction'] = 'Create'
    soap.body = {
      "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" => "TriggeredSend" }}
    }
  end
  if response.to_hash[:create_response][:overall_status] != 'OK'
    raise Exactly::TriggeredSendFailed.new(response)
  end
end

#upsert_data_extension(customer_key, properties) ⇒ Object



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

def upsert_data_extension(customer_key, properties)
  response = client.request "UpdateRequest", :xmlns => "http://exacttarget.com/wsdl/partnerAPI" do
    http.headers['SOAPAction'] = 'Update'
    soap.body = {
      "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" => "DataExtensionObject" }}
    }
  end
  if response.to_hash[:update_response][:overall_status] != 'OK'
    raise Exactly::UpsertDataExtensionFailed.new(response)
  end
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/exactly/client.rb', line 34

def upsert_subscriber(customer_key, email, lists = [])
  response = client.request "CreateRequest", :xmlns => "http://exacttarget.com/wsdl/partnerAPI" do
    http.headers['SOAPAction'] = 'Create'
    body = {
      "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" => "Subscriber" }}
    }

    soap.body = body
  end
  if response.to_hash[:create_response][:overall_status] != 'OK'
    raise Exactly::UpsertSubscriberFailed.new(response)
  end
end