Class: TheCity::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/the_city/collection.rb

Overview

Utility class for collections with paged responses

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, key, klass, client, request_method, path, options) ⇒ TheCity::Collection

Initializes a new Collection

Parameters:

  • attrs (Hash)
  • key (String, Symbol)

    The key to fetch the data from the response

  • klass (Class)

    The class to instantiate objects in the response

  • client (TheCity::API::Client)
  • request_method (String, Symbol)
  • path (String)
  • options (Hash)


34
35
36
37
38
39
40
41
42
43
# File 'lib/the_city/collection.rb', line 34

def initialize(attrs, key, klass, client, request_method, path, options)
  @key = key.to_sym
  @klass = klass
  @client = client
  @request_method = request_method.to_sym
  @path = path
  @options = options
  @collection = []
  set_attrs(attrs)
end

Instance Attribute Details

#attrsObject (readonly) Also known as: to_h, to_hash, to_hsh

Returns the value of attribute attrs.



5
6
7
# File 'lib/the_city/collection.rb', line 5

def attrs
  @attrs
end

Class Method Details

.from_response(response, key, klass, client, request_method, path, options) ⇒ TheCity::Collection

Construct a new Collection object from a response hash

Parameters:

  • response (Hash)
  • key (String, Symbol)

    The key to fetch the data from the response

  • klass (Class)

    The class to instantiate objects in the response

  • client (TheCity::API::Client)
  • request_method (String, Symbol)
  • path (String)
  • options (Hash)

Returns:



20
21
22
# File 'lib/the_city/collection.rb', line 20

def self.from_response(response, key, klass, client, request_method, path, options)
  new(response[:body], key, klass, client, request_method, path, options)
end

Instance Method Details

#[](n) ⇒ Object



102
103
104
# File 'lib/the_city/collection.rb', line 102

def [](n)
  @collection[n]
end

#current_cursorObject



61
62
63
# File 'lib/the_city/collection.rb', line 61

def current_cursor
  @current_cursor
end

#current_pageObject



85
86
87
# File 'lib/the_city/collection.rb', line 85

def current_page
  @current_page
end

#each(start = 0, &block) ⇒ Enumerator

Returns:

  • (Enumerator)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/the_city/collection.rb', line 46

def each(start = 0, &block)
  return to_enum(:each) unless block_given?
  for element in Array(@collection[start..-1])
    yield element
    @current_cursor += 1
  end
  unless last?
    start = [@collection.size, start].max
    fetch_next_page unless last_page?
    each(start, &block)
  end
  @current_cursor = 1
  self
end

#first?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/the_city/collection.rb', line 76

def first?
  previous_cursor.zero?
end

#lastObject



106
107
108
# File 'lib/the_city/collection.rb', line 106

def last
  @collection.last
end

#last?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/the_city/collection.rb', line 81

def last?
  current_cursor >= total_entries
end

#last_page?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/the_city/collection.rb', line 93

def last_page?
  current_page == @total_pages
end

#next_cursurObject Also known as: next



65
66
67
# File 'lib/the_city/collection.rb', line 65

def next_cursur
  (current_cursor + 1) || -1
end

#next_pageObject



89
90
91
# File 'lib/the_city/collection.rb', line 89

def next_page
  current_page + 1
end

#previous_cursorObject Also known as: previous



70
71
72
# File 'lib/the_city/collection.rb', line 70

def previous_cursor
  current_cursor - 1
end

#total_entriesObject Also known as: total



97
98
99
# File 'lib/the_city/collection.rb', line 97

def total_entries
  @total_entries.is_a?(Array) ? @total_entries.first : @total_entries
end