class Khepri

The Main Khepri connector class

Public Class Methods

new(khepri_url, api_key, instance_id) click to toggle source
# File lib/khepri_connector.rb, line 8
def initialize(khepri_url, api_key, instance_id)
  @khepri_url, @api_key,  @instance_id = khepri_url, api_key, instance_id
end

Public Instance Methods

ask(exludes = nil, forced = nil, dimensions = {}) click to toggle source

prepare and launch ask task on Khepri

@param excludes [Array] array of solutions you want to exludes @param forced [Array] array of solutions you want to force @param dimensions [Hash] hash of dimensions you want to use

@return [JSON] the Khepri answer

# File lib/khepri_connector.rb, line 21
def ask(exludes = nil, forced = nil, dimensions = {})
  params_to_add = {}
  if !exludes.nil?
    params_to_add[:exclude] = exludes.join(',')
  end

  if !forced.nil?
    params_to_add[:forced_solutions] = forced.join(',')
  end

  if !dimensions.empty?
    dimensions.each do |key, value|
      params_to_add[key] = value
    end
  end

  launch('ask', params_to_add)

end
dimensions(answer) click to toggle source

prepare and launch dimensions task on Khepri

@param answer [JSON] the JSON answer of your ask

@return [JSON] the Khepri answer

# File lib/khepri_connector.rb, line 64
def dimensions(answer)
  puts answer
  if answer["status"] == 'success' && answer.has_key?('dimensions')
    params_to_add = { :dimensions => answer["dimensions"]}
    launch('dimensions', params_to_add)

  end


end
launch(api, params_to_add = {}) click to toggle source

launch task on khepri and get the result

@param api [String] the task to call on khepri @param params_to_add [Hash] hash of parameters to ad on khepri's call

@return [JSON] the khepri answer to the request

# File lib/khepri_connector.rb, line 93
def launch(api, params_to_add = {})

  url = @khepri_url + '/api/' + api + '.json'
  uri = URI(url)
  params = { :instance => @instance_id, :api_key => @api_key}
  if !params_to_add.empty?
    params.merge!(params_to_add)
  end
  uri.query = URI.encode_www_form(params)
  puts "launching :: " + uri.to_s

  # real call
  res = Net::HTTP.get_response(uri)
  if res.is_a?(Net::HTTPSuccess) || !res.body.empty?
    puts "khepri answer :: " + res.body
    JSON.parse(res.body)
  else
    false
  end

end
reset() click to toggle source

prepare and launch success task on Khepri

@return [JSON] the Khepri answer

# File lib/khepri_connector.rb, line 80
def reset
  launch 'reset'

end
success(answer) click to toggle source

prepare and launch success task on Khepri

@param answer [JSON] the JSON answer of your ask

@return [JSON] the Khepri answer

# File lib/khepri_connector.rb, line 48
def success(answer)
  if answer["status"] == 'success'
    params_to_add = { :solution => answer["solution"]}
    launch('success', params_to_add)

  end

end