rav-meser-api 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rav-meser-api.rb +61 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4761b16181712908553fbc2041d183fecff26740
4
- data.tar.gz: f5245f697b4a48b974b210c729397b7f343753ca
3
+ metadata.gz: e9768fbcdb63add5ea51185a65c75105acab0357
4
+ data.tar.gz: 6c4a975617f575272a2920c2d38a07ad0d0e4a3c
5
5
  SHA512:
6
- metadata.gz: 13d3e087768f3414afb4901a486f8565d685b97b6cb48272957c2e38e4dbdc373dfba9ecdb5030a0eba7080ccb20e61d89907e783f9bb0eaf144830dae8feced
7
- data.tar.gz: bb15e8539a152d68c7bad1f9fb8de193959db5ab5af848c02671e68bfc56e28fb2905ae63cae2a21a8bd06485945b28ac6099b797f2f2b470fd1276610f4a37a
6
+ metadata.gz: 0321ef926979519f7c09384b61c09ba987876039e5ee675fa6c7fe994eebd6e7203efeb2b504fe132a6ba688113daae8a1f12c729221db1327cc7b1706a129bf
7
+ data.tar.gz: 27901ea9251593d2859c13b3997aaab5d83f55cb1b3da78667187e9c407f76e10ff1989cd8205f186fce66a76f953cf71077ab80b037b379d07b6a4b2ea106bb
data/lib/rav-meser-api.rb CHANGED
@@ -4,6 +4,7 @@ require 'json'
4
4
  require 'optparse'
5
5
  require 'pp'
6
6
  require 'uri'
7
+ require 'pry'
7
8
 
8
9
  # gem class name RavMeser
9
10
  class RavMeser
@@ -27,7 +28,7 @@ class RavMeser
27
28
  # get all the lists
28
29
  #
29
30
  # Example:
30
- # >> ה.get_lists()
31
+ # >> RavMeser.get_lists()
31
32
  # => { "LISTS" => [{}, {}, ... ] }
32
33
  #
33
34
  def get_lists
@@ -101,7 +102,7 @@ class RavMeser
101
102
  #
102
103
  # Example:
103
104
  # >> RavMeser.create_subscribers(123456, {0 => {'EMAIL': "sub1@email.com", 'NAME': "sub1"}, 1 => {'EMAIL': "sub2@email.com", 'NAME': "sub2"}} )
104
- # => {"SUBSCRIBERS_CREATED": [], "EMAILS_INVALID": [], "EMAILS_EXISTING": ["johnsmith@gmail.com"], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {}, "ERRORS" : [] }
105
+ # => {"SUBSCRIBERS_CREATED": [112233], "EMAILS_INVALID": [], "EMAILS_EXISTING": ["sub1@email.com"], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {}, "ERRORS" : [] }
105
106
  #
106
107
  # Arguments:
107
108
  # id: (int)
@@ -114,7 +115,7 @@ class RavMeser
114
115
  #
115
116
  # Example:
116
117
  # >> RavMeser.edit_subscribers(123456, {0 => {'IDENTIFIER': "sub1@email.com", 'NAME': "sub1NewName"}, 1 => {'IDENTIFIER': "sub2", 'NAME': "sub2"}} )
117
- # => {"SUBSCRIBERS_UPDATED": [], "INVALID_SUBSCRIBER_IDENTIFIERS": [], "EMAILS_INVALID": [], "EMAILS_EXISTED": ["johnsmith@gmail.com"], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {} }}
118
+ # => {"SUBSCRIBERS_UPDATED": [112233], "INVALID_SUBSCRIBER_IDENTIFIERS": ["sub1@email.com"], "EMAILS_INVALID": [], "EMAILS_EXISTED": [], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {} }
118
119
  #
119
120
  # Arguments:
120
121
  # id: (int)
@@ -136,6 +137,51 @@ class RavMeser
136
137
  send_request(:post, 'subscribers', '/' + id.to_s + '/subscribers?', %w[method delete], args)
137
138
  end
138
139
 
