Sha256: 205fd9c77ceee53c5e3e97d5411e3983e0ec493723eb8179c814409d8d1993d1

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'httparty'
require 'ostruct'

module Voicepartner
  class Client

    def initialize(config)
      @api_key = config.fetch(:api_key)
    end

    def send_vocal_message(to:, message_text:)
      res = send_request(
        phoneNumbers: to,
        text:         message_text
      )

      OpenStruct.new(
        raw_response: res.parsed_response,
        success:      res.parsed_response&.[]('success'),
        rate_limit:   {
          limit:     extract_header(res.headers['x-ratelimit-limit']),
          remaining: extract_header(res.headers['x-ratelimit-remaining']),
          resets_at: extract_header(res.headers['x-ratelimit-reset'])
        }
      )
    end

    private

    def send_request(payload)
      HTTParty.post(
        SEND_VOCAL_MESSAGE_URL,
        body: payload.merge(apiKey: @api_key).to_json,
        headers: HEADERS
      )
    end

    def extract_header(value, strategy: :to_i)
      {
        parsed: handle_strategy(value, strategy),
        raw:    value
      }
    end

    def handle_strategy(value, strategy)
      case strategy
      when :to_i
        value.to_i
      when :time_at
        Time.at(value.to_i).utc
      end
    end

    SEND_VOCAL_MESSAGE_URL = 'http://api.voicepartner.fr/v1/tts/send'.freeze

    HEADERS = {
      'Content-type' => 'application/json;charset=utf-8'
    }.freeze

    private_constant :SEND_VOCAL_MESSAGE_URL
    private_constant :HEADERS

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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