class Smspartner::Client

Constants

ALLOWED_CONFIG_OVERRIDE
MY_DATA_URL
RANGE_VALUES
SEND_SMS_URL
SMS_STATUS_URL

Public Class Methods

new(config) click to toggle source
# File lib/smspartner/client.rb, line 7
def initialize(config)
  @config = config
end

Public Instance Methods

me() click to toggle source
# 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) click to toggle source

@param to [String] phone number @param body [String] SMS body @param config [Hash] overrides to config

# 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) click to toggle source
# 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

Private Instance Methods

send_request(to, body, config) click to toggle source
# File lib/smspartner/client.rb, line 52
def send_request(to, body, config)
  HTTParty.post(
    SEND_SMS_URL,
    body:    sms_json(
      to,
      body,
      config.select { |k, _v| ALLOWED_CONFIG_OVERRIDE.include?(k) }.compact
    ).to_json,
    headers: {
      content_type: 'application/json'
    }
  )
end
sms_json(to, body, config) click to toggle source
# File lib/smspartner/client.rb, line 78
def sms_json(to, body, config)
  final_config = @config.to_h.merge!(config)

  json = {
    apiKey:       final_config[:api_key],
    message:      body,
    phoneNumbers: to,
    gamme:        RANGE_VALUES[final_config[:range_value]],
    sender:       final_config[:sender]
  }
  json.compact!
  json[:sandbox] = 1 if final_config[:sandbox]

  json
end
status_request(to, message_id, config) click to toggle source
# File lib/smspartner/client.rb, line 66
def status_request(to, message_id, config)
  final_config = @config.to_h.merge!(config)
  HTTParty.get(
    SMS_STATUS_URL,
    query: {
      phoneNumber: to,
      messageId:   message_id,
      apiKey:      final_config[:api_key]
    }
  )
end