class Teapi::Sender

Constants

BASE_URL
HEADERS

Public Class Methods

new(configuration) click to toggle source
# File lib/teapi/sender.rb, line 14
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

request(method, resource, args = {}, date = nil) click to toggle source
# File lib/teapi/sender.rb, line 18
def request(method, resource, args = {}, date = nil)
  url = BASE_URL + resource.to_s
  d = date || Time.now.utc.httpdate
  args[:headers] = (args[:headers] || {}).merge({
    'Date' => d,
    'Authorization' => sign(url, d, args),
  })
  scheme = @configuration.secure ? "https" : "http"
  res = HTTParty.send(method, "#{scheme}://#{@configuration.host}#{url}", args)
  if res.code == 401 && res.parsed_response.include?('date') && date.nil?
    return request(method, resource, args, res.parsed_response['date'])
  end
  res
end
sign(url, date, args) click to toggle source
# File lib/teapi/sender.rb, line 33
def sign(url, date, args)
  data = url + date
  data += args[:body] if args.include?(:body)
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @configuration.sync_secret, data)
  "HMAC-SHA256 Credential=#{@configuration.sync_key},Signature=#{signature}"
end