Class: NotificationService::Sms::Kaleyra::Service

Inherits:
Object
  • Object
show all
Includes:
APIConfig, Validations
Defined in:
app/services/notification_service/sms/kaleyra/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from APIConfig

#default_params, #endpoint, #headers, #sort_params

Constructor Details

#initializeService

Returns a new instance of Service.



8
9
10
11
# File 'app/services/notification_service/sms/kaleyra/service.rb', line 8

def initialize
  @default_error_message = 'Failed to send SMS using Kaleyra'
  @service_provider = 'kaleyra'
end

Instance Attribute Details

#default_error_messageObject (readonly)

Returns the value of attribute default_error_message.



6
7
8
# File 'app/services/notification_service/sms/kaleyra/service.rb', line 6

def default_error_message
  @default_error_message
end

#service_providerObject (readonly)

Returns the value of attribute service_provider.



6
7
8
# File 'app/services/notification_service/sms/kaleyra/service.rb', line 6

def service_provider
  @service_provider
end

Instance Method Details

#handle_response(params, res, msg, phone, reservation_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'app/services/notification_service/sms/kaleyra/service.rb', line 34

def handle_response(params, res, msg, phone, reservation_id)
  res_json = JSON.parse(res.body)
  data = res_json['data'].present? ? res_json['data'].first : nil

  if sms_sent_successfully?(res, data, res_json)
    handle_successful_sms(msg, phone, reservation_id, params, res_json, data['message_id'])
  else
    handle_sms_not_sent(msg, phone, reservation_id, res_json)
  end
end

#send_sms(msg, phones, reservation_id) ⇒ Boolean

Sends an SMS message to multiple phone numbers.

Parameters:

  • msg (String)

    The message content to be sent.

  • phones (Array<String>)

    An array of phone numbers to send the message to.

  • reservation_id (Integer)

    The ID of the reservation associated with the message.

Returns:

  • (Boolean)

    Returns true if the SMS messages were sent successfully, false otherwise.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/notification_service/sms/kaleyra/service.rb', line 19

def send_sms(msg, phones, reservation_id)
  phones.each do |phone|
    next unless valid_to_send_sms?(phone)

    params = build_params(msg, phone)

    begin
      res = Faraday.post(endpoint, params, headers)
      handle_response(params, res, msg, phone, reservation_id)
    rescue StandardError => e
      handle_error(e, msg, phone, reservation_id)
    end
  end
end

#sms_sent?(message_id) ⇒ Boolean

Checks if an SMS message has been sent.

Parameters:

  • message_id (String)

    The ID of the message to check.

Returns:

  • (Boolean)

    Returns true if the message has been sent, false otherwise.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/notification_service/sms/kaleyra/service.rb', line 49

def sms_sent?(message_id)
  return false if message_id.blank?

  res = fetch_sms_status(message_id)
  return false if res.nil? || res['data'].blank?

  data = res['data'].first
  if ['Delivered', 'SENT - No DLR'].include?(data['status'])
    true
  else
    if send_report_when_fail_send_sms?
      APMErrorHandler.report(default_error_message, message_id: message_id, response: res)
    end

    false
  end
end