Sha256: b89a5ea5bbc2c2f9a8addce1bf237d3b6f3b3f5991be44ca9c447d620a177f72

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'json'

module VagrantCloud
  class Client
    # Base Vagrant Cloud API URL
    URL_BASE = 'https://vagrantcloud.com/api/v1'.freeze
    attr_accessor :access_token

    # @param [String] access_token - token used to authenticate API requests
    # @param [String] url_base - URL used to make API requests
    def initialize(access_token = nil, url_base = nil)
      if url_base
        @url_base = url_base
      else
        @url_base = URL_BASE
      end

      @access_token = access_token
    end

    # @param [String] method
    # @param [String] path
    # @param [Hash] params
    # @param [String] token
    # @return [Hash]
    def request(method, path, params = {}, token = nil)
      headers = {}

      if token
        headers['Authorization'] = "Bearer #{token}"
      elsif @access_token
        headers['Authorization'] = "Bearer #{@access_token}"
      end

      headers['Accept'] = 'application/json'

      request_params = {
        method: method,
        url: @url_base + path,
        headers: headers,
        ssl_version: 'TLSv1'
      }

      if ['get', 'head', 'delete'].include?(method.downcase)
        headers[:params] = params
      else
        request_params[:payload] = params
      end

      begin
        result = RestClient::Request.execute(request_params)

        parsed_result = JSON.parse(result)
        parsed_result
      rescue RestClient::ExceptionWithResponse => e
        raise ClientError.new(e.message, e.http_body, e.http_code)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant_cloud-2.0.0 lib/vagrant_cloud/client.rb