class RasperClient::Client

Public Class Methods

new(host:, port: nil, timeout: nil, path_prefix: nil, secure: true, empty_nil_values: false, username: nil, password: nil) click to toggle source
# File lib/rasper_client/client.rb, line 7
def initialize(host:, port: nil, timeout: nil, path_prefix: nil,
               secure: true, empty_nil_values: false,
               username: nil, password: nil)
  @host = host
  @port = port
  @timeout = timeout
  @empty_nil_values = empty_nil_values
  @path_prefix = path_prefix
  @secure = secure
  @username = username
  @password = password
end

Public Instance Methods

add(options) click to toggle source
# File lib/rasper_client/client.rb, line 20
def add(options)
  symbolize_keys(options)
  encode_options(options)
  response = execute_request(:add, options)
  JSON.parse(response.body) == { 'success' => true }
end
generate(options) click to toggle source
# File lib/rasper_client/client.rb, line 27
def generate(options)
  symbolize_keys(options)
  empty_nil_values(options) if @empty_nil_values
  options = encode_data(options)
  response = execute_request(:generate, options)
  result = JSON.parse(response.body)
  Base64.decode64(result['content'])
end

Private Instance Methods

build_request_params() click to toggle source
# File lib/rasper_client/client.rb, line 49
def build_request_params
  options = {}
  options[:read_timeout] = @timeout if @timeout
  options[:use_ssl] = true if @secure
  [@host, @port, options].compact
end
check_for_errors(response) click to toggle source
# File lib/rasper_client/client.rb, line 111
def check_for_errors(response)
  raise RasperClient::Error.invalid_credentials if response.code.to_i == 401
end
empty_nil_values(hash) click to toggle source
# File lib/rasper_client/client.rb, line 85
def empty_nil_values(hash)
  hash.each_key do |key|
    if hash[key].is_a?(Hash)
      empty_nil_values(hash[key])
    elsif hash[key].is_a?(Array)
      hash[key].each {|item| empty_nil_values(item) if item.is_a?(Hash) }
    else
      hash[key] = '' if hash[key].nil?
    end
  end
end
encode_data(options) click to toggle source
# File lib/rasper_client/client.rb, line 81
def encode_data(options)
  { data: Base64.encode64(options.to_json) }
end
encode_options(options) click to toggle source
# File lib/rasper_client/client.rb, line 72
def encode_options(options)
  options[:content] = Base64.encode64(options[:content]) if options[:content]
  if options[:images]
    options[:images].each do |image|
      image[:content] = Base64.encode64(image[:content])
    end
  end
end
execute_request(action, options) click to toggle source
# File lib/rasper_client/client.rb, line 38
def execute_request(action, options)
  response = Net::HTTP.start(*build_request_params) do |http|
    request = Net::HTTP::Post.new(uri_for(action))
    request.body = options.to_json
    request.basic_auth(@username, @password) if @username && @password
    http.request(request)
  end
  check_for_errors(response)
  response
end
symbolize_key(hash, key) click to toggle source
# File lib/rasper_client/client.rb, line 68
def symbolize_key(hash, key)
  hash[key.to_sym] = hash.delete(key.to_s) if hash.key?(key.to_s)
end
symbolize_keys(options) click to toggle source
# File lib/rasper_client/client.rb, line 56
def symbolize_keys(options)
  %w(name content images report data parameters).each do |s|
    symbolize_key(options, s)
  end
  if options[:images]
    options[:images].each do |image|
      symbolize_key(image, :name)
      symbolize_key(image, :content)
    end
  end
end
uri_build_class() click to toggle source
# File lib/rasper_client/client.rb, line 107
def uri_build_class
  @secure ? URI::HTTPS : URI::HTTP
end
uri_for(action) click to toggle source
# File lib/rasper_client/client.rb, line 97
def uri_for(action)
  uri_build_class
    .build(
      host: @host,
      port: @port,
      path: "#{@path_prefix}/#{action}"
    )
    .to_s
end