Class: Hungry::Collection
- Inherits:
-
Object
- Object
- Hungry::Collection
- Includes:
- Enumerable, HTTParty
- Defined in:
- lib/hungry/collection.rb,
lib/hungry/collection/pagination.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Pagination
Instance Attribute Summary collapse
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
-
.get(*args) ⇒ Object
CLASS METHODS:.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #all(new_criteria = {}) ⇒ Object
- #count(*args) ⇒ Object
- #each(&block) ⇒ Object
- #first(n = 1) ⇒ Object
- #from_url(url) ⇒ Object
-
#initialize(klass, endpoint, criteria = {}) ⇒ Collection
constructor
INSTANCE METHODS:.
- #results ⇒ Object
- #results=(results) ⇒ Object
Constructor Details
#initialize(klass, endpoint, criteria = {}) ⇒ Collection
INSTANCE METHODS:
22 23 24 25 26 |
# File 'lib/hungry/collection.rb', line 22 def initialize(klass, endpoint, criteria = {}) @klass = klass @endpoint = endpoint @criteria = criteria.symbolize_keys end |
Instance Attribute Details
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
10 11 12 |
# File 'lib/hungry/collection.rb', line 10 def criteria @criteria end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
10 11 12 |
# File 'lib/hungry/collection.rb', line 10 def endpoint @endpoint end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
10 11 12 |
# File 'lib/hungry/collection.rb', line 10 def klass @klass end |
Class Method Details
Instance Method Details
#[](index) ⇒ Object
39 40 41 |
# File 'lib/hungry/collection.rb', line 39 def [](index) build_resource results[index] end |
#all(new_criteria = {}) ⇒ Object
35 36 37 |
# File 'lib/hungry/collection.rb', line 35 def all(new_criteria = {}) self.class.new(klass, endpoint, criteria.merge(new_criteria)) end |
#count(*args) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/hungry/collection.rb', line 53 def count(*args) if args.present? super else results.count end end |
#each(&block) ⇒ Object
61 62 63 64 65 |
# File 'lib/hungry/collection.rb', line 61 def each(&block) results.each do |result| yield build_resource(result) end end |
#first(n = 1) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/hungry/collection.rb', line 43 def first(n = 1) if n == 1 && (value = results.first) build_resource value elsif n > 1 results.first(n).map do |result| build_resource result end end end |
#from_url(url) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/hungry/collection.rb', line 28 def from_url(url) uri = URI.parse(url) = Util.params_from_uri(uri) || klass.default_criteria self.class.new(klass, uri.path, ) end |
#results ⇒ Object
67 68 69 |
# File 'lib/hungry/collection.rb', line 67 def results @results ||= json['results'] end |
#results=(results) ⇒ Object
71 72 73 |
# File 'lib/hungry/collection.rb', line 71 def results=(results) @results = results end |