Class: NotificationService::Sms::Alibaba::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from APIConfig

#access_key_secret, #default_params, #endpoint, #http_method, #separator, #sort_params

Constructor Details

#initializeService

Returns a new instance of Service.



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

def initialize
  @auth = Authentication.new
  @default_error_message = 'Failed to send SMS using Alibaba'
  @service_provider = 'alibaba'
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



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

def auth
  @auth
end

#default_error_messageObject (readonly)

Returns the value of attribute default_error_message.



6
7
8
# File 'app/services/notification_service/sms/alibaba/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/alibaba/service.rb', line 6

def service_provider
  @service_provider
end

Instance Method Details

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



30
31
32
33
34
35
36
37
38
39
# File 'app/services/notification_service/sms/alibaba/service.rb', line 30

def handle_response(params, response, msg, phone, reservation_id)
  res_json = JSON.parse(response.body)
  if sms_not_sent?(response, res_json)
    handle_sms_not_sent(msg, phone, reservation_id, res_json)
  else
    handle_successful_sms(msg, phone, reservation_id, params, res_json)
  end
rescue StandardError => e
  handle_error(e, msg, phone, reservation_id)
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.



20
21
22
23
24
25
26
27
28
# File 'app/services/notification_service/sms/alibaba/service.rb', line 20

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

    params = generate_request_params(phone, msg)
    res = Faraday.get(endpoint, params)
    handle_response(params, res, msg, phone, reservation_id)
  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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/notification_service/sms/alibaba/service.rb', line 45

def sms_sent?(message_id)
  params = generate_request_status_params(message_id)
  res = Faraday.get(endpoint, params)
  res_json = JSON.parse(res.body)

  if sms_not_sent?(res, res_json)
    if send_report_when_fail_send_sms?
      APMErrorHandler.report(default_error_message, message_id: message_id, response: res_json)
    end

    return false
  end

  res_json['ErrorCode'] == 'DELIVERED'
rescue StandardError => e
  HH_LOGGER.error(default_error_message, error: e, message_id: message_id)
  false
end