sailthru 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sailthru/triggermail_client.rb +50 -50
- data/sailthru.gemspec +1 -1
- metadata +9 -4
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# Special thanks to the iminlikewithyou.com team for the development
|
13
13
|
# of this library.
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# Redistribution and use in source and binary forms, with or without
|
16
16
|
# modification, are permitted provided that the following conditions
|
17
17
|
# are met:
|
@@ -34,12 +34,12 @@
|
|
34
34
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
35
35
|
#
|
36
36
|
################################################################################
|
37
|
-
|
37
|
+
|
38
38
|
require 'net/http'
|
39
39
|
require 'uri'
|
40
40
|
require 'rubygems'
|
41
41
|
require 'json'
|
42
|
-
require 'md5'
|
42
|
+
require 'digest/md5'
|
43
43
|
|
44
44
|
module Sailthru
|
45
45
|
|
@@ -48,21 +48,21 @@ module Sailthru
|
|
48
48
|
|
49
49
|
class TriggermailClient
|
50
50
|
attr_accessor :api_uri, :api_key, :secret, :version, :last_request
|
51
|
-
|
51
|
+
|
52
52
|
VERSION = '1.0'
|
53
53
|
|
54
54
|
# params:
|
55
55
|
# api_key, String
|
56
56
|
# secret, String
|
57
57
|
# api_uri, String
|
58
|
-
#
|
58
|
+
#
|
59
59
|
# Instantiate a new client; constructor optionally takes overrides for key/secret/uri.
|
60
60
|
def initialize(api_key, secret, api_uri)
|
61
61
|
@api_key = api_key
|
62
62
|
@secret = secret
|
63
63
|
@api_uri = api_uri
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
# params:
|
67
67
|
# template_name, String
|
68
68
|
# email, String
|
@@ -83,17 +83,17 @@ module Sailthru
|
|
83
83
|
result = self.api_post(:send, post)
|
84
84
|
end
|
85
85
|
|
86
|
-
|
86
|
+
|
87
87
|
# params:
|
88
88
|
# send_id, Fixnum
|
89
89
|
# returns:
|
90
90
|
# Hash, response data from server
|
91
|
-
#
|
91
|
+
#
|
92
92
|
# Get the status of a send.
|
93
93
|
def get_send(send_id)
|
94
|
-
self.api_get(:send, {:send_id => send_id.to_s})
|
94
|
+
self.api_get(:send, {:send_id => send_id.to_s})
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
# params:
|
98
98
|
# name, String
|
99
99
|
# list, String
|
@@ -120,8 +120,8 @@ module Sailthru
|
|
120
120
|
post[:content_text] = content_text
|
121
121
|
self.api_post(:blast, post)
|
122
122
|
end
|
123
|
-
|
124
|
-
|
123
|
+
|
124
|
+
|
125
125
|
# params:
|
126
126
|
# blast_id, Fixnum
|
127
127
|
# returns:
|
@@ -131,24 +131,24 @@ module Sailthru
|
|
131
131
|
def get_blast(blast_id)
|
132
132
|
self.api_get(:blast, {:blast_id => blast_id.to_s})
|
133
133
|
end
|
134
|
-
|
134
|
+
|
135
135
|
# params:
|
136
136
|
# email, String
|
137
137
|
# returns:
|
138
138
|
# Hash, response data from server
|
139
|
-
#
|
139
|
+
#
|
140
140
|
# Return information about an email address, including replacement vars and lists.
|
141
141
|
def get_email(email)
|
142
142
|
self.api_get(:email, {:email => email})
|
143
143
|
end
|
144
|
-
|
144
|
+
|
145
145
|
# params:
|
146
146
|
# email, String
|
147
147
|
# vars, Hash
|
148
148
|
# lists, Hash mapping list name => 1 for subscribed, 0 for unsubscribed
|
149
149
|
# returns:
|
150
150
|
# Hash, response data from server
|
151
|
-
#
|
151
|
+
#
|
152
152
|
# Set replacement vars and/or list subscriptions for an email address.
|
153
153
|
def set_email(email, vars = {}, lists = {}, templates = {})
|
154
154
|
data = {:email => email}
|
@@ -157,7 +157,7 @@ module Sailthru
|
|
157
157
|
data[:templates] = templates unless templates.empty?
|
158
158
|
self.api_post(:email, data)
|
159
159
|
end
|
160
|
-
|
160
|
+
|
161
161
|
# params:
|
162
162
|
# email, String
|
163
163
|
# password, String
|
@@ -172,33 +172,33 @@ module Sailthru
|
|
172
172
|
data[:names] = 1 if with_names
|
173
173
|
self.api_post(:contacts, data)
|
174
174
|
end
|
175
|
-
|
176
|
-
|
175
|
+
|
176
|
+
|
177
177
|
# params:
|
178
178
|
# template_name, String
|
179
179
|
# returns:
|
180
180
|
# Hash of response data.
|
181
|
-
#
|
181
|
+
#
|
182
182
|
# Get a template.
|
183
183
|
def get_template(template_name)
|
184
184
|
self.api_get(:template, {:template => template_name})
|
185
185
|
end
|
186
|
-
|
187
|
-
|
186
|
+
|
187
|
+
|
188
188
|
# params:
|
189
189
|
# template_name, String
|
190
190
|
# template_fields, Hash
|
191
191
|
# returns:
|
192
192
|
# Hash containg response from the server.
|
193
|
-
#
|
193
|
+
#
|
194
194
|
# Save a template.
|
195
195
|
def save_template(template_name, template_fields)
|
196
196
|
data = template_fields
|
197
197
|
data[:template] = template_name
|
198
198
|
self.api_post(:template, data)
|
199
199
|
end
|
200
|
-
|
201
|
-
|
200
|
+
|
201
|
+
|
202
202
|
# params:
|
203
203
|
# params, Hash
|
204
204
|
# request, String
|
@@ -207,57 +207,57 @@ module Sailthru
|
|
207
207
|
def receive_verify_post(params, request)
|
208
208
|
if request.post?
|
209
209
|
[:action, :email, :send_id, :sig].each { |key| return false unless params.has_key?(key) }
|
210
|
-
|
210
|
+
|
211
211
|
return false unless params[:action] == :verify
|
212
|
-
|
212
|
+
|
213
213
|
sig = params[:sig]
|
214
214
|
params.delete(:sig)
|
215
215
|
return false unless sig == TriggermailClient.get_signature_hash(params, @secret)
|
216
|
-
|
216
|
+
|
217
217
|
_send = self.get_send(params[:send_id])
|
218
218
|
return false unless _send.has_key?(:email)
|
219
|
-
|
219
|
+
|
220
220
|
return false unless _send[:email] == params[:email]
|
221
|
-
|
221
|
+
|
222
222
|
return true
|
223
223
|
else
|
224
224
|
return false
|
225
225
|
end
|
226
226
|
end
|
227
|
-
|
228
|
-
|
227
|
+
|
228
|
+
|
229
229
|
# Perform API GET request
|
230
230
|
def api_get(action, data)
|
231
231
|
api_request(action, data, 'GET')
|
232
232
|
end
|
233
|
-
|
233
|
+
|
234
234
|
# Perform API POST request
|
235
235
|
def api_post(action, data)
|
236
236
|
api_request(action, data, 'POST')
|
237
237
|
end
|
238
|
-
|
238
|
+
|
239
239
|
# params:
|
240
240
|
# action, String
|
241
241
|
# data, Hash
|
242
242
|
# request, String "GET" or "POST"
|
243
243
|
# returns:
|
244
244
|
# Hash
|
245
|
-
#
|
245
|
+
#
|
246
246
|
# Perform an API request, using the shared-secret auth hash.
|
247
|
-
#
|
247
|
+
#
|
248
248
|
def api_request(action, data, request_type)
|
249
249
|
data[:api_key] = @api_key
|
250
250
|
data[:format] ||= 'json'
|
251
251
|
data[:sig] = Sailthru::TriggermailClient.get_signature_hash(data, @secret)
|
252
252
|
_result = self.http_request("#{@api_uri}/#{action}", data, request_type)
|
253
|
-
|
254
|
-
|
253
|
+
|
254
|
+
|
255
255
|
# NOTE: don't do the unserialize here
|
256
256
|
unserialized = JSON.parse(_result)
|
257
257
|
return unserialized ? unserialized : _result
|
258
258
|
end
|
259
|
-
|
260
|
-
|
259
|
+
|
260
|
+
|
261
261
|
# params:
|
262
262
|
# uri, String
|
263
263
|
# data, Hash
|
@@ -272,10 +272,10 @@ module Sailthru
|
|
272
272
|
else
|
273
273
|
uri += "?" + data.map{ |key, value| "#{key}=#{value}" }.join("&")
|
274
274
|
end
|
275
|
-
|
275
|
+
|
276
276
|
req = nil
|
277
277
|
headers = {"User-Agent" => "Triggermail API Ruby Client #{VERSION}"}
|
278
|
-
|
278
|
+
|
279
279
|
_uri = URI.parse(uri)
|
280
280
|
if method == 'POST'
|
281
281
|
req = Net::HTTP::Post.new(_uri.path, headers)
|
@@ -283,7 +283,7 @@ module Sailthru
|
|
283
283
|
else
|
284
284
|
req = Net::HTTP::Get.new("#{_uri.path}?#{_uri.query}", headers)
|
285
285
|
end
|
286
|
-
|
286
|
+
|
287
287
|
@last_request = req
|
288
288
|
begin
|
289
289
|
response = Net::HTTP.start(_uri.host, _uri.port) {|http|
|
@@ -292,15 +292,15 @@ module Sailthru
|
|
292
292
|
rescue Exception => e
|
293
293
|
raise Sailthru::TriggermailClientException.new("Unable to open stream: #{_uri.to_s}");
|
294
294
|
end
|
295
|
-
|
295
|
+
|
296
296
|
if response.body
|
297
297
|
return response.body
|
298
298
|
else
|
299
299
|
raise Sailthru::TriggermailClientException.new("No response received from stream: #{_uri.to_s}")
|
300
300
|
end
|
301
|
-
|
301
|
+
|
302
302
|
end
|
303
|
-
|
303
|
+
|
304
304
|
# Flatten nested hash for GET / POST request.
|
305
305
|
def flatten_nested_hash(hash, brackets = true)
|
306
306
|
f = {}
|
@@ -335,8 +335,8 @@ module Sailthru
|
|
335
335
|
end
|
336
336
|
return values
|
337
337
|
end
|
338
|
-
|
339
|
-
|
338
|
+
|
339
|
+
|
340
340
|
# params:
|
341
341
|
# params, Hash
|
342
342
|
# secret, String
|
@@ -346,6 +346,6 @@ module Sailthru
|
|
346
346
|
string = secret + self.extract_param_values(params).sort_by{|x| x.to_s}.join("")
|
347
347
|
MD5.md5(string) # debuggin
|
348
348
|
end
|
349
|
-
|
349
|
+
|
350
350
|
end
|
351
|
-
end
|
351
|
+
end
|
data/sailthru.gemspec
CHANGED
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sailthru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 1
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
9
|
+
- 1
|
10
|
+
version: 1.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Ian Enders
|
@@ -43,23 +44,27 @@ rdoc_options:
|
|
43
44
|
require_paths:
|
44
45
|
- lib
|
45
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
46
48
|
requirements:
|
47
49
|
- - ">="
|
48
50
|
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
49
52
|
segments:
|
50
53
|
- 0
|
51
54
|
version: "0"
|
52
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
53
57
|
requirements:
|
54
58
|
- - ">="
|
55
59
|
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
56
61
|
segments:
|
57
62
|
- 0
|
58
63
|
version: "0"
|
59
64
|
requirements: []
|
60
65
|
|
61
66
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.
|
67
|
+
rubygems_version: 1.4.2
|
63
68
|
signing_key:
|
64
69
|
specification_version: 3
|
65
70
|
summary: Gemified Sailthru Client Library
|