-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsendpulse_api.rb
More file actions
640 lines (578 loc) · 13.8 KB
/
sendpulse_api.rb
File metadata and controls
640 lines (578 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
62
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
require 'net/http'
require 'json'
require 'base64'
#
# Sendpulse REST API Ruby class
#
# Documentation
# https://login.sendpulse.com/manual/rest-api/
# https://sendpulse.com/api
#
class SendpulseApi
#
# Sendpulse API constructor
#
# @param [Fixnum] user_id
# @param [String] secret
# @param [String] protocol
# @param [String] token
# @raise [Exception]
#
def initialize(user_id, secret, protocol = 'https', token = '')
raise 'Empty ID or SECRET' if user_id.to_s.empty? || secret.to_s.empty?
@url = "#{protocol}://api.sendpulse.com"
@user_id = user_id
@secret = secret
@protocol = protocol
@refresh_token = 0
@token = token
if @token.nil? || @token.empty?
raise 'Could not connect to api, check your ID and SECRET' unless refresh_token
end
end
#
# Refresh token
#
# @return [Boolean]
#
def refresh_token
@refresh_token += 1
data = {
grant_type: 'client_credentials',
client_id: @user_id,
client_secret: @secret
}
request_data = send_request('oauth/access_token', 'POST', data, false)
if !request_data.nil? && request_data[:data]['access_token']
@token = request_data[:data]['access_token']
@refresh_token = 0
else
return false
end
true
end
private :refresh_token
#
# Get token
#
# @return [String]
#
def get_token
@token
end
#
# Get serialized string
#
# @param [Mixed] data
# @return [String]
#
def serialize(data)
JSON.generate(data)
end
private :serialize
#
# Get unserialized data
#
# @param [String] data
# @return [Mixed]
#
def unserialize(data)
JSON.parse(data)
end
private :unserialize
#
# Form and send request to API service
#
# @param [String] path
# @param [String] method
# @param [Hash] data
# @param [Boolean] use_token
# @return [Hash]
#
def send_request(path, method = 'GET', data = {}, use_token = true)
request_data = {}
uri = URI.parse("#{@url}/#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if @protocol == 'https'
token = {}
token.merge!( 'Authorization' => "Bearer #{@token}" ) if use_token
case method
when 'POST'
request = Net::HTTP::Post.new(uri.request_uri, token)
request.set_form_data(data)
when 'PUT'
request = Net::HTTP::Put.new(uri.request_uri, token)
request.set_form_data(data)
when 'DELETE'
request = Net::HTTP::Delete.new(uri.request_uri, token)
request.set_form_data(data)
else
request = Net::HTTP::Get.new(uri.request_uri, token)
end
begin
response = http.request(request)
if response.code.to_i == 401 && @refresh_token == 0
refresh_token
return send_request(path, method, data, use_token)
else
request_data[:data] = JSON.parse(response.body)
request_data[:http_code] = response.code
end
rescue Exception => e
puts "Exception \n message: #{e.message} \n backtrace: #{e.backtrace}"
end
handle_result(request_data)
end
private :send_request
#
# Process results
#
# @param [String] custom_message
# @return [Hash]
#
def handle_error (custom_message = nil)
data = { is_error: true }
data[:message] = custom_message unless custom_message.nil?
data
end
private :handle_error
#
# Process errors
#
# @param [Hash] data
# @return [Hash]
#
def handle_result (data)
unless data[:http_code].to_i == 200
data[:is_error] = true
end
data
end
private :handle_result
#
# Create address book
#
# @param [String] book_name
# @return [Hash]
#
def create_address_book(book_name)
handle_error('Empty book name') if book_name.to_s.empty?
data = { bookName: book_name }
send_request('addressbooks', 'POST', data)
end
#
# Edit address book name
#
# @param [Fixnum] id
# @param [String] new_name
# @return [Hash]
#
def edit_address_book(id, new_name)
return handle_error('Empty new name or book id') if id.to_i <= 0 || new_name.to_s.empty?
data = { name: new_name }
send_request("addressbooks/#{id}", 'PUT', data)
end
#
# Remove address book
#
# @param [Fixnum] id
# @return [Hash]
#
def remove_address_book(id)
return handle_error('Empty book id') unless id.to_i > 0
send_request("addressbooks/#{id}", 'DELETE')
end
#
# Get list of address books
#
# @param [Fixnum] limit
# @param [Fixnum] offset
# @return [Hash]
#
def list_address_books(limit = nil, offset = nil)
data = {}
data.merge!({limit: limit}) unless limit.nil?
data.merge!({offset: offset}) unless offset.nil?
send_request('addressbooks', 'GET', data)
end
#
# Get information about book
#
# @param [Fixnum] id
# @return [Hash]
#
def get_book_info(id)
return handle_error('Empty book id') unless id.to_i > 0
send_request("addressbooks/#{id}")
end
#
# List email addresses from book
#
# @param [Fixnum] id
# @return [Hash]
#
def get_emails_from_book(id)
return handle_error('Empty book id') unless id.to_i > 0
send_request("addressbooks/#{id}/emails")
end
#
# Add new emails to address book
#
# @param [Fixnum] book_id
# @param [Hash] emails
# @return [Hash]
#
def add_emails(book_id, emails)
return handle_error('Empty book id or emails') if book_id.to_i <= 0 || emails.empty?
data = { emails: serialize(emails) }
send_request("addressbooks/#{book_id}/emails", 'POST', data)
end
#
# Remove email addresses from book
#
# @param [Fixnum] book_id
# @param [Hash] emails
# @return [Hash]
#
def remove_emails(book_id, emails)
return handle_error('Empty book id or emails') if book_id.to_i <= 0 || emails.empty?
data = { emails: serialize(emails) }
send_request("addressbooks/#{book_id}/emails", 'DELETE', data)
end
#
# Get information about email address from book
#
# @param [Fixnum] book_id
# @param [String] email
# @return [Hash]
#
def get_email_info(book_id, email)
return handle_error('Empty book id or email') if book_id.to_i <= 0 || email.to_s.empty?
send_request("addressbooks/#{book_id}/emails/#{email}")
end
#
# Get cost of campaign based on address book
#
# @param [Fixnum] book_id
# @return [Hash]
#
def campaign_cost(book_id)
return handle_error('Empty book id') unless book_id.to_i > 0
send_request("addressbooks/#{book_id}/cost")
end
#
# Get list of campaigns
#
# @param [Fixnum] limit
# @param [Fixnum] offset
# @return [Hash]
#
def list_campaigns(limit = nil, offset = nil)
data = {}
data.merge!({limit: limit}) unless limit.nil?
data.merge!({offset: offset}) unless offset.nil?
send_request('campaigns', 'GET', data)
end
#
# Get information about campaign
#
# @param [Fixnum] id
# @return [Hash]
#
def get_campaign_info(id)
return handle_error('Empty campaign id') unless id.to_i > 0
send_request("campaigns/#{id}")
end
#
# Get campaign statistic by countries
#
# @param [Fixnum] id
# @return [Hash]
#
def campaign_stat_by_countries(id)
return handle_error('Empty campaign id') unless id.to_i > 0
send_request("campaigns/#{id}/countries")
end
#
# Get campaign statistic by referrals
#
# @param [Fixnum] id
# @return [Hash]
#
def campaign_stat_by_referrals(id)
return handle_error('Empty campaign id') unless id.to_i > 0
send_request("campaigns/#{id}/referrals")
end
#
# Create new campaign
#
# @param [String] sender_name
# @param [String] sender_email
# @param [String] subject
# @param [String] body
# @param [Fixnum] book_id
# @param [String] name
# @param [String] attachments
# @return [Hash]
#
def create_campaign(sender_name, sender_email, subject, body, book_id, name = '', attachments = '')
if sender_name.empty? || sender_email.empty? || subject.empty? || body.empty? || book_id.to_i <= 0
return handle_error('Not all data.')
end
attachments = serialize(attachments) unless attachments.empty?
data = {
sender_name: sender_name,
sender_email: sender_email,
subject: subject,
body: Base64.encode64(body),
list_id: book_id,
name: name,
attachments: attachments
}
send_request('campaigns', 'POST', data)
end
#
# Cancel campaign
#
# @param [Fixnum] id
# @return [Hash]
#
def cancel_campaign(id)
return handle_error('Empty campaign id') unless id.to_i > 0
send_request("campaigns/#{id}", 'DELETE')
end
#
# List all senders
#
# @return [Hash]
#
def list_senders
send_request('senders')
end
#
# Cancel Add new sender
#
# @param [String] sender_name
# @param [String] sender_email
# @return [Hash]
#
def add_sender(sender_name, sender_email)
return handle_error('Empty book sender name or sender email') if sender_name.to_s.empty? || sender_email.to_s.empty?
data = {
email: sender_email,
name: sender_name
}
send_request('senders', 'POST', data)
end
#
# Remove sender
#
# @param [String] email
# @return [Hash]
#
def remove_sender(email)
return handle_error('Empty email') if email.to_s.empty?
data = { email: email }
send_request('senders', 'DELETE', data)
end
#
# Activate sender using code
#
# @param [String] email
# @param [String] code
# @return [Hash]
#
def activate_sender(email, code)
return handle_error('Empty email or activation code') if email.to_s.empty? || code.to_s.empty?
data = { code: code }
send_request("senders/#{email}/code", 'POST', data)
end
#
# Request mail with activation code
#
# @param [String] email
# @return [Hash]
#
def get_sender_activation_mail(email)
return handle_error('Empty email') if email.to_s.empty?
send_request("senders/#{email}/code")
end
#
# Get global information about email
#
# @param [String] email
# @return [Hash]
#
def get_email_global_info(email)
return handle_error('Empty email') if email.to_s.empty?
send_request("emails/#{email}")
end
#
# Remove email from all books
#
# @param [String] email
# @return [Hash]
#
def remove_email_from_all_books(email)
return handle_error('Empty email') if email.to_s.empty?
send_request("emails/#{email}", 'DELETE')
end
#
# Get email statistic by all campaigns
#
# @param [String] email
# @return [Hash]
#
def email_stat_by_campaigns(email)
return handle_error('Empty email') if email.to_s.empty?
send_request("emails/#{email}/campaigns")
end
#
# Get all emails from blacklist
#
# @return [Hash]
#
def get_black_list
send_request('blacklist')
end
#
# Add email to blacklist
#
# @param [String] emails
# @param [String] comment
# @return [Hash]
#
def add_to_black_list(emails, comment = '')
return handle_error('Empty emails') if emails.to_s.empty?
data = {
emails: Base64.encode64(emails),
comment: comment
}
send_request('blacklist', 'POST', data)
end
#
# Remove emails from blacklist
#
# @param [String] emails
# @return [Hash]
#
def remove_from_black_list(emails)
return handle_error('Empty emails') if emails.to_s.empty?
data = { emails: Base64.encode64(emails) }
send_request('blacklist', 'DELETE', data)
end
#
# Get balance
#
# @param [String] currency
# @return [Hash]
#
def get_balance(currency = '')
url = 'balance'
url += '/' + currency.to_s.upcase unless currency.empty?
send_request(url)
end
#
# SMTP: get list of emails
#
# @param [Fixnum] limit
# @param [Fixnum] offset
# @param [String] from_date
# @param [String] to_date
# @param [String] sender
# @param [String] recipient
# @return [Hash]
#
def smtp_list_emails(limit = 0, offset = 0, from_date = '', to_date = '', sender = '', recipient = '')
data = {
limit: limit,
offset: offset,
from_date: from_date,
to_date: to_date,
sender: sender,
recipient: recipient
}
send_request('/smtp/emails', 'GET', data)
end
#
# SMTP: add emails to unsubscribe list
#
# @param [Fixnum] id
# @return [Hash]
#
def smtp_get_email_info_by_id(id)
return handle_error('Empty id') if id.to_s.empty?
send_request("smtp/emails/#{id}")
end
#
# SMTP: remove emails from unsubscribe list
#
# @param [Hash] emails
# @return [Hash]
#
def smtp_unsubscribe_emails(emails)
return handle_error('Empty emails') if emails.empty?
data = { emails: serialize(emails) }
send_request('smtp/unsubscribe', 'POST', data)
end
#
# SMTP: remove emails from unsubscribe list
#
# @param [Hash] emails
# @return [Hash]
#
def smtp_remove_from_unsubscribe(emails)
return handle_error('Empty emails') if emails.empty?
data = { emails: serialize(emails) }
send_request('smtp/unsubscribe', 'DELETE', data)
end
#
# SMTP: get list of IP
#
# @return [Hash]
#
def smtp_list_ip
send_request('smtp/ips')
end
#
# SMTP: get list of allowed domains
#
# @return [Hash]
#
def smtp_list_allowed_domains
send_request('smtp/domains')
end
#
# SMTP: add new domain
#
# @param [String] email
# @return [Hash]
#
def smtp_add_domain(email)
return handle_error('Empty email') if email.to_s.empty?
data = { email: email }
send_request('smtp/domains', 'POST', data)
end
#
# SMTP: verify domain
#
# @param [String] email
# @return [Hash]
#
def smtp_verify_domain(email)
return handle_error('Empty email') if email.to_s.empty?
send_request("smtp/domains/#{email}")
end
#
# SMTP: send mail
#
# @param [Hash] email
# @return [Hash]
#
def smtp_send_mail(email)
return handle_error('Empty email') if email.empty?
email[:html] = Base64.encode64(email[:html])
data = { email: serialize(email) }
send_request('smtp/emails', 'POST', data)
end
end