class Atlas::Client

Client for interacting with the Atlas API.

Constants

DEFAULT_HEADERS

Attributes

token[R]
url[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/atlas/client.rb, line 11
def initialize(opts = {})
  @url = opts[:url] || 'https://app.vagrantup.com/'
  @token = opts[:token]
end

Private Instance Methods

parse_opts(opts) click to toggle source
# File lib/atlas/client.rb, line 53
def parse_opts(opts)
  body = opts.fetch(:body, nil)
  body = JSON.dump(body) if body && body.is_a?(Hash)

  query = opts.fetch(:query, {})
  headers = opts.fetch(:headers, {})

  [body, query, headers]
end
request(method, path, opts = {}) click to toggle source
# File lib/atlas/client.rb, line 26
def request(method, path, opts = {}) # rubocop:disable AbcSize, MethodLength
  body, query, headers = parse_opts(opts)

  # set the default headers
  headers.merge!(DEFAULT_HEADERS)

  # set the access token
  headers["Authorization"] = "Bearer #{token}"

  connection = Excon.new(@url)
  response = connection.request(expects: [200, 201], method: method,
                                path: "/api/v1#{path}", body: body,
                                query: query, headers: headers)

  JSON.parse(response.body)
rescue Excon::Errors::BadRequest => e
  raise ClientError, e.response.body
rescue Excon::Errors::Unauthorized => e
  raise UnauthorizedError, e.response.body
rescue Excon::Errors::NotFound => e
  raise NotFoundError, e.response.body
rescue Excon::Errors::InternalServerError => e
  raise ServerError, e.response.body
rescue Excon::Errors::Timeout => e
  raise TimeoutError, e.message
end