Sha256: 59565264117cffa8685b51529f8eef9199b8f9bb6900a1afbfff41f6ebe4d097

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'httparty'
require 'smspartner/response'

module Smspartner
  class Client

    def initialize(config)
      @config = config
    end

    SEND_SMS_URL = 'https://api.smspartner.fr/v1/send'.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

    # @param to     [String] phone number
    # @param body   [String] SMS body
    # @param config [Hash]   overrides to config
    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

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

    private

    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

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smspartner-0.1.0 lib/smspartner/client.rb