Class: Hungry::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HTTParty
Defined in:
lib/hungry/collection.rb,
lib/hungry/collection/pagination.rb

Direct Known Subclasses

Venue::Collection

Defined Under Namespace

Modules: Pagination

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#criteriaObject (readonly)

Returns the value of attribute criteria.



10
11
12
# File 'lib/hungry/collection.rb', line 10

def criteria
  @criteria
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/hungry/collection.rb', line 10

def endpoint
  @endpoint
end

#klassObject (readonly)

Returns the value of attribute klass.



10
11
12
# File 'lib/hungry/collection.rb', line 10

def klass
  @klass
end

Class Method Details

.get(*args) ⇒ Object

CLASS METHODS:



14
15
16
17
18
# File 'lib/hungry/collection.rb', line 14

def self.get(*args)
  self.base_uri Hungry.api_url
  
  super
end

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)
  options = Util.params_from_uri(uri) || klass.default_criteria
  
  self.class.new(klass, uri.path, options)
end

#resultsObject



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