sailthru-client 1.02 → 1.03

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +178 -2
  2. data/lib/sailthru.rb +260 -13
  3. metadata +4 -4
data/README.md CHANGED
@@ -1,4 +1,180 @@
1
- # A simple client library to remotely access the Sailthru REST API.
1
+ sailthru-ruby-client
2
+ ====================
2
3
 
3
- ## Installing from rubygems.org (Tested with Ruby 1.8.7)
4
+ A simple client library to remotely access the `Sailthru REST API` as per [http://docs.sailthru.com/api](http://docs.sailthru.com/api)
5
+
6
+ It can make requests to following [API calls](http://docs.sailthru.com/api):
7
+
8
+ * [email](http://docs.sailthru.com/api/email)
9
+ * [send](http://docs.sailthru.com/api/send)
10
+ * [blast](http://docs.sailthru.com/api/blast)
11
+ * [template](http://docs.sailthru.com/api/template)
12
+ * [list](http://docs.sailthru.com/api/list)
13
+ * [contacts](http://docs.sailthru.com/api/contacts)
14
+ * [content](http://docs.sailthru.com/api/content)
15
+ * [alert](http://docs.sailthru.com/api/alert)
16
+ * [stats](http://docs.sailthru.com/api/stats)
17
+ * [purchase](http://docs.sailthru.com/api/purchase)
18
+ * [horizon](http://docs.sailthru.com/api/horizon)
19
+
20
+ ### Installing from rubygems.org (Tested with Ruby 1.8.7)
4
21
  $ gem install sailthru-client
22
+
23
+ Examples
24
+ --------
25
+ require 'lib/sailthru'
26
+
27
+ api_key = "api_key";
28
+ api_secret = 'secret';
29
+ api_url = "https://api.sailthru.com";
30
+ sailthru = Sailthru::SailthruClient.new(api_key, api_secret, api_url)
31
+
32
+ ### [send](http://docs.sailthru.com/api/send)
33
+
34
+ #send
35
+ template_name = 'my-template'
36
+ email = 'praj@sailthru.com'
37
+ vars = {'name' => 'Prajwal Tuladhar', "myvar" => [1111,2,3]}
38
+ options = {'test' => 1}
39
+ schedule_time = '+3 hours'
40
+ response = sailthru.send(template_name, email, vars, options, schedule_time)
41
+
42
+ #get send
43
+ send_id = '6363'
44
+ response = sailthru.get_send(send_id)
45
+
46
+ #cancel send
47
+ send_id = '236236sbs'
48
+ response = sailthru.cancel_send(send_id)
49
+
50
+ #multi send
51
+ template_name = 'my-template'
52
+ emails = 'praj@sailthru.com, ian@sailthru.com'
53
+ vars = {'name' => 'Prajwal Tuladhar', "myvar" => [1111,2,3]}
54
+ options = {'test' => 1}
55
+ response = sailthru.multi_send(template_name, emails, vars, options)
56
+
57
+ ### [email](http://docs.sailthru.com/api/email)
58
+
59
+ #get email
60
+ email = 'praj@sailthru.com'
61
+ response = sailthru.get_email(email)
62
+
63
+ #set email
64
+ email = 'praj@sailthru.com'
65
+ response = sailthru.set_email(email)
66
+
67
+ ### [blast](http://docs.sailthru.com/api/blast)
68
+
69
+ # schedule blast
70
+ blast_name = 'My blast name'
71
+ template = 'my-template'
72
+ schedule_time = '+5 hours'
73
+ from_name = 'prajwal tuladhar'
74
+ from_email = 'praj@sailthru.com'
75
+ subject = 'What's up!'
76
+ html = '<p>Lorem ispum is great</p>'
77
+ text = 'Lorem ispum is great'
78
+ response = sailthru.schedule_blast(blast_name, template, schedule_time, from_name, from_email, subject, html, text)
79
+
80
+ #update blast
81
+ blast_id = 7886
82
+ name = 'prajwal tuladhar 64'
83
+ response = sailthru.update_blast(blast_id, name = name)
84
+
85
+ #get blast info
86
+ blast_id = 7886
87
+ response = sailthru.get_blast(blast_id)
88
+
89
+ #cancel blast
90
+ blast_id = 7886
91
+ response = sailthru.cancel_blast(blast_id)
92
+
93
+ #delete blast
94
+ blast_id = 7886
95
+ response = sailthru.delete_blast(blast_id)
96
+
97
+
98
+ ### [list](http://docs.sailthru.com/api/list)
99
+
100
+ #save list
101
+ list_name = 'my-list'
102
+ emails = ['praj@sailthru.com', 'ian@sailthru.com']
103
+ response = sailthru.save_list(list_name, emails)
104
+
105
+ #download /get list
106
+ list_name = 'my-list'
107
+ format = 'json'
108
+ response = sailthru.get_list(list_name, format)
109
+
110
+ #delete list
111
+ list_name = 'my-list'
112
+ response = sailthru.delete_list(list_name)
113
+
114
+ ### [contacts](http://docs.sailthru.com/api/contacts)
115
+
116
+ #import contacts
117
+ email = 'infynyxx@aol.com'
118
+ password = 'my super awesome password'
119
+ with_names = true
120
+ response = sailthru.import_contacts(email, password, with_names)
121
+
122
+ ### [content](http://docs.sailthru.com/api/content)
123
+
124
+ #push content
125
+ title = 'hello world'
126
+ url = 'http://example.com/product-url'
127
+ response = sailthru.push_content(title, url)
128
+
129
+ #another push content exammple
130
+ title = 'hello world'
131
+ url = 'http://example.com/product-url'
132
+ tags = ["blue", "red", "green"]
133
+ vars = {'vars' => ['price' => 17299]}
134
+ date = nil
135
+ response = sailthru.push_content(title, url, date, tags = tags, vars = vars)
136
+
137
+ ### [alert](http://docs.sailthru.com/api/alert)
138
+
139
+ #get alert info
140
+ email = 'praj@sailthru.com'
141
+ response = sailthru.get_alert(email)
142
+
143
+ #save alert
144
+ email = 'praj@sailthru.com'
145
+ type = 'daily'
146
+ _when = '+5 hours'
147
+ extras = {'tags' => ['red', 'blue'], 'match' => {'type' => 'yellow'}}
148
+ response = sailthru.save_alert(email, type, _when, extras)
149
+
150
+ #delete alert
151
+ email = 'praj@sailthru.com'
152
+ alert_id = '4d4b17a36763d930210007ba'
153
+ response = sailthru.delete_alert(email, alert_id)
154
+
155
+ ### [purchase](http://docs.sailthru.com/api/purchase)
156
+
157
+ #purchase API call
158
+ email = 'praj@sailthru.com'
159
+ items = [{"price"=>1099, "qty"=>22, "title"=>"High-Impact Water Bottle", "url"=>"http://example.com/234/high-impact-water-bottle", "id"=>"234"}, {"price"=>500, "qty"=>2, "title"=>"Lorem Ispum", "url"=>"http://example.com/2304/lorem-ispum", "id"=>"2304"}]
160
+ response = sailthru.purchase(email, items)
161
+
162
+ ### [stats](http://docs.sailthru.com/api/stats)
163
+
164
+ #stats list
165
+ response = sailthru.stats_list()
166
+
167
+ #stats blast
168
+ blast_id = 42382
169
+ response = sailthru.stats_blast(blast_id)
170
+
171
+ ### [horizon](http://docs.sailthru.com/api/horizon)
172
+
173
+ #get horizon user info
174
+ email = 'praj@sailthru.com'
175
+ response = sailthru.get_horizon(email)
176
+
177
+ #set horizon data
178
+ email = 'praj@sailthru.com'
179
+ tags = ['red', 'blue']
180
+ response = sailthru.set_horizon(email, tags)
data/lib/sailthru.rb CHANGED
@@ -7,7 +7,7 @@ require 'md5'
7
7
 
8
8
  module Sailthru
9
9
 
10
- Version = VERSION = '1.02'
10
+ Version = VERSION = '1.03'
11
11
 
12
12
  class SailthruClientException < Exception
13
13
  end
@@ -133,7 +133,7 @@ module Sailthru
133
133
  if vars.length > 0
134
134
  post[:vars] = vars
135
135
  end
136
-
136
+
137
137
  if schedule_time != nil
138
138
  post[:schedule_time] = schedule_time
139
139
  end
@@ -169,7 +169,7 @@ module Sailthru
169
169
  self.api_get(:send, {:send_id => send_id.to_s})
170
170
  end
171
171
 
172
-
172
+
173
173
  def cancel_send(send_id)
174
174
  self.api_delete(:send, {:send_id => send_id.to_s})
175
175
  end
@@ -198,18 +198,78 @@ module Sailthru
198
198
  post[:subject] = subject
199
199
  post[:content_html] = content_html
200
200
  post[:content_text] = content_text
201
- self.api_post(:blast, post)
201
+ api_post(:blast, post)
202
+ end
203
+
204
+
205
+ # params
206
+ # blast_id, Fixnum | String
207
+ # name, String
208
+ # list, String
209
+ # schedule_time, String
210
+ # from_name, String
211
+ # from_email, String
212
+ # subject, String
213
+ # content_html, String
214
+ # content_text, String
215
+ # options, hash
216
+ #
217
+ # updates existing blast
218
+ def update_blast(blast_id, name = nil, list = nil, schedule_time = nil, from_name = nil, from_email = nil, subject = nil, content_html = nil, content_text = nil, options = {})
219
+ data = options ? options : {}
220
+ data[:blast_id] = blast_id
221
+ if name != nil
222
+ data[:name] = name
223
+ end
224
+ if list != nil
225
+ data[:list] = list
226
+ end
227
+ if schedule_time != nil
228
+ data[:schedule_time] = schedule_time
229
+ end
230
+ if from_name != nil
231
+ data[:from_name] = from_name
232
+ end
233
+ if from_email != nil
234
+ data[:from_email] = from_email
235
+ end
236
+ if subject != nil
237
+ data[:subject] = subject
238
+ end
239
+ if content_html != nil
240
+ data[:content_html] = content_html
241
+ end
242
+ if content_text != nil
243
+ data[:content_text] = content_text
244
+ end
245
+ api_post(:blast, data)
202
246
  end
203
247
 
204
248
 
205
249
  # params:
206
- # blast_id, Fixnum
250
+ # blast_id, Fixnum | String
207
251
  # returns:
208
252
  # Hash, response data from server
209
253
  #
210
254
  # Get information on a previously scheduled email blast
211
255
  def get_blast(blast_id)
212
- self.api_get(:blast, {:blast_id => blast_id.to_s})
256
+ api_get(:blast, {:blast_id => blast_id.to_s})
257
+ end
258
+
259
+ # params:
260
+ # blast_id, Fixnum | String
261
+ #
262
+ # Cancel a scheduled Blast
263
+ def cancel_blast(blast_id)
264
+ api_post(:blast, {:blast_id => blast_id, :schedule_time => ''})
265
+ end
266
+
267
+ # params:
268
+ # blast_id, Fixnum | String
269
+ #
270
+ # Delete a Blast
271
+ def delete_blast(blast_id)
272
+ api_delete(:blast, {:blast_id => blast_id})
213
273
  end
214
274
 
215
275
  # params:
@@ -219,7 +279,7 @@ module Sailthru
219
279
  #
220
280
  # Return information about an email address, including replacement vars and lists.
221
281
  def get_email(email)
222
- self.api_get(:email, {:email => email})
282
+ api_get(:email, {:email => email})
223
283
  end
224
284
 
225
285
  # params:
@@ -317,7 +377,7 @@ module Sailthru
317
377
  def purchase(email, items, incomplete = nil, message_id = nil)
318
378
  data = {}
319
379
  data[:email] = email
320
-
380
+
321
381
  if verify_purchase_items(items)
322
382
  data[:items] = items
323
383
  end
@@ -325,13 +385,15 @@ module Sailthru
325
385
  if incomplete != nil
326
386
  data[:incomplete] = incomplete.to_i
327
387
  end
328
-
388
+
329
389
  if message_id != nil
330
390
  data[:message_id] = message_id
331
391
  end
332
392
  api_post(:purchase, data)
333
393
  end
334
394
 
395
+
396
+ # <b>DEPRECATED:</b> Please use either stats_list or stats_blast
335
397
  # params:
336
398
  # stat, String
337
399
  #
@@ -339,12 +401,190 @@ module Sailthru
339
401
  # hash, response from server
340
402
  # Request various stats from Sailthru.
341
403
  def get_stats(stat)
404
+ warn "[DEPRECATION] `get_stats` is deprecated. Please use `stats_list` and `stats_blast` instead"
342
405
  api_get(:stats, {:stat => stat})
343
406
  end
344
407
 
345
408
 
409
+ # params
410
+ # list, String
411
+ # date, String
412
+ #
413
+ # returns:
414
+ # hash, response from server
415
+ # Retrieve information about your subscriber counts on a particular list, on a particular day.
416
+ def stats_list(list = nil, date = nil)
417
+ data = {}
418
+ if list != nil
419
+ data[:list] = list
420
+ end
421
+ if date != nil
422
+ data[:date] = date
423
+ end
424
+ data[:stat] = 'list'
425
+
426
+ stats(data)
427
+ end
428
+
429
+
430
+ # params
431
+ # blast_id, String
432
+ # start_date, String
433
+ # end_date, String
434
+ # options, Hash
435
+ #
436
+ # returns:
437
+ # hash, response from server
438
+ # Retrieve information about a particular blast or aggregated information from all of blasts over a specified date range
439
+ def stats_blast(blast_id = nil, start_date = nil, end_date = nil, options = {})
440
+ data = options
441
+ if blast_id != nil
442
+ data[:blast_id] = blast_id
443
+ end
444
+ if start_date != nil
445
+ data[:start_date] = start_date
446
+ end
447
+ if end_date != nil
448
+ data[:end_date] = end_date
449
+ end
450
+ data[:stat] = 'blast'
451
+ stats(data)
452
+ end
453
+
454
+
455
+ # params
456
+ # title, String
457
+ # url, String
458
+ # date, String
459
+ # tags, Array or Comma separated string
460
+ # vars, Hash
461
+ #
462
+ # Push a new piece of content to Sailthru, triggering any applicable alerts.
463
+ # http://docs.sailthru.com/api/content
464
+ def push_content(title, url, date = nil, tags = nil, vars = {})
465
+ data = {}
466
+ data[:title] = title
467
+ data[:url] = url
468
+ if date != nil
469
+ data[:date] = date
470
+ end
471
+ if tags != nil
472
+ if tags.class == Array
473
+ tags = tags.join(',')
474
+ end
475
+ data[:tags] = tags
476
+ end
477
+ if vars.length > 0
478
+ data[:vars] = vars
479
+ end
480
+ api_post(:content, data)
481
+ end
482
+
483
+ # params
484
+ # list, String
485
+ # format, String
486
+ #
487
+ # Download a list. Obviously, this can potentially be a very large download.
488
+ # 'txt' is default format since, its more compact as compare to others
489
+ def get_list(list, format = 'txt')
490
+ return api_get(:list, {:list => list, :format => format})
491
+ end
492
+
493
+
494
+ # params
495
+ # list, String
496
+ # emails, String | Array
497
+ # Upload a list. The list import job is queued and will happen shortly after the API request.
498
+ def save_list(list, emails)
499
+ data = {}
500
+ data[:list] = list
501
+ data[:emails] = (emails.class == Array) ? emails.join(',') : emails
502
+ return api_post(:list, data)
503
+ end
504
+
505
+
506
+ # params
507
+ # list, String
508
+ #
509
+ # Deletes a list
510
+ def delete_list(list)
511
+ api_delete(:list, {:list => list})
512
+ end
513
+
514
+ # params
515
+ # email, String
516
+ # hid_only, Boolean
517
+ #
518
+ # gets horizon data
519
+ def get_horizon(email, hid_only = false)
520
+ data = {}
521
+ data[:email] = email
522
+ if hid_only == true
523
+ data[:hid_only] = 1
524
+ end
525
+ api_get(:horizon, data)
526
+ end
527
+
528
+
529
+ # params
530
+ # email, String
531
+ # tags, String | Array
532
+ #
533
+ # sets horizon data
534
+ def set_horizon(email, tags)
535
+ data = {}
536
+ data[
537
+ :email] = email
538
+ data[:tags] = (tags.class == Array) ? tags.join(',') : tags
539
+ api_post(:horizon, data)
540
+ end
541
+
542
+ # params
543
+ # email, String
544
+ #
545
+ # get user alert data
546
+ def get_alert(email)
547
+ api_get(:alert, {:email => email})
548
+ end
549
+
550
+ # params
551
+ # email, String
552
+ # type, String
553
+ # template, String
554
+ # _when, String
555
+ # options, hash
556
+ #
557
+ # Add a new alert to a user. You can add either a realtime or a summary alert (daily/weekly).
558
+ # _when is only required when alert type is weekly or daily
559
+ def save_alert(email, type, template, _when = nil, options = {})
560
+ data = options
561
+ data[:email] = email
562
+ data[:type] = type
563
+ data[:template] = template
564
+ if (type == 'weekly' || type == 'daily')
565
+ data[:when] = _when
566
+ end
567
+ api_post(:alert, data)
568
+ end
569
+
570
+
571
+ # params
572
+ # email, String
573
+ # alert_id, String
574
+ #
575
+ # delete user alert
576
+ def delete_alert(email, alert_id)
577
+ data = {:email => email, :alert_id => alert_id}
578
+ api_delete(:alert, data)
579
+ end
580
+
346
581
  protected
347
582
 
583
+ # Make Stats API Request
584
+ def stats(data)
585
+ api_get(:stats, data)
586
+ end
587
+
348
588
  # Perform API GET request
349
589
  def api_get(action, data)
350
590
  api_request(action, data, 'GET')
@@ -377,8 +617,15 @@ module Sailthru
377
617
 
378
618
 
379
619
  # NOTE: don't do the unserialize here
380
- unserialized = JSON.parse(_result)
381
- return unserialized ? unserialized : _result
620
+ if data[:format] == 'json'
621
+ begin
622
+ unserialized = JSON.parse(_result)
623
+ return unserialized ? unserialized : _result
624
+ rescue JSON::JSONError => e
625
+ return {'error' => e}
626
+ end
627
+ end
628
+ return _result
382
629
  end
383
630
 
384
631
 
@@ -397,7 +644,7 @@ module Sailthru
397
644
  end
398
645
  req = nil
399
646
  headers = {"User-Agent" => "Sailthru API Ruby Client #{VERSION}"}
400
-
647
+
401
648
  _uri = URI.parse(uri)
402
649
  if method == 'POST'
403
650
  req = Net::HTTP::Post.new(_uri.path, headers)
@@ -424,6 +671,6 @@ module Sailthru
424
671
  else
425
672
  raise SailthruClientException.new("No response received from stream: #{_uri.to_s}")
426
673
  end
427
- end
674
+ end
428
675
  end
429
676
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sailthru-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
9
- version: "1.02"
8
+ - 3
9
+ version: "1.03"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Prajwal Tuladhar
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-25 00:00:00 -05:00
17
+ date: 2011-02-03 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency