class LibTAD::Client

Main endpoint for accessing the Time and Date APIs.

Constants

ENDPOINT

The endpoint the client connects to.

USER_AGENT

Client user agent.

VERSION

API version.

Public Class Methods

new(access_key:, secret_key:) click to toggle source
# File lib/libtad.rb, line 71
def initialize(access_key:, secret_key:)
  @access_key = access_key
  @secret_key = secret_key
end

Private Instance Methods

authenticate(service, args) click to toggle source
# File lib/libtad.rb, line 78
def authenticate(service, args)
  timestamp = ::Time.now.utc.strftime('%FT%T')
  message = @access_key + service + timestamp

  hmac = OpenSSL::HMAC.digest('sha1', @secret_key, message)
  signature = Base64.encode64(hmac).chomp

  args[:accesskey] = @access_key
  args[:signature] = signature
  args[:timestamp] = timestamp

  args
end
get(service, args) click to toggle source
# File lib/libtad.rb, line 92
def get(service, args)
  args = args.transform_values! do |e|
    if [true, false].include?(e)
      e && 1 || 0
    else
      e
    end
  end

  args[:version] = VERSION
  args = authenticate(service, args)

  uri = URI(ENDPOINT + service)
  uri.query = URI.encode_www_form(args)

  request = Net::HTTP::Get.new(uri)
  request["User-Agent"] = USER_AGENT

  res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) {|http|
    http.request(request)
  }

  res = JSON.parse(res.body)

  if (error = res.fetch('errors', nil))
    raise Exception.new error
  else
    res
  end
end