class DataVirtuality::Rest

DataVirtuality REST client

Attributes

host[R]
http_client[R]
password[R]
username[R]

Public Class Methods

new(host, username, password, receive_timeout = 0) click to toggle source
# File lib/datavirtuality/rest.rb, line 10
def initialize(host, username, password, receive_timeout = 0)
  @host = host
  @username = username
  @password = password

  @http_client = HTTPClient.new
  @http_client.set_auth(host, username, password)
  @http_client.receive_timeout = receive_timeout
end

Public Instance Methods

data_sources() click to toggle source
# File lib/datavirtuality/rest.rb, line 24
def data_sources
  json_response(http_get('source')).map { |h| h[:Name] }
end
get(table) click to toggle source
# File lib/datavirtuality/rest.rb, line 28
def get(table)
  json_response(http_get('source/' + table))
end
query(sql) click to toggle source
# File lib/datavirtuality/rest.rb, line 32
def query(sql)
  json_response(http_post('query?array=false', sql: sql))
end
status() click to toggle source
# File lib/datavirtuality/rest.rb, line 20
def status
  json_response(http_get('status'))&.[](:status)&.downcase&.to_sym
end

Private Instance Methods

endpoint(path) click to toggle source
# File lib/datavirtuality/rest.rb, line 56
def endpoint(path)
  host + '/rest/api/' + path
end
http_get(path) click to toggle source
# File lib/datavirtuality/rest.rb, line 40
def http_get(path)
  response = http_client.get endpoint(path)

  { status: response.status, body: response.body }
end
http_post(path, body) click to toggle source
# File lib/datavirtuality/rest.rb, line 46
def http_post(path, body)
  response = http_client.post(
    endpoint(path),
    body.to_json,
    'Content-Type' => 'application/json'
  )

  { status: response.status, body: response.body }
end
json_response(result) click to toggle source
# File lib/datavirtuality/rest.rb, line 60
def json_response(result)
  if result[:status] != 200
    error = begin
      JSON.parse result[:body]
    rescue => e
      raise RestException, e.message
    end

    raise RestException, error['description'], error['hint']
  end

  JSON.parse result[:body], symbolize_names: true
end