Class: Dabrab

Inherits:
Object
  • Object
show all
Defined in:
lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_level) ⇒ Dabrab

Returns a new instance of Dabrab.



7
8
9
10
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 7

def initialize(log_level)
  @internal_func = InternalFunc.new
  @log_level = log_level
end

Instance Attribute Details

#internal_funcObject

Returns the value of attribute internal_func.



5
6
7
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 5

def internal_func
  @internal_func
end

#log_levelObject

Returns the value of attribute log_level.



5
6
7
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 5

def log_level
  @log_level
end

Instance Method Details

#dabrabyt_text_field_parse(stri, viberttl) ⇒ Object



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
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 12

def dabrabyt_text_field_parse(stri, viberttl)
  button_url = ""
  image = ""
  button_text = ""
  text = stri

  #############
  #0 - Text only
  #1 - Text + Button
  #2 - Text + Image + Button
  #3 - image only
  #############
  scheme = 0
  scheme_dic = {0 => "Text only", 1 => "Text+Button", 2 => "Text+Image+Button", 3 => "Image only"}
  #############

  output_answer = {}
  sk_format = {}

  begin

    regexp_image1 = /(image:(\S+)[\s\n]?)/
    button_url1 = /(button_url:(\S+)[\s\n]?)/
    button_text1 = /(button_text:["']([^"]+)["'][\s\n])/

    button_url_dat = stri.scan(button_url1)
    button_text_dat = stri.scan(button_text1)
    image_dat = stri.scan(regexp_image1)


    #p button_url_dat
    #p button_text_dat
    #p image_dat

    begin
      if button_url_dat != [] && button_url_dat != nil && button_url_dat != ""
        button_url = button_url_dat[0][1]
        text = text.gsub(button_url_dat[0][0], "")
      end
    rescue
      nil
    end

    begin
      if button_text_dat != [] && button_text_dat != nil && button_text_dat != ""
        button_text = button_text_dat[0][1]
        text = text.gsub(button_text_dat[0][0], "")
      end
    rescue
      nil
    end

    begin
      if image_dat != [] && image_dat != nil && image_dat != ""
        image = image_dat[0][1]
        text = text.gsub(image_dat[0][0], "")
      end
    rescue
      nil
    end

    #corrected text rewrite for arutskiy. commented 20210921
    #we don't need do any text changes
    #text = text.gsub(/\s+$/, '').gsub(/\n$/, '')


    if button_url != "" && button_text != "" && image != "" #111
      scheme = 2
    elsif button_url != "" && button_text != "" && image == "" #110
      scheme = 1
    elsif button_url == "" && button_text == "" && image != "" #001
      if text == ""
        scheme = 3
      else
        scheme = 2
        button_url = image
        button_text = "Открыть"
      end
    elsif button_url != "" && button_text == "" && image != "" #101
      scheme = 2
      button_text = "Открыть"
    elsif button_url == "" && button_text != "" && image != "" #011
      scheme = 2
      button_url = image
    elsif button_url == "" && button_text != "" && image == "" #010
      scheme = 0
    elsif button_url != "" && button_text == "" && image == "" #100
      button_text = "Открыть"
      scheme = 1
    else
      scheme = 0
    end

    case scheme
      when 0
        sk_format = {"text": text, "ttl": viberttl}
      when 1
        sk_format = {"text": text, "ttl": viberttl, "caption": button_text, "action": button_url}
      when 2
        sk_format = {"text": text, "ttl": viberttl, "caption": button_text, "action": button_url, "img": image}
      when 3
        sk_format = {"img": image, "ttl": viberttl}
      else
        nil
    end

    #p sk_format

    output_answer ={:code => 200, :result => "Success", :body => {:to_sk_format => sk_format, :button_url => button_url, :image => image, :button_text => button_text, :text => text, :scheme => scheme, :scheme_dic => scheme_dic[scheme]}}
  rescue
    output_answer ={:code => 507, :result => "dabrabyt_text_field_parse: Unknown error"}
  end
  output_answer
end

#delivery_report_transform(hub_report_in) ⇒ Object



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
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 205

def delivery_report_transform(hub_report_in)

  internal_func.printer_texter({:input => "Start delivery_report_transform", :procedure => "Dabrab.delivery_report_transform"}, log_level)

#{"number"=>"375297116638", "time"=>1588673492000, "status"=>2, "substatus"=>23, "msg_status"=>23033, "message_id"=>"486649ba-a573-4ee9-8f58-018ed20ca6fd", "extra_id"=>"444/0/ServiceModel/ScGetReports.svc/GetReports", "sent_via"=>"viber", "controller"=>"reports", "action"=>"delivery", "report"=>{"number"=>"375297116638", "time"=>1588673492000, "status"=>2, "substatus"=>23, "msg_status"=>23033, "message_id"=>"486649ba-a573-4ee9-8f58-018ed20ca6fd", "extra_id"=>"444/0/ServiceModel/ScGetReports.svc/GetReports", "sent_via"=>"viber"}}
#p hub_report
#p "hub_report"
  report_to_recip = []

  data_for_status = {
      23011 => {:status_id => 5, :status_grid => 3, :status_grname => "DELIVERED", :status_name => "DELIVERED_TO_HANDSET", :status_descr => "Message delivered by SMS",
                :error_id => 0, :error_grid => 0, :error_grname => "Ok", :error_name => "NO_ERROR", :error_descr => "No Error", :error_permanent => false
      },
      23033 => {:status_id => 5, :status_grid => 3, :status_grname => "DELIVERED", :status_name => "DELIVERED_TO_HANDSET", :status_descr => "Message delivered by Viber",
                :error_id => 0, :error_grid => 0, :error_grname => "Ok", :error_name => "NO_ERROR", :error_descr => "No Error", :error_permanent => false
      },
      35015 => {:status_id => 15, :status_grid => 4, :status_grname => "EXPIRED", :status_name => "EXPIRED_EXPIRED", :status_descr => "SMS Message TTL EXPIRED",
                :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_ABSENT_SUBSCRIBER_SM", :error_descr => "Subscriber is absent", :error_permanent => false
      }, #message SMS expired
      36463 => {:status_id => 15, :status_grid => 4, :status_grname => "EXPIRED", :status_name => "EXPIRED_EXPIRED", :status_descr => "Viber Message TTL EXPIRED",
                :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_ABSENT_SUBSCRIBER_SM", :error_descr => "Subscriber is absent.", :error_permanent => false
      }, #message Viber expired
      36010 => {:status_id => 4, :status_grid => 2, :status_grname => "UNDELIVERABLE", :status_name => "UNDELIVERABLE_REJECTED_OPERATOR", :status_descr => "SMS. Subscriber not exists",
                :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_UNKNOWN_SUBSCRIBER", :error_descr => "Subscriber not found.", :error_permanent => true
      }, #SMS subscriber not exists
      36131 => {:status_id => 4, :status_grid => 2, :status_grname => "UNDELIVERABLE", :status_name => "UNDELIVERABLE_REJECTED_OPERATOR", :status_descr => "SMS. Unknown subscriber",
                :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_UNKNOWN_SUBSCRIBER", :error_descr => "Subscriber not found.", :error_permanent => true
      }, #SMS unknown subscriber
      36505 => {:status_id => 4, :status_grid => 2, :status_grname => "UNDELIVERABLE", :status_name => "UNDELIVERABLE_REJECTED_OPERATOR", :status_descr => "Viber. Subscriber not exists",
                :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_UNKNOWN_SUBSCRIBER", :error_descr => "Subscriber not found.", :error_permanent => true
      } #Viber subscriber not exists
  }

  data_for_status.default = {:status_id => 9,
                             :status_grid => 2,
                             :status_grname => 'UNDELIVERABLE_NOT_DELIVERED',
                             :status_name => 'UNDELIVERED',
                             :status_descr => 'Unknown error default',
                             :error_id => 32,
                             :error_grid => 1,
                             :error_grname => 'HANDSET_ERRORS',
                             :error_name => 'EC_SM_DELIVERY_FAILURE',
                             :error_descr => 'Unknown error default',
                             :error_permanent => false
  }


  if hub_report_in.key?("msg_status") && !hub_report_in.key?("_json") && !hub_report_in["report"].key?("_json")
    internal_func.printer_texter({:input => "One message delivery_report_transform", :procedure => "Dabrab.delivery_report_transform"}, log_level)

    hub_report = hub_report_in

    if data_for_status[hub_report["msg_status"]] != nil && data_for_status[hub_report["msg_status"]] != {} && data_for_status[hub_report["msg_status"]] != [] && data_for_status[hub_report["msg_status"]] != ""

      report_to_recip.push({

                               "bulkId": "00000000-0000-0000-0000-000000000000",
                               "messageId": hub_report["report"]["message_id"],
                               "to": hub_report["report"]["number"],
                               #    "sentAt": "2020-05-05T20:00:00.000+0000",
                               #    "doneAt": "2020-05-05T20:00:00.000+0000",
                               "status": {
                                   "groupId": data_for_status[hub_report["msg_status"]][:status_grid],
                                   "groupName": data_for_status[hub_report["msg_status"]][:status_grname],
                                   "id": data_for_status[hub_report["msg_status"]][:status_id],
                                   "name": data_for_status[hub_report["msg_status"]][:status_name],
                                   "description": data_for_status[hub_report["msg_status"]][:status_descr]
                               },
                               "error": {
                                   "groupId": data_for_status[hub_report["msg_status"]][:error_grid],
                                   "groupName": data_for_status[hub_report["msg_status"]][:error_grname],
                                   "id": data_for_status[hub_report["msg_status"]][:error_id],
                                   "name": data_for_status[hub_report["msg_status"]][:error_name],
                                   "description": data_for_status[hub_report["msg_status"]][:error_descr],
                                   "permanent": data_for_status[hub_report["msg_status"]][:error_permanent]
                               }
                           })


    else

      report_to_recip.push({

                               "bulkId": "00000000-0000-0000-0000-000000000000",
                               "messageId": hub_report["report"]["message_id"],
                               "to": hub_report["report"]["number"],
                               #    "sentAt": "2020-05-05T20:00:00.000+0000",
                               #    "doneAt": "2020-05-05T20:00:00.000+0000",
                               "status": {
                                   "groupId": 2,
                                   "groupName": "UNDELIVERABLE_NOT_DELIVERED",
                                   "id": 9,
                                   "name": "UNDELIVERED",
                                   "description": "Unknown error"
                               },
                               "error": {
                                   "groupId": 1,
                                   "groupName": "HANDSET_ERRORS",
                                   "id": 32,
                                   "name": "EC_SM_DELIVERY_FAILURE",
                                   "description": "Unknown error",
                                   "permanent": false
                               }
                           })

    end

  else

    hub_report_in["_json"].each {|hub_report2|

      internal_func.printer_texter({:input => "Start Mass message delivery_report_transform", :procedure => "Dabrab.delivery_report_transform"}, log_level)

      internal_func.printer_texter({:input => {:hub_report2 => hub_report2}, :procedure => "Dabrab.delivery_report_transform"}, log_level)


      if data_for_status[hub_report2["msg_status"]] != nil && data_for_status[hub_report2["msg_status"]] != {} && data_for_status[hub_report2["msg_status"]] != [] && data_for_status[hub_report2["msg_status"]] != ''
        report_to_recip.push({

                                 "bulkId": "00000000-0000-0000-0000-000000000000",
                                 "messageId": hub_report2["message_id"],
                                 "to": hub_report2["number"],
                                 #    "sentAt": "2020-05-05T20:00:00.000+0000",
                                 #    "doneAt": "2020-05-05T20:00:00.000+0000",
                                 "status": {
                                     "groupId": data_for_status[hub_report2["msg_status"]][:status_grid],
                                     "groupName": data_for_status[hub_report2["msg_status"]][:status_grname],
                                     "id": data_for_status[hub_report2["msg_status"]][:status_id],
                                     "name": data_for_status[hub_report2["msg_status"]][:status_name],
                                     "description": data_for_status[hub_report2["msg_status"]][:status_descr]
                                 },
                                 "error": {
                                     "groupId": data_for_status[hub_report2["msg_status"]][:error_grid],
                                     "groupName": data_for_status[hub_report2["msg_status"]][:error_grname],
                                     "id": data_for_status[hub_report2["msg_status"]][:error_id],
                                     "name": data_for_status[hub_report2["msg_status"]][:error_name],
                                     "description": data_for_status[hub_report2["msg_status"]][:error_descr],
                                     "permanent": data_for_status[hub_report2["msg_status"]][:error_permanent]
                                 }
                             })


      else

        report_to_recip.push({

                                 "bulkId": "00000000-0000-0000-0000-000000000000",
                                 "messageId": hub_report2["message_id"],
                                 "to": hub_report2["number"],
                                 #    "sentAt": "2020-05-05T20:00:00.000+0000",
                                 #    "doneAt": "2020-05-05T20:00:00.000+0000",
                                 "status": {
                                     "groupId": 2,
                                     "groupName": "UNDELIVERABLE_NOT_DELIVERED",
                                     "id": 9,
                                     "name": "UNDELIVERED",
                                     "description": "Unknown error"
                                 },
                                 "error": {
                                     "groupId": 1,
                                     "groupName": "HANDSET_ERRORS",
                                     "id": 32,
                                     "name": "EC_SM_DELIVERY_FAILURE",
                                     "description": "Unknown error",
                                     "permanent": false
                                 }
                             })
      end

    }
  end

  {:code => 200, :result => "Data processed", :body => {:report_to_recip => {"results": report_to_recip}}}
end

#num_formatter(infobip_msisdn, extra_id) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 128

def num_formatter(infobip_msisdn, extra_id)
  #[{"to"=>{"phoneNumber"=>"375297116638", "emailAddress"=>nil}}]
  # [{"phone_number": 375298766719}, {"phone_number": 375295730878}]

  converted_msisdn_list = []

  for g in infobip_msisdn
    begin
      converted_msisdn_list.append({
                                       "phone_number": g["to"]["phoneNumber"].to_i
                                       #"extra_id": extra_id
                                   })
    rescue
      nil
      #p "failed: #{g.to_s}"
    end

  end
  #p converted_msisdn_list
  converted_msisdn_list

end

#send_responce_formatter(hub_answer) ⇒ Object



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
# File 'lib/imperituroard/projects/mhub/subs/dabrab/dabrab_proced.rb', line 152

def send_responce_formatter(hub_answer)
  input = {:hub_answer => hub_answer}
  mess_to_recip = {}
  messages_sect = []
  example = {
      "bulkId": "2034072219640523072",
      "messages": [{
                       "to": "41793026727",
                       "status": {
                           "groupId": 1,
                           "groupName": "PENDING",
                           "id": 26,
                           "name": "MESSAGE_ACCEPTED",
                           "description": "Message sent to next instance"
                       },
                       "messageId": "2250be2d4219-3af1-78856-aabe-1362af1edfd2"
                   }
      ]
  }

  #p "send_responce_formatter(hub_answer)"
  #p hub_answer

  for a in hub_answer[:body][:res_body]["messages"]
    status1 = {}
    if a['accepted']
      status1 = {"to": {"phoneNumber": a["phone_number"]},
                 "status": {"groupId": 1,
                            "groupName": "PENDING",
                            "id": 26,
                            "name": "MESSAGE_ACCEPTED",
                            "description": "Message sent to next instance"},
                 "messageId": a["message_id"]}
    else
      status1 = {"to": {"phoneNumber": a["phone_number"]},
                 "status": {"groupId": 5,
                            "groupName": "REJECTED",
                            "id": 21,
                            "name": "REJECTED_SYSTEM_ERROR",
                            "description": "Unknown error"},
                 "messageId": a["message_id"]}
    end

    messages_sect.append(status1)
  end

  #p messages_sect

  mess_to_recip = {"bulkId": "00000000-0000-0000-0000-000000000000", "messages": messages_sect}
  {:code => 200, :result => "Data processed", :body => {:mess_to_recip => mess_to_recip}}
end