class Voicepartner::Client

Constants

HEADERS
SEND_VOCAL_MESSAGE_URL

Public Class Methods

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

Public Instance Methods

send_vocal_message(to:, message_text:) click to toggle source
# File lib/voicepartner/client.rb, line 11
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 Instance Methods

extract_header(value, strategy: :to_i) click to toggle source
# File lib/voicepartner/client.rb, line 38
def extract_header(value, strategy: :to_i)
  {
    parsed: handle_strategy(value, strategy),
    raw:    value
  }
end
handle_strategy(value, strategy) click to toggle source
# File lib/voicepartner/client.rb, line 45
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_request(payload) click to toggle source
# File lib/voicepartner/client.rb, line 30
def send_request(payload)
  HTTParty.post(
    SEND_VOCAL_MESSAGE_URL,
    body: payload.merge(apiKey: @api_key).to_json,
    headers: HEADERS
  )
end