140
+ # create new subscribers in specific list and update subscribers that exists
141
+ #
142
+ # Example:
143
+ # >> RavMeser.upsert_subscribers(123456, {0 => {'EMAIL': "sub1@email.com", 'NAME': "sub1"}, 1 => {'EMAIL': "sub2@email.com", 'NAME': "sub2"}} )
144
+ # => [{"SUBSCRIBERS_CREATED": [112233], "EMAILS_INVALID": [], "EMAILS_EXISTING": ["sub1@email.com"], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {}, "ERRORS" : [] },
145
+ # {"SUBSCRIBERS_UPDATED": [223344], "INVALID_SUBSCRIBER_IDENTIFIERS": [], "EMAILS_INVALID": [], "EMAILS_EXISTED": [], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {} }]
146
+ #
147
+ # Arguments:
148
+ # id: (int)
149
+ # args: (Hash)
150
+ def upsert_subscribers(id, args = {})
151
+ create_response = send_request(:post, 'subscribers', '/' + id.to_s + '/subscribers', [], args)
152
+
153
+ update_args = {}
154
+ create_response['EMAILS_EXISTING'].each_with_index do |email, index|
155
+ update_args[index] = get_subscriber_args_and_set_identifier(args, email)
156
+ end
157
+ edit_response = send_request(:put, 'subscribers', '/' + id.to_s + '/subscribers', [], update_args)
158
+
159
+ [create_response, edit_response]
160
+ end
161
+
162
+ # create new subscriber in specific list - update the subscriber if already exists
163
+ #
164
+ # Example:
165
+ # >> RavMeser.upsert_subscriber(123456, {0 => {'EMAIL': "sub1@email.com", 'NAME': "sub1"} )
166
+ # => [{"SUBSCRIBERS_CREATED": [], "EMAILS_INVALID": [], "EMAILS_EXISTING": ["sub1@email.com"], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {}, "ERRORS" : [] },
167
+ # {"SUBSCRIBERS_UPDATED": [223344], "INVALID_SUBSCRIBER_IDENTIFIERS": [], "EMAILS_INVALID": [], "EMAILS_EXISTED": [], "EMAILS_BANNED": [], "PHONES_INVALID": [], "PHONES_EXISTING": [], "BAD_PERSONAL_FIELDS": {} }]
168
+ #
169
+ # Arguments:
170
+ # id: (int)
171
+ # args: (Hash)
172
+ def upsert_subscriber(id, args = {})
173
+ return { error: 'Too many subscribers' } unless args.length == 1
174
+
175
+ create_response = send_request(:post, 'subscribers', '/' + id.to_s + '/subscribers', [], args)
176
+ if create_response['EMAILS_EXISTING'].length == 1
177
+ args[0][:IDENTIFIER] = args[0].delete(:EMAIL)
178
+ edit_response = send_request(:put, 'subscribers', '/' + id.to_s + '/subscribers', [], args)
179
+ end
180
+
181
+ [create_response, edit_response || {}]
182
+ end
183
+
184
+
139
185
  # <!----------- PERSONAL FIELDS -----------!>
140
186
 
141
187
  # get personal fields from specific list
@@ -226,10 +272,12 @@ class RavMeser
226
272
  # args: (Hash)
227
273
  def create_and_send_message(id, msg)
228
274
  res = create_message(id, msg)
229
- send_message(id, res["MESSAGE_ID"])
275
+ send_message(id, res['MESSAGE_ID'])
230
276
  end
231
277
 
232
- # privare method
278
+ private
279
+
280
+ # private method
233
281
  # common code to send the requests
234
282
  #
235
283
  # Example:
@@ -255,5 +303,12 @@ class RavMeser
255
303
  response
256
304
  end
257
305
 
258
- private :send_request
306
+ def get_subscriber_args_and_set_identifier(args, email)
307
+ args.each do |_, v|
308
+ if v[:EMAIL] == email
309
+ v[:IDENTIFIER] = v.delete(:EMAIL)
310
+ return v
311
+ end
312
+ end
313
+ end
259
314
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rav-meser-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Itamar Davidyan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-25 00:00:00.000000000 Z
11
+ date: 2019-09-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rav Meser API Ruby
14
14
  email: itamardavidyan@gmail.com