class Kongfigure::HTTPClient

Constants

HTTP_HEADERS

Public Class Methods

new(parser, url) click to toggle source
# File lib/kongfigure/http_client.rb, line 12
def initialize(parser, url)
  @configuration = {
    ssl_ca_path: nil,
    verify_ssl:  OpenSSL::SSL::VERIFY_NONE,
    url:         url || parser.parse!.url
  }
  @inflector = Dry::Inflector.new
end

Public Instance Methods

delete(path) click to toggle source
# File lib/kongfigure/http_client.rb, line 38
 def delete(path)
   execute(request_options(:delete, path, nil))
end
get(path, size=nil) click to toggle source
# File lib/kongfigure/http_client.rb, line 33
def get(path, size=nil)
  size = size = 1000
  execute(request_options(:get, path, size))
end
patch(path, payload) click to toggle source
# File lib/kongfigure/http_client.rb, line 29
def patch(path, payload)
  execute(request_options(:patch, path, nil, payload))
end
post(path, payload) click to toggle source
# File lib/kongfigure/http_client.rb, line 21
def post(path, payload)
  execute(request_options(:post, path, nil, payload))
end
put(path, payload) click to toggle source
# File lib/kongfigure/http_client.rb, line 25
def put(path, payload)
  execute(request_options(:put, path, nil, payload))
end

Private Instance Methods

execute(options) click to toggle source
# File lib/kongfigure/http_client.rb, line 44
def execute(options)
  RestClient::Request.execute(options) do |response, request, _result|
    handle_response(response)
  end
end
handle_response(response) click to toggle source
# File lib/kongfigure/http_client.rb, line 76
def handle_response(response)
  if response.code == 204
    nil
  elsif response.code.between?(200, 399)
    handle_success(response)
  elsif response.code == 400
    parsed_response = JSON.parse(response.body)
    raise Kongfigure::Errors::BadRequest, {
      name:   parsed_response["name"],
      fields: parsed_response["fields"]
    }
  elsif response.code == 404
    raise Kongfigure::Errors::ResourceNotFound
  elsif response.code == 409
    raise Kongfigure::Errors::ResourceConflict
  elsif response.code == 500
    raise Kongfigure::Errors::InternalServerError
  end
end
handle_success(response) click to toggle source
# File lib/kongfigure/http_client.rb, line 96
def handle_success(response)
  JSON.parse(response.body)
end
request_options(method, path, size, payload = nil) click to toggle source
# File lib/kongfigure/http_client.rb, line 57
def request_options(method, path, size, payload = nil)
  uri       = URI.join(@configuration[:url], path)
  query     = [uri.query]
  query     = query + ["size=#{size}"] if size
  query     = query.compact
  uri.query = query.join("&") if query.size > 0
  opts      = {
    method: method,
    url: uri.to_s,
    headers: HTTP_HEADERS
  }

  opts.merge!(ssl_options) if @configuration[:url].include?("https://")
  opts.merge!(payload: payload) if payload

  opts
end
ssl_options() click to toggle source
# File lib/kongfigure/http_client.rb, line 50
def ssl_options
  {
   ssl_ca_file: @configuration[:ssl_ca_path],
   verify_ssl:  @configuration[:verify_ssl]
 }
end