Class: RZoopla::BaseCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/zoopla/base_collection.rb,
lib/rzoopla/base_collection.rb

Direct Known Subclasses

Listings

Constant Summary collapse

ATTRIBUTES =
[:result_count, :page_number, :page_size, :results]
DEFAULT_PAGINATION_OPTIONS =
{
  page_number: 1,
  page_size: 10
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseCollection

Returns a new instance of BaseCollection.



13
14
15
16
17
# File 'lib/zoopla/base_collection.rb', line 13

def initialize(options = {})
  ATTRIBUTES.each do |attr|
    instance_variable_set("@#{attr}", options[attr])
  end
end

Class Method Details

.where(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/zoopla/base_collection.rb', line 31

def self.where(options = {})
  opt = DEFAULT_PAGINATION_OPTIONS.clone.merge(options)
  result = RZoopla::Utils.symbolize_keys(RZoopla::Client.get(end_point, opt))
  new(
    page_number: opt[:page_number],
    page_size: opt[:page_size],
    result_count: result[:result_count],
    results: extract_results(result)
  )
end

Instance Method Details

#[](index) ⇒ Object



27
28
29
# File 'lib/zoopla/base_collection.rb', line 27

def [](index)
  @results[index]
end

#each(&block) ⇒ Object



19
20
21
# File 'lib/zoopla/base_collection.rb', line 19

def each(&block)
  @results.each(&block)
end

#sizeObject



23
24
25
# File 'lib/zoopla/base_collection.rb', line 23

def size
  @results.size
end