class Kybus::Client::RESTClient

HTTP client using HTTParty as support. This implementation makes it easier to integrate with other backends.

Public Class Methods

new(configs) click to toggle source
# File lib/kybus/client/rest_client.rb, line 18
def initialize(configs)
  @session = Session.build(configs)
  @config = configs
  @format = Format.build(configs)
  @endpoint = configs[:endpoint]
  @validator = Validator.build(configs)
end

Public Instance Methods

delete(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 38
def delete(path, data = {})
  perform_request(:delete, path, data)
end
get(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 26
def get(path, data = {})
  perform_request(:get, path, data)
end
patch(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 42
def patch(path, data = {})
  perform_request(:patch, path, data)
end
post(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 30
def post(path, data = {})
  perform_request(:post, path, data)
end
put(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 34
def put(path, data = {})
  perform_request(:put, path, data)
end
raw_get(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 46
def raw_get(path, data = {})
  perform_raw_request(:get, path, data)
end
raw_post(path, data = {}) click to toggle source
# File lib/kybus/client/rest_client.rb, line 50
def raw_post(path, data = {})
  perform_raw_request(:post, path, data)
end

Private Instance Methods

perform_raw_request(method, path, data) click to toggle source
# File lib/kybus/client/rest_client.rb, line 56
def perform_raw_request(method, path, data)
  log_debug('Performing request', method: method, path: path, data: data)
  init_time = Time.now
  uri = (path.start_with?('http') ? path : "#{@endpoint}#{path}")
  result = @session.perform_request(method, uri,
                                    @format.pack(data))
  log_info('Request perfomed',
           path: path,
           server: @endpoint,
           verb: method,
           processing_time: (Time.now - init_time).to_f * 1000)
  result
end
perform_request(method, path, data) click to toggle source
# File lib/kybus/client/rest_client.rb, line 70
def perform_request(method, path, data)
  result = perform_raw_request(method, path, data).body
  unpacked = @format.unpack(result)
  @validator.validate(unpacked)
end