Class: Ratlas::Query
- Inherits:
-
Object
- Object
- Ratlas::Query
- Defined in:
- lib/ratlas/query.rb
Instance Method Summary collapse
- #and(conditions) ⇒ Object
- #each(&block) ⇒ Object
- #execute ⇒ Object
-
#initialize(target, scope = :all, conditions = {}) ⇒ Query
constructor
A new instance of Query.
- #limit(limit) ⇒ Object
- #request ⇒ Object
- #to_a ⇒ Object
- #to_params ⇒ Object
- #to_uri ⇒ Object
- #where(conditions) ⇒ Object
Constructor Details
#initialize(target, scope = :all, conditions = {}) ⇒ Query
Returns a new instance of Query.
4 5 6 7 8 9 |
# File 'lib/ratlas/query.rb', line 4 def initialize(target, scope = :all, conditions = {}) @target = target @scope = scope @conditions = {:limit => "50"}.merge(conditions) @conditions.delete_if{|k,v| @target.respond_to?(:exclude) && @target.exclude.include?(k)} end |
Instance Method Details
#and(conditions) ⇒ Object
25 26 27 |
# File 'lib/ratlas/query.rb', line 25 def and(conditions) where(conditions) end |
#each(&block) ⇒ Object
17 18 19 |
# File 'lib/ratlas/query.rb', line 17 def each &block execute.each &block end |
#execute ⇒ Object
49 50 51 |
# File 'lib/ratlas/query.rb', line 49 def execute Ratlas::Response.new(request, @target.resource_key) end |
#limit(limit) ⇒ Object
29 30 31 |
# File 'lib/ratlas/query.rb', line 29 def limit(limit) add_condition(:limit => limit ) end |
#request ⇒ Object
45 46 47 |
# File 'lib/ratlas/query.rb', line 45 def request JSON.parse(Net::HTTP.get_response(URI.parse('http:'+to_uri.to_s)).body) end |
#to_a ⇒ Object
21 22 23 |
# File 'lib/ratlas/query.rb', line 21 def to_a execute.to_a end |
#to_params ⇒ Object
33 34 35 |
# File 'lib/ratlas/query.rb', line 33 def to_params to_uri.query end |
#to_uri ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/ratlas/query.rb', line 37 def to_uri uri = Addressable::URI.new uri.host = Ratlas::ENDPOINT uri.path = Ratlas::ENDPOINT_VERSION + '/' + @target.resource_name + '.json' uri.query_values = conditions_for_query uri end |
#where(conditions) ⇒ Object
11 12 13 14 15 |
# File 'lib/ratlas/query.rb', line 11 def where(conditions) raise "#where expects a conditions hash" unless conditions.is_a?(Hash) add_conditions(conditions) self end |