Class: Masheri::Query
- Inherits:
-
Object
- Object
- Masheri::Query
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#object_type ⇒ Object
readonly
Returns the value of attribute object_type.
-
#page ⇒ Object
Returns the value of attribute page.
Instance Method Summary collapse
- #execute ⇒ Object
-
#fetch_all(qps = DEFAULT_QUERIES_PER_SECOND) ⇒ Object
Page through the results.
-
#initialize(object_type, options = {}) ⇒ Query
constructor
A new instance of Query.
- #items ⇒ Object
- #page_clause ⇒ Object
- #query_string ⇒ Object
- #where_clause ⇒ Object
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, ={}) if !OBJECT_TYPES.include?(object_type) raise "Invalid object type. '#{object_type}' must be in #{OBJECT_TYPES.inspect}" end @object_type = object_type if [:fields] @fields = [:fields] else @fields = "*" end @where = [:where] @page = [:page] end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
11 12 13 |
# File 'lib/masheri/query.rb', line 11 def fields @fields end |
#object_type ⇒ Object (readonly)
Returns the value of attribute object_type.
11 12 13 |
# File 'lib/masheri/query.rb', line 11 def object_type @object_type end |
#page ⇒ Object
Returns the value of attribute page.
12 13 14 |
# File 'lib/masheri/query.rb', line 12 def page @page end |
Instance Method Details
#execute ⇒ Object
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 |
#items ⇒ Object
47 48 49 |
# File 'lib/masheri/query.rb', line 47 def items execute['items'] end |
#page_clause ⇒ Object
31 32 33 |
# File 'lib/masheri/query.rb', line 31 def page_clause "PAGE #{@page}" if @page end |
#query_string ⇒ Object
39 40 41 |
# File 'lib/masheri/query.rb', line 39 def query_string "SELECT #{fields} FROM #{object_type} #{where_clause} #{page_clause}" end |
#where_clause ⇒ Object
35 36 37 |
# File 'lib/masheri/query.rb', line 35 def where_clause "WHERE #{@where}" if @where end |