class Qtc::Client

Attributes

default_headers[RW]
http_client[R]

Public Class Methods

new(api_url, default_headers = {}) click to toggle source

Initialize api client

@param [String] api_url @param [Hash] default_headers

# File lib/qtc/client.rb, line 15
def initialize(api_url, default_headers = {})
  @http_client = HTTPClient.new
  @default_headers = {'Accept' => 'application/json', 'Content-Type' => 'application/json'}.merge(default_headers)
  @api_url = api_url
end

Public Instance Methods

delete(path, body = nil, params = {}, headers = {}) click to toggle source

Delete request

@param [String] path @param [Hash,String] body @param [Hash] params @param [Hash] headers @return [Hash]

# File lib/qtc/client.rb, line 87
def delete(path, body = nil, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
      header: request_headers,
      body: encode_body(body, request_headers['Content-Type']),
      query: params
  }
  response = http_client.delete(request_uri(path), request_options)
  if response.status == 200
    parse_response(response)
  else
    handle_error_response(response)
  end
end
get(path, params = nil, headers = {}) click to toggle source

Get request

@param [String] path @param [Hash,NilClass] params @return [Hash]

# File lib/qtc/client.rb, line 26
def get(path, params = nil, headers = {})
  response = http_client.get(request_uri(path), params, request_headers(headers))
  if response.status == 200
    parse_response(response)
  else
    handle_error_response(response)
  end
end
post(path, obj, params = {}, headers = {}) click to toggle source

Post request

@param [String] path @param [Object] obj @param [Hash] params @param [Hash] headers @return [Hash]

# File lib/qtc/client.rb, line 42
def post(path, obj, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
      header: request_headers,
      body: encode_body(obj, request_headers['Content-Type']),
      query: params
  }
  response = http_client.post(request_uri(path), request_options)
  if [200, 201].include?(response.status)
    parse_response(response)
  else
    handle_error_response(response)
  end
end
put(path, obj, params = {}, headers = {}) click to toggle source

Put request

@param [String] path @param [Object] obj @param [Hash] params @param [Hash] headers @return [Hash]

# File lib/qtc/client.rb, line 64
def put(path, obj, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
      header: request_headers,
      body: encode_body(obj, request_headers['Content-Type']),
      query: params
  }

  response = http_client.put(request_uri(path), request_options)
  if [200, 201].include?(response.status)
    parse_response(response)
  else
    handle_error_response(response)
  end
end

Private Instance Methods

dump_json(obj) click to toggle source

Dump json

@param [Object] obj @return [String]

# File lib/qtc/client.rb, line 162
def dump_json(obj)
  JSON.dump(obj)
end
encode_body(body, content_type) click to toggle source

Encode body based on content type

@param [Object] body @param [String] content_type

# File lib/qtc/client.rb, line 127
def encode_body(body, content_type)
  if content_type == 'application/json'
    dump_json(body)
  else
    body
  end
end
handle_error_response(response) click to toggle source
# File lib/qtc/client.rb, line 166
def handle_error_response(response)
  raise Qtc::Errors::StandardError.new(response.status, response.body)
end
parse_json(json) click to toggle source

Parse json

@param [String] json @return [Hash,Object,NilClass]

# File lib/qtc/client.rb, line 153
def parse_json(json)
  JSON.parse(json) rescue nil
end
parse_response(response) click to toggle source

Parse response

@param [HTTP::Message] @return [Object]

# File lib/qtc/client.rb, line 140
def parse_response(response)
  if response.headers['Content-Type'].include?('application/json')
    parse_json(response.body)
  else
    response.body
  end
end
request_headers(headers = {}) click to toggle source

Get request headers

@param [Hash] headers @return [Hash]

# File lib/qtc/client.rb, line 118
def request_headers(headers = {})
  @default_headers.merge(headers)
end
request_uri(path) click to toggle source

Get full request uri

@param [String] path @return [String]

# File lib/qtc/client.rb, line 109
def request_uri(path)
  "#{@api_url}#{path}"
end