danest-sailthru-client 1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +15 -0
  3. data/lib/sailthru.rb +970 -0
  4. metadata +118 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 837599b172f021e53746c666ee63ba91b3356caa
4
+ data.tar.gz: 79ca81519104cb8c4eb6b8c1087c3647a657dbf4
5
+ SHA512:
6
+ metadata.gz: 7e30c5cbc539336514f506e02451c5af3fd15563b1183df4192fd8e75ed326b5a66fb74d2879996b1ea8b89c50a1c45798cca1132991aa0b083472cf4bf8bb86
7
+ data.tar.gz: ed06a75b6eabd368501eaf9c7becb34d4d37772e0ed12afa0c9f0d064006b04777bb678107d573b895c475011dbd06fe868033144e7166be1f2a3b2559f4f26f
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ This gem add more features to the current sailthru gem version.
2
+
3
+ sailthru-ruby-client
4
+ ====================
5
+
6
+ For installation instructions, documentation, and examples please visit:
7
+ [http://getstarted.sailthru.com/developers/api-libraries/ruby](http://getstarted.sailthru.com/developers/api-libraries/ruby)
8
+
9
+ A simple client library to remotely access the `Sailthru REST API` as per [http://getstarted.sailthru.com/developers/api](http://getstarted.sailthru.com/developers/api)
10
+
11
+ By default, it will make request in `JSON` format.
12
+
13
+ ### Installing from rubygems.org (Tested with Ruby 1.8.7)
14
+ $ gem install sailthru-client
15
+
data/lib/sailthru.rb ADDED
@@ -0,0 +1,970 @@
1
+ require 'rubygems'
2
+ require 'net/https'
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'cgi'
6
+ require 'json'
7
+ require 'digest/md5'
8
+ require 'net/http/post/multipart'
9
+
10
+ module Sailthru
11
+
12
+ Version = VERSION = '1.15'
13
+
14
+ class SailthruClientException < Exception
15
+ end
16
+
17
+ module Helpers
18
+ # params:
19
+ # params, Hash
20
+ # returns:
21
+ # Array, values of each item in the Hash (and nested hashes)
22
+ #
23
+ # Extracts the values of a set of parameters, recursing into nested assoc arrays.
24
+ def extract_param_values(params)
25
+ values = []
26
+ params.each do |k, v|
27
+ if v.class == Hash
28
+ values.concat extract_param_values(v)
29
+ elsif v.class == Array
30
+ temp_hash = Hash.new()
31
+ v.each_with_index do |v_,i_|
32
+ temp_hash[i_.to_s] = v_
33
+ end
34
+ values.concat extract_param_values(temp_hash)
35
+ else
36
+ values.push v.to_s
37
+ end
38
+ end
39
+ return values
40
+ end
41
+
42
+ # params:
43
+ # params, Hash
44
+ # secret, String
45
+ # returns:
46
+ # String
47
+ #
48
+ # Returns the unhashed signature string (secret + sorted list of param values) for an API call.
49
+ def get_signature_string(params, secret)
50
+ return secret + extract_param_values(params).sort.join("")
51
+ end
52
+
53
+
54
+ # params:
55
+ # params, Hash
56
+ # secret, String
57
+ # returns:
58
+ # String
59
+ #
60
+ # Returns an MD5 hash of the signature string for an API call.
61
+ def get_signature_hash(params, secret)
62
+ Digest::MD5.hexdigest(get_signature_string(params, secret)).to_s
63
+ end
64
+
65
+
66
+ # Flatten nested hash for GET / POST request.
67
+ def flatten_nested_hash(hash, brackets = true)
68
+ f = {}
69
+ hash.each do |key, value|
70
+ _key = brackets ? "[#{key}]" : key.to_s
71
+ if value.class == Hash
72
+ flatten_nested_hash(value).each do |k, v|
73
+ f["#{_key}#{k}"] = v
74
+ end
75
+ elsif value.class == Array
76
+ temp_hash = Hash.new()
77
+ value.each_with_index do |v, i|
78
+ temp_hash[i.to_s] = v
79
+ end
80
+ flatten_nested_hash(temp_hash).each do |k, v|
81
+ f["#{_key}#{k}"] = v
82
+ end
83
+
84
+ else
85
+ f[_key] = value
86
+ end
87
+ end
88
+ return f
89
+ end
90
+
91
+ end
92
+
93
+ class SailthruClient
94
+
95
+ include Helpers
96
+
97
+ attr_accessor :verify_ssl
98
+
99
+ # params:
100
+ # api_key, String
101
+ # secret, String
102
+ # api_uri, String
103
+ #
104
+ # Instantiate a new client; constructor optionally takes overrides for key/secret/uri and proxy server settings.
105
+ def initialize(api_key, secret, api_uri=nil, proxy_host=nil, proxy_port=nil)
106
+ @api_key = api_key
107
+ @secret = secret
108
+ @api_uri = if api_uri.nil? then 'https://api.sailthru.com' else api_uri end
109
+ @proxy_host = proxy_host
110
+ @proxy_port = proxy_port
111
+ @verify_ssl = true
112
+ end
113
+
114
+ # params:
115
+ # template_name, String
116
+ # email, String
117
+ # replacements, Hash
118
+ # options, Hash
119
+ # replyto: override Reply-To header
120
+ # test: send as test email (subject line will be marked, will not count towards stats)
121
+ # schedule_time, Date
122
+ # returns:
123
+ # Hash, response data from server
124
+ #
125
+ # Send a transactional email, or schedule one for the near future
126
+ # http://docs.sailthru.com/api/send
127
+ def send(template_name, email, vars={}, options = {}, schedule_time = nil)
128
+ warn "[DEPRECATION] `send` is deprecated. Please use `send_email` instead."
129
+ send_email(template_name, email, vars={}, options = {}, schedule_time = nil)
130
+ end
131
+
132
+ # params:
133
+ # template_name, String
134
+ # email, String
135
+ # vars, Hash
136
+ # options, Hash
137
+ # replyto: override Reply-To header
138
+ # test: send as test email (subject line will be marked, will not count towards stats)
139
+ # returns:
140
+ # Hash, response data from server
141
+ def send_email(template_name, email, vars={}, options = {}, schedule_time = nil)
142
+ post = {}
143
+ post[:template] = template_name
144
+ post[:email] = email
145
+ post[:vars] = vars if vars.length >= 1
146
+ post[:options] = options if options.length >= 1
147
+ post[:schedule_time] = schedule_time if !schedule_time.nil?
148
+ return self.api_post(:send, post)
149
+ end
150
+
151
+
152
+ def multi_send(template_name, emails, vars={}, options = {}, schedule_time = nil, evars = {})
153
+ post = {}
154
+ post[:template] = template_name
155
+ post[:email] = emails
156
+ post[:vars] = vars if vars.length >= 1
157
+ post[:options] = options if options.length >= 1
158
+ post[:schedule_time] = schedule_time if !schedule_time.nil?
159
+ post[:evars] = evars if evars.length >= 1
160
+ return self.api_post(:send, post)
161
+ end
162
+
163
+
164
+ # params:
165
+ # send_id, Fixnum
166
+ # returns:
167
+ # Hash, response data from server
168
+ #
169
+ # Get the status of a send.
170
+ def get_send(send_id)
171
+ self.api_get(:send, {:send_id => send_id.to_s})
172
+ end
173
+
174
+
175
+ def cancel_send(send_id)
176
+ self.api_delete(:send, {:send_id => send_id.to_s})
177
+ end
178
+
179
+ # params:
180
+ # name, String
181
+ # list, String
182
+ # schedule_time, String
183
+ # from_name, String
184
+ # from_email, String
185
+ # subject, String
186
+ # content_html, String
187
+ # content_text, String
188
+ # options, Hash
189
+ # returns:
190
+ # Hash, response data from server
191
+ #
192
+ # Schedule a mass mail blast
193
+ def schedule_blast(name, list, schedule_time, from_name, from_email, subject, content_html, content_text, options = {})
194
+ post = options ? options : {}
195
+ post[:name] = name
196
+ post[:list] = list
197
+ post[:schedule_time] = schedule_time
198
+ post[:from_name] = from_name
199
+ post[:from_email] = from_email
200
+ post[:subject] = subject
201
+ post[:content_html] = content_html
202
+ post[:content_text] = content_text
203
+ api_post(:blast, post)
204
+ end
205
+
206
+ # Schedule a mass mail blast from template
207
+ def schedule_blast_from_template(template, list, schedule_time, options={})
208
+ post = options ? options : {}
209
+ post[:copy_template] = template
210
+ post[:list] = list
211
+ post[:schedule_time] = schedule_time
212
+ api_post(:blast, post)
213
+ end
214
+
215
+ # Schedule a mass mail blast from previous blast
216
+ def schedule_blast_from_blast(blast_id, schedule_time, options={})
217
+ post = options ? options : {}
218
+ post[:copy_blast] = blast_id
219
+ #post[:name] = name
220
+ post[:schedule_time] = schedule_time
221
+ api_post(:blast, post)
222
+ end
223
+
224
+
225
+ # params
226
+ # blast_id, Fixnum | String
227
+ # name, String
228
+ # list, String
229
+ # schedule_time, String
230
+ # from_name, String
231
+ # from_email, String
232
+ # subject, String
233
+ # content_html, String
234
+ # content_text, String
235
+ # options, hash
236
+ #
237
+ # updates existing blast
238
+ 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 = {})
239
+ data = options ? options : {}
240
+ data[:blast_id] = blast_id
241
+ if name != nil
242
+ data[:name] = name
243
+ end
244
+ if list != nil
245
+ data[:list] = list
246
+ end
247
+ if schedule_time != nil
248
+ data[:schedule_time] = schedule_time
249
+ end
250
+ if from_name != nil
251
+ data[:from_name] = from_name
252
+ end
253
+ if from_email != nil
254
+ data[:from_email] = from_email
255
+ end
256
+ if subject != nil
257
+ data[:subject] = subject
258
+ end
259
+ if content_html != nil
260
+ data[:content_html] = content_html
261
+ end
262
+ if content_text != nil
263
+ data[:content_text] = content_text
264
+ end
265
+ api_post(:blast, data)
266
+ end
267
+
268
+
269
+ # params:
270
+ # blast_id, Fixnum | String
271
+ # options, hash
272
+ # returns:
273
+ # Hash, response data from server
274
+ #
275
+ # Get information on a previously scheduled email blast
276
+ def get_blast(blast_id, options={})
277
+ options[:blast_id] = blast_id.to_s
278
+ api_get(:blast, options)
279
+ end
280
+
281
+ # params:
282
+ # blast_id, Fixnum | String
283
+ #
284
+ # Cancel a scheduled Blast
285
+ def cancel_blast(blast_id)
286
+ api_post(:blast, {:blast_id => blast_id, :schedule_time => ''})
287
+ end
288
+
289
+ # params:
290
+ # blast_id, Fixnum | String
291
+ #
292
+ # Delete a Blast
293
+ def delete_blast(blast_id)
294
+ api_delete(:blast, {:blast_id => blast_id})
295
+ end
296
+
297
+ # params:
298
+ # email, String
299
+ # returns:
300
+ # Hash, response data from server
301
+ #
302
+ # Return information about an email address, including replacement vars and lists.
303
+ def get_email(email)
304
+ api_get(:email, {:email => email})
305
+ end
306
+
307
+ # params:
308
+ # email, String
309
+ # vars, Hash
310
+ # lists, Hash mapping list name => 1 for subscribed, 0 for unsubscribed
311
+ # options, Hash mapping optional parameters
312
+ # returns:
313
+ # Hash, response data from server
314
+ #
315
+ # Set replacement vars and/or list subscriptions for an email address.
316
+ def set_email(email, vars = {}, lists = {}, templates = {}, options = {})
317
+ data = options
318
+ data[:email] = email
319
+ data[:vars] = vars unless vars.empty?
320
+ data[:lists] = lists unless lists.empty?
321
+ data[:templates] = templates unless templates.empty?
322
+ self.api_post(:email, data)
323
+ end
324
+
325
+ # params:
326
+ # new_email, String
327
+ # old_email, String
328
+ # options, Hash mapping optional parameters
329
+ # returns:
330
+ # Hash of response data.
331
+ #
332
+ # change a user's email address.
333
+ def change_email(new_email, old_email, options = {})
334
+ data = options
335
+ data[:email] = new_email
336
+ data[:change_email] = old_email
337
+ self.api_post(:email, data)
338
+ end
339
+
340
+ # params:
341
+ # template_name, String
342
+ # returns:
343
+ # Hash of response data.
344
+ #
345
+ # Get a template.
346
+ def get_template(template_name)
347
+ self.api_get(:template, {:template => template_name})
348
+ end
349
+
350
+
351
+ # params:
352
+ # template_name, String
353
+ # template_fields, Hash
354
+ # returns:
355
+ # Hash containg response from the server.
356
+ #
357
+ # Save a template.
358
+ def save_template(template_name, template_fields)
359
+ data = template_fields
360
+ data[:template] = template_name
361
+ self.api_post(:template, data)
362
+ end
363
+
364
+ # params:
365
+ # template_name, String
366
+ # returns:
367
+ # Hash of response data.
368
+ #
369
+ # Delete a template.
370
+ def delete_template(template_name)
371
+ self.api_delete(:template, {:template => template_name})
372
+ end
373
+
374
+
375
+ # params:
376
+ # params, Hash
377
+ # request, String
378
+ # returns:
379
+ # TrueClass or FalseClass, Returns true if the incoming request is an authenticated verify post.
380
+ def receive_verify_post(params, request)
381
+ if request.post?
382
+ [:action, :email, :send_id, :sig].each { |key| return false unless params.has_key?(key) }
383
+
384
+ return false unless params[:action] == :verify
385
+
386
+ sig = params.delete(:sig)
387
+ return false unless sig == get_signature_hash(params, @secret)
388
+
389
+ _send = self.get_send(params[:send_id])
390
+ return false unless _send.has_key?(:email)
391
+
392
+ return false unless _send[:email] == params[:email]
393
+
394
+ return true
395
+ else
396
+ return false
397
+ end
398
+ end
399
+
400
+ # params:
401
+ # params, Hash
402
+ # request, String
403
+ # returns:
404
+ # TrueClass or FalseClass, Returns true if the incoming request is an authenticated optout post.
405
+ def receive_optout_post(params, request)
406
+ if request.post?
407
+ [:action, :email, :sig].each { |key| return false unless params.has_key?(key) }
408
+
409
+ return false unless params[:action] == :optout
410
+
411
+ sig = params.delete(:sig)
412
+ return false unless sig == get_signature_hash(params, @secret)
413
+ return true
414
+ else
415
+ return false
416
+ end
417
+ end
418
+
419
+ # params:
420
+ # params, Hash
421
+ # request, String
422
+ # returns:
423
+ # TrueClass or FalseClass, Returns true if the incoming request is an authenticated hardbounce post.
424
+ def receive_hardbounce_post(params, request)
425
+ if request.post?
426
+ [:action, :email, :sig].each { |key| return false unless params.has_key?(key) }
427
+
428
+ return false unless params[:action] == :hardbounce
429
+
430
+ sig = params.delete(:sig)
431
+ return false unless sig == get_signature_hash(params, @secret)
432
+ return true
433
+ else
434
+ return false
435
+ end
436
+ end
437
+
438
+ # params:
439
+ # email, String
440
+ # items, String
441
+ # incomplete, Integer
442
+ # message_id, String
443
+ # options, Hash
444
+ # returns:
445
+ # hash, response from server
446
+ #
447
+ # Record that a user has made a purchase, or has added items to their purchase total.
448
+ def purchase(email, items, incomplete = nil, message_id = nil, options = {})
449
+ data = options
450
+ data[:email] = email
451
+ data[:items] = items
452
+
453
+ if incomplete != nil
454
+ data[:incomplete] = incomplete.to_i
455
+ end
456
+
457
+ if message_id != nil
458
+ data[:message_id] = message_id
459
+ end
460
+ api_post(:purchase, data)
461
+ end
462
+
463
+
464
+ # <b>DEPRECATED:</b> Please use either stats_list or stats_blast
465
+ # params:
466
+ # stat, String
467
+ #
468
+ # returns:
469
+ # hash, response from server
470
+ # Request various stats from Sailthru.
471
+ def get_stats(stat)
472
+ warn "[DEPRECATION] `get_stats` is deprecated. Please use `stats_list` and `stats_blast` instead"
473
+ api_get(:stats, {:stat => stat})
474
+ end
475
+
476
+
477
+ # params
478
+ # list, String
479
+ # date, String
480
+ #
481
+ # returns:
482
+ # hash, response from server
483
+ # Retrieve information about your subscriber counts on a particular list, on a particular day.
484
+ def stats_list(list = nil, date = nil)
485
+ data = {}
486
+ if list != nil
487
+ data[:list] = list
488
+ end
489
+ if date != nil
490
+ data[:date] = date
491
+ end
492
+ data[:stat] = 'list'
493
+ stats(data)
494
+ end
495
+
496
+
497
+ # params
498
+ # blast_id, String
499
+ # start_date, String
500
+ # end_date, String
501
+ # options, Hash
502
+ #
503
+ # returns:
504
+ # hash, response from server
505
+ # Retrieve information about a particular blast or aggregated information from all of blasts over a specified date range
506
+ def stats_blast(blast_id = nil, start_date = nil, end_date = nil, options = {})
507
+ data = options
508
+ if blast_id != nil
509
+ data[:blast_id] = blast_id
510
+ end
511
+ if start_date != nil
512
+ data[:start_date] = start_date
513
+ end
514
+ if end_date != nil
515
+ data[:end_date] = end_date
516
+ end
517
+ data[:stat] = 'blast'
518
+ stats(data)
519
+ end
520
+
521
+ # params
522
+ # template, String
523
+ # start_date, String
524
+ # end_date, String
525
+ # options, Hash
526
+ #
527
+ # returns:
528
+ # hash, response from server
529
+ # Retrieve information about a particular blast or aggregated information from all of blasts over a specified date range
530
+ def stats_send(template = nil, start_date = nil, end_date = nil, options = {})
531
+ data = options
532
+ if template != nil
533
+ data[:template] = template
534
+ end
535
+ if start_date != nil
536
+ data[:start_date] = start_date
537
+ end
538
+ if end_date != nil
539
+ data[:end_date] = end_date
540
+ end
541
+ data[:stat] = 'send'
542
+ stats(data)
543
+ end
544
+
545
+
546
+ # params
547
+ # title, String
548
+ # url, String
549
+ # date, String
550
+ # tags, Array or Comma separated string
551
+ # vars, Hash
552
+ # options, Hash
553
+ #
554
+ # Push a new piece of content to Sailthru, triggering any applicable alerts.
555
+ # http://docs.sailthru.com/api/content
556
+ def push_content(title, url, date = nil, tags = nil, vars = {}, options = {})
557
+ data = options
558
+ data[:title] = title
559
+ data[:url] = url
560
+ if date != nil
561
+ data[:date] = date
562
+ end
563
+ if tags != nil
564
+ if tags.class == Array
565
+ tags = tags.join(',')
566
+ end
567
+ data[:tags] = tags
568
+ end
569
+ if vars.length > 0
570
+ data[:vars] = vars
571
+ end
572
+ api_post(:content, data)
573
+ end
574
+
575
+ # params
576
+ # url, String
577
+ #
578
+ # Delete a piece of content from Sailthru.
579
+ # http://docs.sailthru.com/api/content
580
+ def delete_content(url)
581
+ data = {}
582
+ data[:url] = url
583
+ api_delete(:content, data)
584
+ end
585
+
586
+ # params
587
+ # list, String
588
+ #
589
+ # Get information about a list.
590
+ def get_list(list)
591
+ return api_get(:list, {:list => list})
592
+ end
593
+
594
+ # params
595
+ #
596
+ # Get information about all lists
597
+ def get_lists()
598
+ return api_get(:list, {})
599
+ end
600
+
601
+ # params
602
+ # list, String
603
+ # options, Hash
604
+ # Create a list, or update a list.
605
+ def save_list(list, options = {})
606
+ data = options
607
+ data[:list] = list
608
+ return api_post(:list, data)
609
+ end
610
+
611
+ # params
612
+ # list, String
613
+ #
614
+ # Deletes a list
615
+ def delete_list(list)
616
+ api_delete(:list, {:list => list})
617
+ end
618
+
619
+ # params
620
+ # email, String
621
+ #
622
+ # get user alert data
623
+ def get_alert(email)
624
+ api_get(:alert, {:email => email})
625
+ end
626
+
627
+ # params
628
+ # email, String
629
+ # type, String
630
+ # template, String
631
+ # _when, String
632
+ # options, hash
633
+ #
634
+ # Add a new alert to a user. You can add either a realtime or a summary alert (daily/weekly).
635
+ # _when is only required when alert type is weekly or daily
636
+ def save_alert(email, type, template, _when = nil, options = {})
637
+ data = options
638
+ data[:email] = email
639
+ data[:type] = type
640
+ data[:template] = template
641
+ if (type == 'weekly' || type == 'daily')
642
+ data[:when] = _when
643
+ end
644
+ api_post(:alert, data)
645
+ end
646
+
647
+
648
+ # params
649
+ # email, String
650
+ # alert_id, String
651
+ #
652
+ # delete user alert
653
+ def delete_alert(email, alert_id)
654
+ data = {:email => email, :alert_id => alert_id}
655
+ api_delete(:alert, data)
656
+ end
657
+
658
+ # Make Stats API Request
659
+ def stats(data)
660
+ api_get(:stats, data)
661
+ end
662
+
663
+ # params
664
+ # job, String
665
+ # options, hash
666
+ # report_email, String
667
+ # postback_url, String
668
+ # binary_key, String
669
+ #
670
+ # interface for making request to job call
671
+ def process_job(job, options = {}, report_email = nil, postback_url = nil, binary_key = nil)
672
+ data = options
673
+ data['job'] = job
674
+ if !report_email.nil?
675
+ data['report_email'] = report_email
676
+ end
677
+
678
+ if !postback_url.nil?
679
+ data['postback_url'] = postback_url
680
+ end
681
+ api_post(:job, data, binary_key)
682
+ end
683
+
684
+ # params
685
+ # emails, String | Array
686
+ # implementation for import_job
687
+ def process_import_job(list, emails, report_email = nil, postback_url = nil)
688
+ data = {}
689
+ data['list'] = list
690
+ data['emails'] = Array(emails).join(',')
691
+ process_job(:import, data, report_email, postback_url)
692
+ end
693
+
694
+ # implementation for import job using file upload
695
+ def process_import_job_from_file(list, file_path, report_email = nil, postback_url = nil)
696
+ data = {}
697
+ data['list'] = list
698
+ data['file'] = file_path
699
+ process_job(:import, data, report_email, postback_url, 'file')
700
+ end
701
+
702
+ # implementation for update job using file upload
703
+ def process_update_job_from_file(file_path, report_email = nil, postback_url = nil)
704
+ data = {}
705
+ data['file'] = file_path
706
+ process_job(:update, data, report_email, postback_url, 'file')
707
+ end
708
+
709
+ # implementation for snapshot job
710
+ def process_snapshot_job(query = {}, report_email = nil, postback_url = nil)
711
+ data = {}
712
+ data['query'] = query
713
+ process_job(:snapshot, data, report_email, postback_url)
714
+ end
715
+
716
+ # implementation for export list job
717
+ def process_export_list_job(list, report_email = nil, postback_url = nil)
718
+ data = {}
719
+ data['list'] = list
720
+ process_job(:export_list_data, data, report_email, postback_url)
721
+ end
722
+
723
+ # get status of a job
724
+ def get_job_status(job_id)
725
+ api_get(:job, {'job_id' => job_id})
726
+ end
727
+
728
+ # Get user by Sailthru ID
729
+ def get_user_by_sid(id, fields = {})
730
+ api_get(:user, {'id' => id, 'fields' => fields})
731
+ end
732
+
733
+ # Get user by specified key
734
+ def get_user_by_key(id, key, fields = {})
735
+ data = {
736
+ 'id' => id,
737
+ 'key' => key,
738
+ 'fields' => fields
739
+ }
740
+ api_get(:user, data)
741
+ end
742
+
743
+ # Create new user, or update existing user
744
+ def save_user(id, options = {})
745
+ data = options
746
+ data['id'] = id
747
+ api_post(:user, data)
748
+ end
749
+
750
+ # params
751
+ # Get an existing trigger
752
+ def get_triggers()
753
+ api_get(:trigger, {})
754
+ end
755
+
756
+ # params
757
+ # template, String
758
+ # trigger_id, String
759
+ # Get an existing trigger
760
+ def get_trigger_by_template(template, trigger_id = nil)
761
+ data = {}
762
+ data['template'] = template
763
+ if trigger_id != nil then data['trigger_id'] = trigger_id end
764
+ api_get(:trigger, data)
765
+ end
766
+
767
+ # params
768
+ # event, String
769
+ # Get an existing trigger
770
+ def get_trigger_by_event(event)
771
+ data = {}
772
+ data['event'] = event
773
+ api_get(:trigger, data)
774
+ end
775
+
776
+ # params
777
+ # template, String
778
+ # time, String
779
+ # time_unit, String
780
+ # event, String
781
+ # zephyr, String
782
+ # Create or update a trigger
783
+ def post_template_trigger(template, time, time_unit, event, zephyr)
784
+ data = {}
785
+ data['template'] = template
786
+ data['time'] = time
787
+ data['time_unit'] = time_unit
788
+ data['event'] = event
789
+ data['zephyr'] = zephyr
790
+ api_post(:trigger, data)
791
+ end
792
+
793
+ # params
794
+ # template, String
795
+ # time, String
796
+ # time_unit, String
797
+ # zephyr, String
798
+ # Create or update a trigger
799
+ def post_event_trigger(event, time, time_unit, zephyr)
800
+ data = {}
801
+ data['time'] = time
802
+ data['time_unit'] = time_unit
803
+ data['event'] = event
804
+ data['zephyr'] = zephyr
805
+ api_post(:trigger, data)
806
+ end
807
+
808
+ # params
809
+ # id, String
810
+ # event, String
811
+ # options, Hash (Can contain vars, Hash and/or key)
812
+ # Notify Sailthru of an Event
813
+ def post_event(id, event, options = {})
814
+ data = options
815
+ data['id'] = id
816
+ data['event'] = event
817
+ api_post(:event, data)
818
+ end
819
+
820
+ # Perform API GET request
821
+ def api_get(action, data)
822
+ api_request(action, data, 'GET')
823
+ end
824
+
825
+ # Perform API POST request
826
+ def api_post(action, data, binary_key = nil)
827
+ api_request(action, data, 'POST', binary_key)
828
+ end
829
+
830
+ #Perform API DELETE request
831
+ def api_delete(action, data)
832
+ api_request(action, data, 'DELETE')
833
+ end
834
+
835
+ protected
836
+
837
+ # params:
838
+ # action, String
839
+ # data, Hash
840
+ # request, String "GET" or "POST"
841
+ # returns:
842
+ # Hash
843
+ #
844
+ # Perform an API request, using the shared-secret auth hash.
845
+ #
846
+ def api_request(action, data, request_type, binary_key = nil)
847
+ if (!binary_key.nil?)
848
+ binary_key_data = data[binary_key]
849
+ data.delete(binary_key)
850
+ end
851
+
852
+ if data[:format].nil? or data[:format] == 'json'
853
+ data = self.prepare_json_payload(data)
854
+ else
855
+ data[:api_key] = @api_key
856
+ data[:format] ||= 'json'
857
+ data[:sig] = get_signature_hash(data, @secret)
858
+ end
859
+
860
+ if (!binary_key.nil?)
861
+ data[binary_key] = binary_key_data
862
+ end
863
+ _result = self.http_request("#{@api_uri}/#{action}", data, request_type, binary_key)
864
+
865
+ # NOTE: don't do the unserialize here
866
+ if data[:format] == 'json'
867
+ begin
868
+ unserialized = JSON.parse(_result)
869
+ return unserialized ? unserialized : _result
870
+ rescue JSON::JSONError => e
871
+ return {'error' => e}
872
+ end
873
+ end
874
+ return _result
875
+ end
876
+
877
+ # set up our post request
878
+ def set_up_post_request(uri, data, headers, binary_key = nil)
879
+ if (!binary_key.nil?)
880
+ binary_data = data[binary_key]
881
+
882
+ if binary_data.is_a?(StringIO)
883
+ data[binary_key] = UploadIO.new(
884
+ binary_data, "text/plain"
885
+ )
886
+ else
887
+ data[binary_key] = UploadIO.new(
888
+ File.open(binary_data), "text/plain"
889
+ )
890
+ end
891
+
892
+ req = Net::HTTP::Post::Multipart.new(uri.path, data)
893
+ else
894
+ req = Net::HTTP::Post.new(uri.path, headers)
895
+ req.set_form_data(data)
896
+ end
897
+ req
898
+ end
899
+
900
+ # params:
901
+ # uri, String
902
+ # data, Hash
903
+ # method, String "GET" or "POST"
904
+ # returns:
905
+ # String, body of response
906
+ def http_request(uri, data, method = 'POST', binary_key = nil)
907
+ data = flatten_nested_hash(data, false)
908
+
909
+ if method != 'POST'
910
+ uri += "?" + data.map{ |key, value| "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" }.join("&")
911
+ end
912
+
913
+ req = nil
914
+ headers = {"User-Agent" => "Sailthru API Ruby Client #{VERSION}"}
915
+
916
+ _uri = URI.parse(uri)
917
+
918
+ if method == 'POST'
919
+ req = self.set_up_post_request(
920
+ _uri, data, headers, binary_key
921
+ )
922
+
923
+ else
924
+ request_uri = "#{_uri.path}?#{_uri.query}"
925
+ if method == 'DELETE'
926
+ req = Net::HTTP::Delete.new(request_uri, headers)
927
+ else
928
+ req = Net::HTTP::Get.new(request_uri, headers)
929
+ end
930
+ end
931
+
932
+ begin
933
+ http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(_uri.host, _uri.port)
934
+
935
+ if _uri.scheme == 'https'
936
+ http.use_ssl = true
937
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl != true # some openSSL client doesn't work without doing this
938
+ end
939
+
940
+ response = http.start {
941
+ http.request(req)
942
+ }
943
+
944
+ rescue Exception => e
945
+ raise SailthruClientException.new(["Unable to open stream: #{_uri}", e.inspect, e.backtrace].join("\n"));
946
+ end
947
+
948
+ if response.body
949
+ return response.body
950
+ else
951
+ raise SailthruClientException.new("No response received from stream: #{_uri}")
952
+ end
953
+ end
954
+
955
+ def http_multipart_request(uri, data)
956
+ req = Net::HTTP::Post::Multipart.new url.path,
957
+ "file" => UploadIO.new(data['file'], "application/octet-stream")
958
+ end
959
+
960
+ def prepare_json_payload(data)
961
+ payload = {
962
+ :api_key => @api_key,
963
+ :format => 'json', #<3 XML
964
+ :json => data.to_json
965
+ }
966
+ payload[:sig] = get_signature_hash(payload, @secret)
967
+ payload
968
+ end
969
+ end
970
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danest-sailthru-client
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.16'
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Urrutia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multipart-post
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fakeweb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email: urrutia.kevin@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - README.md
89
+ files:
90
+ - README.md
91
+ - lib/sailthru.rb
92
+ homepage: http://docs.sailthru.com
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --main
98
+ - README.md
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.0.6
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: A simple client library to remotely access the Sailthru REST API. Updated
117
+ with latest git master.
118
+ test_files: []