Class: Smspartner::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/smspartner/client.rb

Constant Summary collapse

SEND_SMS_URL =
'https://api.smspartner.fr/v1/send'.freeze
SMS_STATUS_URL =
'https://api.smspartner.fr/v1/message-status'.freeze
MY_DATA_URL =
'https://api.smspartner.fr/v1/me'.freeze
ALLOWED_CONFIG_OVERRIDE =
%i[sandbox sender range_value].freeze
RANGE_VALUES =
{ premium: 1, low_cost: 2 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/smspartner/client.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#meObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smspartner/client.rb', line 38

def me
  HTTParty.get(
    MY_DATA_URL,
    query:   {
      apiKey: @config.api_key
    },
    headers: {
      content_type: 'application/json'
    }
  ).parsed_response
end

#send_sms(to:, body:, **config) ⇒ Object

Parameters:

  • to (String)

    phone number

  • body (String)

    SMS body

  • config (Hash)

    overrides to config

Raises:

  • (SmsSendError)


24
25
26
27
28
29
# File 'lib/smspartner/client.rb', line 24

def send_sms(to:, body:, **config)
  res = send_request(to, body, config)
  ret = Response.new(res.parsed_response)
  raise SmsSendError.new(ret) if !ret.success? && config[:raise_on_error]
  ret
end

#sms_status(to:, message_id:, **config) ⇒ Object

Raises:

  • (SmsStatusError)


31
32
33
34
35
36
# File 'lib/smspartner/client.rb', line 31

def sms_status(to:, message_id:, **config)
  res = status_request(to, message_id, config)
  ret = Response.new(res.parsed_response)
  raise SmsStatusError.new(ret) if !ret.success? && config[:raise_on_error]
  ret
end