class Vscale::Api::Client

Public Class Methods

new(token) click to toggle source
# File lib/vscale/api.rb, line 36
def initialize(token)
  @token = token

  uri = URI.parse(API_ENDPOINT)

  # TODO: rescue StandardError
  @http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
  @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  @http.ssl_version = :TLSv1_2
end

Private Instance Methods

encode_path_params(path, params) click to toggle source
# File lib/vscale/api.rb, line 78
def encode_path_params(path, params)
  [path, URI.encode_www_form(params)].join('?')
end
request(meth, path, params = {}) click to toggle source
# File lib/vscale/api.rb, line 62
def request(meth, path, params = {})
  full_path = encode_path_params([API_ENDPOINT, path].join, params)
  
  request = VERB_MAP[meth.to_sym].new(full_path)
  
  request['Accept'] = 'application/json, text/plain'
  request['Content-Type'] = 'application/json'
  request['X-Token'] = @token

  if ['post', 'put', 'patch'].include?(meth.to_s)
    request.body = params.to_json
    return @http.request(request)
  end
  @http.request(request)
end
request_json(meth, path, params = {}) click to toggle source
# File lib/vscale/api.rb, line 49
def request_json(meth, path, params = {})
  response = request(meth, path, params)

  result = Struct.new(:code, :body)

  if response.body.nil? || response.body.empty?
    result.new(nil, nil)
  else
    json_body = JSON.parse(response.body, quirks_mode: true) # TODO: TypeError if nil
    result.new(response.code, json_body)
  end
end