class Quber::HTTP

Constants

Response
VERBS

Public Class Methods

new(options = {}) click to toggle source
# File lib/quber/http.rb, line 17
def initialize options = {}
  @defaults = {
    connection: Net::HTTP::Persistent.new,
    api_uri:    ENV['QUBER_API_URI']   || 'http://localhost:5672/api/v1/',
    api_token:  ENV['QUBER_API_TOKEN'] || '1234567890'
  }.merge!(options)
end

Public Instance Methods

base_headers() click to toggle source
# File lib/quber/http.rb, line 25
def base_headers
  {
    'X-Auth-Token' => @defaults[:api_token],
    'User-Agent'   => "Quber/#{Quber::VERSION}",
    'Content-Type' => 'application/json'
  }
end
delete(path, options = {}) click to toggle source
# File lib/quber/http.rb, line 45
def delete(path, options = {})
  execute(path, :delete, options)
end
get(path, options = {}) click to toggle source
# File lib/quber/http.rb, line 33
def get(path, options = {})
  execute(path, :get, options)
end
post(path, options = {}) click to toggle source
# File lib/quber/http.rb, line 37
def post(path, options = {})
  execute(path, :post, options)
end
put(path, options = {}) click to toggle source
# File lib/quber/http.rb, line 41
def put(path, options = {})
  execute(path, :put, options)
end

Private Instance Methods

execute(path, method, options = {}) click to toggle source
# File lib/quber/http.rb, line 50
def execute(path, method, options = {})
  uri = URI.join(@defaults[:api_uri], path)
  req = VERBS[method].new(uri.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 = @defaults[:connection].request(uri.to_s, req)
  body = resp.body.empty? ? {} : JSON.parse(resp.body)
  Response.new(resp.code.to_i, body)
end