Class: Kidsmin::SmsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/kidsmin/sms_client.rb

Constant Summary collapse

TWILIO_BASE =
"https://api.twilio.com/2010-04-01"

Class Method Summary collapse

Class Method Details

.configured?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'lib/kidsmin/sms_client.rb', line 8

def self.configured?
  cfg = Kidsmin.configuration
  cfg..present? &&
    cfg.twilio_auth_token.present? &&
    cfg.twilio_from_number.present?
end

.send(to:, body:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kidsmin/sms_client.rb', line 15

def self.send(to:, body:)
  return false unless configured?

  cfg = Kidsmin.configuration
  url = "#{TWILIO_BASE}/Accounts/#{cfg.}/Messages.json"

  response = HTTParty.post(url,
    basic_auth: { username: cfg., password: cfg.twilio_auth_token },
    body: { From: cfg.twilio_from_number, To: to, Body: body }
  )

  unless response.success?
    Rails.logger.error("[Kidsmin::SmsClient] Twilio error #{response.code}: #{response.body}")
    return false
  end

  Rails.logger.info("[Kidsmin::SmsClient] SMS sent to #{to}")
  true
rescue => e
  Rails.logger.error("[Kidsmin::SmsClient] #{e.message}")
  false
end