Skip to content
This repository
Browse code

add whitelist/blacklist and readonly configuration options

  • Loading branch information...
commit 66440fa0e16d39861bb67c575b8a6c0d0c9f20a3 1 parent c4f94f2
authored August 24, 2011
1  lib/exact_target.rb
@@ -74,6 +74,7 @@ def send_to_exact_target(request)
74 74
 
75 75
       data = "qf=xml&xml=#{URI.escape(URI.escape(request), "&")}"
76 76
       uri = URI.parse(configuration.base_url)
  77
+
77 78
       http = net_http_or_proxy.new(uri.host, uri.port)
78 79
       http.use_ssl = configuration.secure?
79 80
       http.open_timeout = configuration.http_open_timeout
13  lib/exact_target/configuration.rb
@@ -2,8 +2,11 @@ module ExactTarget
2 2
   # Used to set up and modify settings for ExactTarget
3 3
   class Configuration
4 4
 
5  
-    OPTIONS = [:base_url, :username, :password,
  5
+    OPTIONS = [:base_url, :username, :password, :readonly, :email_whitelist, :email_blacklist
6 6
                :http_method, :http_open_timeout, :http_read_timeout, :http_proxy].freeze
  7
+    STANDARD_READONLY_CALLS = [:list_add, :list_edit, :list_import, :list_delete,
  8
+                               :subscriber_add, :subscriber_edit, :subscriber_delete, :subscriber_masterunsub,
  9
+                               :email_add, :email_add_text, :job_send].freeze
7 10
 
8 11
     # The (optional) base URL for accessing ExactTarget (can be http or https).
9 12
     # Defaults to 'https://api.dc1.exacttarget.com/integrate.aspx'
@@ -30,11 +33,19 @@ class Configuration
30 33
     # The HTTP method to make the request with
31 34
     attr_accessor :http_method
32 35
 
  36
+    # The (optional) readonly flag (defaults to false)
  37
+    attr_accessor :readonly
  38
+
  39
+    # (optional) limiting triggeredsend email addresses
  40
+    attr_accessor :email_whitelist
  41
+    attr_accessor :email_blacklist
  42
+
33 43
     def initialize
34 44
       @base_url                 = 'https://api.dc1.exacttarget.com/integrate.aspx'
35 45
       @http_open_timeout        = 2
36 46
       @http_read_timeout        = 5
37 47
       @http_method              = "get"
  48
+      @readonly                 = []
38 49
     end
39 50
 
40 51
     def valid?
31  lib/exact_target/error.rb
@@ -13,4 +13,35 @@ def to_s
13 13
     end
14 14
 
15 15
   end
  16
+
  17
+  class EmailAddressError < Error
  18
+    def initialize(email)
  19
+      @email = email
  20
+    end
  21
+  end
  22
+
  23
+  class BlacklistError < EmailAddressError
  24
+    def to_s
  25
+      "#{@email} is on the email address blacklist"
  26
+    end
  27
+  end
  28
+
  29
+  class WhitelistError < EmailAddressError
  30
+    def to_s
  31
+      "#{@email} is not on the email address whitelist"
  32
+    end
  33
+  end
  34
+
  35
+  class ReadonlyError < Error
  36
+    def initialize(name, *args)
  37
+      @name = name
  38
+      @args = args
  39
+    end
  40
+
  41
+    def to_s
  42
+      "#{@name} is not allowed when the client is set to readonly"
  43
+    end
  44
+  end
  45
+
  46
+
16 47
 end
45  lib/exact_target/request_builder.rb
@@ -8,10 +8,12 @@ def initialize(config)
8 8
     end
9 9
 
10 10
     def accountinfo_retrieve_attrbs
  11
+      ensure_executable!("accountinfo_retrieve_attrbs")
11 12
       build(:accountinfo, :retrieve_attrbs)
12 13
     end
13 14
 
14 15
     def list_add(list_name, list_type=nil)
  16
+      ensure_executable!("list_add")
15 17
       list_type = :public unless %w(public private salesforce).include?(list_type.to_s)
16 18
       build(:list, :add) do |li|
17 19
         li.list_type list_type.to_s
@@ -20,12 +22,14 @@ def list_add(list_name, list_type=nil)
20 22
     end
21 23
 
22 24
     def list_edit(list_id, new_list_name)
  25
+      ensure_executable!("list_edit")
23 26
       build(:list, :edit, :listid, list_id) do |li|
24 27
         li.list_name new_list_name.to_s
25 28
       end
26 29
     end
27 30
 
28 31
     def list_retrieve(id_or_name=nil)
  32
+      ensure_executable!("list_retrieve")
29 33
       if id_or_name.is_a?(Fixnum) or id_or_name =~ /^\d+$/
30 34
         build(:list, :retrieve, :listid, id_or_name.to_i)
31 35
       else
@@ -34,6 +38,7 @@ def list_retrieve(id_or_name=nil)
34 38
     end
35 39
 
36 40
     def list_import(list_id, file_name, file_mapping, options={})
  41
+      ensure_executable!("list_import")
37 42
       options = list_import_default_options(options, file_name)
38 43
       build(:list, :import, :listid, list_id) do |li|
39 44
         li.tags_from_options! options, :file_name, :email_address, :file_type, :column_headings
@@ -45,6 +50,7 @@ def list_import(list_id, file_name, file_mapping, options={})
45 50
     end
46 51
 
47 52
     def list_importstatus(import_id)
  53
+      ensure_executable!("list_importstatus")
48 54
       build(
49 55
         :list, :import,
50 56
         :sub_action => :importstatus,
@@ -54,6 +60,7 @@ def list_importstatus(import_id)
54 60
     end
55 61
 
56 62
     def list_retrieve_sub(list_id, status=nil)
  63
+      ensure_executable!("list_retrieve_sub")
57 64
       unless status.nil? or %w(Active Unsubscribed Returned Undeliverable Deleted).include?(status)
58 65
         raise "Invalid status: #{status}"
59 66
       end
@@ -63,14 +70,17 @@ def list_retrieve_sub(list_id, status=nil)
63 70
     end
64 71
 
65 72
     def list_delete(id)
  73
+      ensure_executable!("list_delete")
66 74
       build(:list, :delete, :listid, id)
67 75
     end
68 76
 
69 77
     def list_retrievegroups
  78
+      ensure_executable!("list_retrievegroups")
70 79
       build(:list, :retrievegroups, :groups)
71 80
     end
72 81
 
73 82
     def list_refresh_group(group_id)
  83
+      ensure_executable!("list_refresh_group")
74 84
       build(
75 85
         :list, :refresh_group,
76 86
         :sub_action => nil,
@@ -81,6 +91,7 @@ def list_refresh_group(group_id)
81 91
     end
82 92
 
83 93
     def batch_inquire(batch_id)
  94
+      ensure_executable!("batch_inquire")
84 95
       build(
85 96
         :batch, :inquire, :batchid, batch_id,
86 97
         :sub_action => nil,
@@ -91,10 +102,12 @@ def batch_inquire(batch_id)
91 102
     ###################################################################
92 103
 
93 104
     def subscriber_add(list_id, subscriber, options={})
  105
+      ensure_executable!("subscriber_add")
94 106
       subscriber_edit(list_id, nil, subscriber, options)
95 107
     end
96 108
 
97 109
     def subscriber_edit(list_id, orig_email, subscriber, options={})
  110
+      ensure_executable!("subscriber_edit")
98 111
       options = subscriber_edit_default_options(options)
99 112
       subscriber = subscriber.to_et_hash if subscriber.is_a?(Subscriber)
100 113
       action = orig_email.nil? ? :add : :edit
@@ -112,16 +125,19 @@ def subscriber_edit(list_id, orig_email, subscriber, options={})
112 125
     end
113 126
 
114 127
     def subscriber_retrieve(id, email=nil)
  128
+      ensure_executable!("subscriber_retrieve")
115 129
       type = email.blank? ? :subid : :listid
116 130
       build(:subscriber, :retrieve, type, id, :search_value2 => email)
117 131
     end
118 132
 
119 133
     def subscriber_delete(id, email=nil)
  134
+      ensure_executable!("subscriber_delete")
120 135
       type = email.blank? ? :subid : :listid
121 136
       build(:subscriber, :delete, type, id, :search_value2 => email)
122 137
     end
123 138
 
124 139
     def subscriber_masterunsub(*email_addresses)
  140
+      ensure_executable!("subscriber_masterunsub")
125 141
       build(:subscriber, :masterunsub, :emailaddress, :search_value => :omit) do |sub|
126 142
         sub.search_value do |sv|
127 143
           email_addresses.flatten.each { |a| sv.emailaddress(a) }
@@ -132,6 +148,7 @@ def subscriber_masterunsub(*email_addresses)
132 148
     ###################################################################
133 149
 
134 150
     def email_retrieve(name=nil, options={})
  151
+      ensure_executable!("email_retrieve")
135 152
       name, options = nil, name if name.is_a?(Hash)
136 153
       start_date, end_date = %w(start_date end_date).map do |n|
137 154
         et_date options[n.to_sym]
@@ -150,6 +167,7 @@ def email_retrieve(name=nil, options={})
150 167
     end
151 168
 
152 169
     def email_add(name, subject, options)
  170
+      ensure_executable!("email_add")
153 171
       build(:email, :add, :search_type => :omit, :search_value => :omit, :sub_action => 'HTMLPaste') do |em|
154 172
         em.category
155 173
         em.email_name name
@@ -163,6 +181,7 @@ def email_add(name, subject, options)
163 181
     end
164 182
 
165 183
     def email_add_text(email_id, options)
  184
+      ensure_executable!("email_add_text")
166 185
       build(:email, :add, :search_type => :emailid,
167 186
                   :search_value => email_id, :sub_action => :text) do |em|
168 187
         if options.has_key? :body
@@ -174,6 +193,7 @@ def email_add_text(email_id, options)
174 193
     end
175 194
 
176 195
     def email_retrieve_body(email_id)
  196
+      ensure_executable!("email_retrieve_body")
177 197
       build(:email, :retrieve, :emailid, email_id, :sub_action => :htmlemail) do |em|
178 198
         em.search_value2
179 199
         em.search_value3
@@ -183,6 +203,7 @@ def email_retrieve_body(email_id)
183 203
     ###################################################################
184 204
 
185 205
     def triggeredsend_add(email, customer_key, attributes = {})
  206
+      ensure_executable!("triggeredsend_add", email)
186 207
       build(:triggeredsend, :add) do |xml|
187 208
         xml.TriggeredSend :"xmlns:xsi" => "http://www.w3.org/2001/XMLSchemainstance",
188 209
                           :"xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
@@ -197,7 +218,7 @@ def triggeredsend_add(email, customer_key, attributes = {})
197 218
           attributes.each do |key, value|
198 219
             xml.Attributes do
199 220
               xml.Name key.to_s
200  
-              xml.Value value
  221
+              xml.Value {|v| v.cdata! value}
201 222
             end
202 223
           end
203 224
         end
@@ -207,6 +228,7 @@ def triggeredsend_add(email, customer_key, attributes = {})
207 228
     ###################################################################
208 229
 
209 230
     def job_send(email_id, list_ids, options={})
  231
+      ensure_executable!("job_send")
210 232
       options = job_send_default_options(options)
211 233
 
212 234
       build(:job, :send, :emailid, email_id) do |job|
@@ -274,7 +296,9 @@ def build(system_name, action, search_type=nil, search_value=nil, options=nil, &
274 296
           build_system(s, system_name, action, options, &block)
275 297
         end
276 298
       end
277  
-      %Q[<?xml version="1.0"?>#{xml}]
  299
+x =       %Q[<?xml version="1.0"?>#{xml}]
  300
+pp x
  301
+x
278 302
     end
279 303
 
280 304
     def parse_options(search_type, search_value, options)
@@ -303,5 +327,22 @@ def build_system_option(s, name, value)
303 327
       end
304 328
     end
305 329
 
  330
+    def ensure_executable!(method, email = nil)
  331
+      # stop if the method is readonly
  332
+      raise ReadonlyError.new(method, args) if @config.readonly && @config.readonly.include?(method.to_sym)
  333
+
  334
+      ensure_sendable!(email) if email
  335
+    end
  336
+
  337
+    def ensure_sendable!(email)
  338
+      if @config.email_whitelist && !@config.email_whitelist.match(email)
  339
+        raise WhitelistError.new(email)
  340
+      end
  341
+
  342
+      if @config.email_blacklist && @config.email_blacklist.match(email)
  343
+        raise BlacklistError.new(email)
  344
+      end
  345
+    end
  346
+
306 347
   end
307 348
 end

0 notes on commit 66440fa

Please sign in to comment.
Something went wrong with that request. Please try again.