class Kriterion::API

Attributes

backend[RW]
metrics[RW]
queue_uri[RW]

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method
# File lib/kriterion/api.rb, line 19
def initialize(opts = {})
  # If there is already an instance, copy the objects from that
  if @@instance
    @queue_uri = @@instance.queue_uri
    @metrics   = @@instance.metrics
    @backend   = @@instance.backend
  else
    @queue_uri, @metrics, @backend = Kriterion::Connector.connections(opts)
    @@instance = self
  end
  super()
  logger.info "Initialised Kritioner API version #{Kriterion::VERSION}"
end

Private Instance Methods

find(thing, query = {}) click to toggle source

Finds things in the backend and returns them as JSON

# File lib/kriterion/api.rb, line 81
def find(thing, query = {})
  result = backend.send("find_#{thing}", query, recurse: options[:recurse])

  if result.is_a? Array
    result.map do |object|
      object.to_h(options[:mode])
    end.to_json
  else
    result.to_h(options[:mode]).to_json
  end
end
mode_options() click to toggle source
# File lib/kriterion/api.rb, line 109
def mode_options
  {
    'basic' => {
      recurse: false,
      mode: :basic
    },
    'full' => {
      recurse: true,
      mode: :full
    }
  }
end
options() click to toggle source

Returns options that are relevant to the queries we will be doing based on the params that were passed

# File lib/kriterion/api.rb, line 95
def options
  # Defualt level should be full
  level = params['level'] || 'full'

  # Convert all other params to symbols for later use
  sym_params = params.each_with_object({}) do |memo, (k, v)|
    memo[k.to_sym] = v
    memo
  end

  # Return all data
  sym_params.merge(mode_options[level])
end