class Qube::HTTP

Constants

Response
VERBS

Public Class Methods

new() click to toggle source
# File lib/qube/transport/http.rb, line 18
def initialize
  @config     = Qube.config
  @connection = Net::HTTP::Persistent.new

  @api_uri    = @config.api_uri
  @api_token  = @config.api_token
  @user_agent = @config.user_agent
  @logger     = @config.logger
end

Public Instance Methods

base_headers() click to toggle source
# File lib/qube/transport/http.rb, line 28
def base_headers
  {
    'X-Auth-Token' => @api_token,
    'User-Agent'   => @user_agent,
    'Content-Type' => 'application/json'
  }
end
delete(path, options = {}) click to toggle source
# File lib/qube/transport/http.rb, line 48
def delete(path, options = {})
  execute(path, :delete, options)
end
get(path, options = {}) click to toggle source
# File lib/qube/transport/http.rb, line 36
def get(path, options = {})
  execute(path, :get, options)
end
post(path, options = {}) click to toggle source
# File lib/qube/transport/http.rb, line 40
def post(path, options = {})
  execute(path, :post, options)
end
put(path, options = {}) click to toggle source
# File lib/qube/transport/http.rb, line 44
def put(path, options = {})
  execute(path, :put, options)
end

Private Instance Methods

execute(path, method, options = {}) click to toggle source
# File lib/qube/transport/http.rb, line 53
def execute(path, method, options = {})
  url = URI.join(@api_uri, path)
  req = VERBS[method].new(url.request_uri)

  # Build headers and body
  options.transform_keys!(&:to_s) unless options.empty?
  headers = base_headers.merge(options.dig('headers') || options)
  headers.each{ |k,v| req[k] = v }
  req.body = (options.dig('body') || options || {}).to_json

  # Send request and process response
  resp = @connection.request(url.to_s, req)
  body = resp.body.empty? ? {} : JSON.parse(resp.body)
  Response.new(resp.code.to_i, body)

  rescue => e
    @logger.error(e.message) if @logger
    raise e
end