Class: Masheri::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/masheri/query.rb

Constant Summary collapse

OBJECT_TYPES =
['members', 'keys', 'services', 'roles', 'applications']
DEFAULT_QUERIES_PER_SECOND =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_type, options = {}) ⇒ Query

Returns a new instance of Query.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/masheri/query.rb', line 14

def initialize(object_type, options={})
  if !OBJECT_TYPES.include?(object_type)
    raise "Invalid object type. '#{object_type}' must be in #{OBJECT_TYPES.inspect}"
  end

  @object_type = object_type

  if options[:fields]
    @fields = options[:fields]
  else
    @fields = "*"
  end

  @where = options[:where]
  @page = options[:page]
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



11
12
13
# File 'lib/masheri/query.rb', line 11

def fields
  @fields
end

#object_typeObject (readonly)

Returns the value of attribute object_type.



11
12
13
# File 'lib/masheri/query.rb', line 11

def object_type
  @object_type
end

#pageObject

Returns the value of attribute page.



12
13
14
# File 'lib/masheri/query.rb', line 12

def page
  @page
end

Instance Method Details

#executeObject



43
44
45
# File 'lib/masheri/query.rb', line 43

def execute
  Masheri.client.call_remote('object.query', query_string)
end

#fetch_all(qps = DEFAULT_QUERIES_PER_SECOND) ⇒ Object

Page through the results. Due heavy use of the API, this method takes a qps parameter to control how often the API is called.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/masheri/query.rb', line 53

def fetch_all(qps = DEFAULT_QUERIES_PER_SECOND)
  response = execute
  items = response['items']

  while response['current_page'] < response['total_pages']
    self.page = response['current_page'] + 1
    response = execute
    items = items + response['items']

    sleep(1.0/DEFAULT_QUERIES_PER_SECOND)
  end

  return items
end

#itemsObject



47
48
49
# File 'lib/masheri/query.rb', line 47

def items
  execute['items']
end

#page_clauseObject



31
32
33
# File 'lib/masheri/query.rb', line 31

def page_clause
  "PAGE #{@page}" if @page
end

#query_stringObject



39
40
41
# File 'lib/masheri/query.rb', line 39

def query_string
  "SELECT #{fields} FROM #{object_type} #{where_clause} #{page_clause}"
end

#where_clauseObject



35
36
37
# File 'lib/masheri/query.rb', line 35

def where_clause
  "WHERE #{@where}" if @where
end