Class: ShortMessage::Message

Inherits:
ApplicationRecord show all
Defined in:
app/models/short_message/message.rb

Instance Method Summary collapse

Instance Method Details

#deliverObject



8
9
10
11
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
# File 'app/models/short_message/message.rb', line 8

def deliver
  self.sender = ShortMessage.config.default_sms_sender if self.sender.blank?
  normalize_addresses!
  return false if errors.any?
  return false if self.recipient.blank? || self.text.blank?
  return false if ShortMessage.config.api_key.to_s.strip.empty?

  response = send_v2_request
  parsed = parse_delivery_response(response)
  (response, parsed)

  if delivery_successful?(response, parsed)
    self.message_key = parsed[:message_key] if parsed[:message_key].present?
    ActiveSupport::Notifications.instrument("short_message.delivered", key: self.message_key)
    return self.save
  else
    if response.code.to_i == 402 && ShortMessage.config.reload_notification_email.present?
      ShortMessage::Mailer.payment_required_notification.deliver_now
    end

    Rails.logger.error("ShortMessage delivery failed: HTTP #{response.code}, body=#{response.body}")
    send_error_notification(response)
    return false
  end
rescue StandardError => e
  Rails.logger.error("ShortMessage delivery raised #{e.class}: #{e.message}")
  send_error_notification(e)
  false
end

#status_textObject



38
39
40
# File 'app/models/short_message/message.rb', line 38

def status_text
  I18n.t("short_message.status.code_#{self.status_code}")
end