maxemail_api 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c535e4c357aee80beca7c417bd9227a6298d02f
4
- data.tar.gz: 47235986f230dd0d919730b22a90bb53e1da0080
3
+ metadata.gz: 532d04e35daedfca7a6598f8b668548e792f7ade
4
+ data.tar.gz: dc0228aa78feb5ede97bd14a5e104909446d03f4
5
5
  SHA512:
6
- metadata.gz: 20395d82c76c386b2e173277c1d8b0926b81d9beff7d8c8b5aa9e4a60573ac56715dc0353d58fd6e8a4ff67387c788c76274efa83ca1ef4cdc4603086080efd2
7
- data.tar.gz: 06572aef5194f213714a5a06a78744ccbb9b805dc3e9384a9b605aca174f436fa7a8243b0836085a033ad632f21f4c00b9efe30096d56765de5485e4dfe76632
6
+ metadata.gz: b425180553db383a4a1168e900de729ba1f3001c4c1504e0c898fcf9e5447d0e6a3d5fbe71a62b7ea656db4d2b09fb61d7cf1b69e80b95a4b627a17e5e09d74f
7
+ data.tar.gz: d81f90b604b2516f2cda763adb7aaae9f287795269da23029d7b9920a218d004c010a514d9c71f556e78537fafb9228819f7be95ef4ae49afd97f6a7b212ec12
@@ -0,0 +1,23 @@
1
+ class MaxemailApiResponse
2
+ attr_accessor :data
3
+
4
+ def initialize(data:, success:, message:)
5
+ @data = data
6
+ @data[:success] = success
7
+ @data[:message] = message
8
+ end
9
+
10
+ def to_json
11
+ {
12
+ data: @data
13
+ }.to_json
14
+ end
15
+
16
+ def successful?
17
+ @data[:success]
18
+ end
19
+
20
+ def message
21
+ @data[:message]
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module MaxemailApiShared
2
+ class << self
3
+ def send_request(params:, method:)
4
+ HTTP.headers(authentication_header)
5
+ .get("#{ENV['MAXEMAIL_API_URL']}#{method}",
6
+ params: params)
7
+ end
8
+
9
+ private
10
+
11
+ def authentication_header
12
+ { Authorization: 'Basic ' + Base64.encode64(ENV['MAXEMAIL_USERNAME'] + ':' + ENV['MAXEMAIL_PASSWORD']).chomp("\n") }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ module MaxemailApiSubscriptions
2
+ extend self
3
+ include MaxemailApiShared
4
+
5
+ def subscribe(email_address:, list_id:)
6
+ return MaxemailApiShared.send_request(params: { method: 'insertRecipient', data: { email_address: email_address, subscribed: 1 }.to_json, listID: list_id }, method: 'list') unless inserted?(email_address: email_address, list_id: list_id)
7
+ MaxemailApiShared.send_request(params: { method: 'updateRecipient', data: { email_address: email_address, subscribed: 1 }.to_json, listID: list_id, recipientId: find_recipient_id(email_address: email_address) }, method: 'list')
8
+ end
9
+
10
+ def subscribed?(email_address:, list_id:)
11
+ response = MaxemailApiShared.send_request(params: { method: 'fetchRecipient', listID: list_id, recipientId: find_recipient_id(email_address: email_address) }, method: 'list')
12
+ return false if response.body.to_s == 'false'
13
+ response = JSON.parse(response.body)
14
+ return false if response['success'].to_s == 'false'
15
+ return false if response['subscribed'].to_s == '0'
16
+ return true if response['subscribed'].to_s == '1'
17
+ rescue StandardError
18
+ false
19
+ end
20
+
21
+ def inserted?(email_address:, list_id:)
22
+ response = MaxemailApiShared.send_request(params: { method: 'fetchRecipient', listID: list_id, recipientId: find_recipient_id(email_address: email_address) }, method: 'list')
23
+ return false if response.body.to_s == 'false'
24
+ response = JSON.parse(response.body)
25
+ return false if response['success'].to_s == 'false'
26
+ return true if response['subscribed'].to_s == '0'
27
+ return true if response['subscribed'].to_s == '1'
28
+ rescue StandardError
29
+ false
30
+ end
31
+
32
+ def unsubscribe(email_address: nil, recipient_id: nil, list_id:)
33
+ recipient_id = find_recipient_id(email_address: email_address) if recipient_id.nil?
34
+ MaxemailApiShared.send_request(params: { method: 'updateRecipient', data: { email_address: email_address, subscribed: 0 }.to_json, listID: list_id, recipientId: recipient_id }, method: 'list')
35
+ end
36
+
37
+ def subscriptions(email_address: nil, recipient_id: nil, list_id:)
38
+ recipient_id = find_recipient_id(email_address: email_address) if recipient_id.nil?
39
+ MaxemailApiShared.send_request(params: { method: 'fetchLists', listID: list_id, recipientId: recipient_id }, method: 'recipient')
40
+ end
41
+
42
+ def find_recipient_id(email_address: nil)
43
+ MaxemailApiShared.send_request(params: { method: 'findByEmailAddress', emailAddress: email_address }, method: 'recipient')
44
+ end
45
+
46
+ def available_subscriptions
47
+ lists = JSON.parse(fetch_list)
48
+ lists.delete_if { |list| list['type'] != 'include' }
49
+ end
50
+
51
+ def fetch_list
52
+ MaxemailApiShared.send_request(params: { method: 'fetchAll' }, method: 'list')
53
+ end
54
+ end
@@ -0,0 +1,77 @@
1
+ module MaxemailApiTriggered
2
+ extend self
3
+ include MaxemailApiShared
4
+
5
+ def send(folder_name: nil, email_name: nil, email_id: nil, email_address:, profile_data:)
6
+ email_id = MaxemailApiTriggered.find_email_id(folder_name: folder_name, email_name: email_name) if email_id.nil?
7
+ MaxemailApiTriggered.send_triggered(email_address: email_address, email_id: email_id, profile_data: profile_data)
8
+ end
9
+
10
+ def send_triggered(email_address: nil, email_id: nil, profile_data: nil)
11
+ return MaxemailApiResponse.new(data: {}, success: false, message: 'Template not found') if email_id.nil?
12
+ return MaxemailApiResponse.new(data: {}, success: false, message: 'Invalid Profile data') unless valid_profile_data?(profile_data)
13
+ return MaxemailApiResponse.new(data: {}, success: false, message: 'Invalid Email address') unless valid_email?(email_address)
14
+ response = JSON.parse(MaxemailApiShared.send_request(params: { method: 'trigger',
15
+ emailAddress: email_address,
16
+ emailId: email_id,
17
+ profileData: profile_data.to_json }, method: 'email_send').body)
18
+ return MaxemailApiResponse.new(data: {}, success: true, message: 'Mail sent') if response['success'] == true
19
+ return MaxemailApiResponse.new(data: {}, success: false, message: 'Server error') if response['success'] == false
20
+ rescue StandardError => e
21
+ puts 'MaxemailApiResponse Error:'
22
+ puts e
23
+ puts 'END MaxemailApiResponse Error:'
24
+ return MaxemailApiResponse.new(data: {}, success: false, message: 'Server error') if response['success'] == false
25
+ end
26
+
27
+ def find_email_id(folder_name: nil, email_name: nil)
28
+ folder_id = find_folder_id(folder_name: folder_name)
29
+ response = JSON.parse(fetch_tree(tree: 'email', node_id: folder_id, node_class: 'folder'))
30
+ response.map do |node|
31
+ node['nodeId'].to_i if node['text'] == email_name
32
+ end.compact.first
33
+ rescue StandardError
34
+ nil
35
+ end
36
+
37
+ def fetch_root(tree: 'email', children: '[]')
38
+ MaxemailApiShared.send_request(params: { method: 'fetchRoot',
39
+ tree: tree,
40
+ children: children }, method: 'tree')
41
+ end
42
+
43
+ def fetch_tree(tree: 'email', node_id: nil, node_class: 'folder')
44
+ MaxemailApiShared.send_request(params: { method: 'fetchTree',
45
+ tree: tree,
46
+ nodeId: node_id,
47
+ nodeClass: node_class }, method: 'tree')
48
+ end
49
+
50
+ def fetch_trigged(folder_id: nil)
51
+ MaxemailApiShared.send_request(params: { method: 'fetchAll',
52
+ folderId: folder_id }, method: 'email_triggered')
53
+ end
54
+
55
+ def find_folder_id(folder_name: nil)
56
+ response = JSON.parse(fetch_root)
57
+ response[0]['children'].map do |node|
58
+ node['nodeId'].to_i if node['text'] == folder_name
59
+ end.compact.first
60
+ rescue StandardError
61
+ nil
62
+ end
63
+
64
+ def valid_email?(email_address)
65
+ valid = '[A-Za-z\d.+-]+'
66
+ (email_address =~ /#{valid}@#{valid}\.#{valid}/).zero?
67
+ rescue StandardError
68
+ false
69
+ end
70
+
71
+ def valid_profile_data?(profile_data)
72
+ JSON.parse(profile_data.to_s)
73
+ true
74
+ rescue StandardError
75
+ false
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maxemail_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Murray Catto
@@ -31,6 +31,10 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/maxemail_api.rb
34
+ - lib/maxemail_api/response.rb
35
+ - lib/maxemail_api/shared.rb
36
+ - lib/maxemail_api/subscriptions.rb
37
+ - lib/maxemail_api/triggered.rb
34
38
  homepage:
35
39
  licenses: []
36
40
  metadata: {}