Class: RedboothRuby::Request::Collection

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/redbooth-ruby/request/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#camelize, #underscore

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
13
# File 'lib/redbooth-ruby/request/collection.rb', line 7

def initialize(attributes={})
  @response = attributes[:response]
  @params = attributes[:params]
  @method = attributes[:method]
  @resource = attributes[:resource]
  @session = attributes[:session]
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/redbooth-ruby/request/collection.rb', line 5

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/redbooth-ruby/request/collection.rb', line 5

def params
  @params
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/redbooth-ruby/request/collection.rb', line 5

def resource
  @resource
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/redbooth-ruby/request/collection.rb', line 5

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



5
6
7
# File 'lib/redbooth-ruby/request/collection.rb', line 5

def session
  @session
end

Instance Method Details

#allArray(resource)

Returns an array of resuce objects built from each one of the response items

Returns:



18
19
20
# File 'lib/redbooth-ruby/request/collection.rb', line 18

def all
  results_from(response)
end

#countInteger

Return total elements

Returns:

  • (Integer)


49
50
51
52
53
# File 'lib/redbooth-ruby/request/collection.rb', line 49

def count
  return all.size unless paginated?
  return all.size if total_pages.to_i <= 1
  total_pages * per_page
end

#current_pageInteger

Returns elements per page

Returns:

  • (Integer)


41
42
43
44
# File 'lib/redbooth-ruby/request/collection.rb', line 41

def current_page
  return unless response.headers['PaginationCurrentPage']
  response.headers['PaginationCurrentPage'].to_i
end

#next_pageRedbooth::Request::Collection

Performs the request for the next page if exists

Returns:

  • (Redbooth::Request::Collection)


58
59
60
61
62
# File 'lib/redbooth-ruby/request/collection.rb', line 58

def next_page
  return nil unless paginated?
  return nil unless (total_pages - current_page) > 0
  request_with(page: current_page + 1)
end

#per_pageInteger

Returns elements per page

Returns:

  • (Integer)


33
34
35
36
# File 'lib/redbooth-ruby/request/collection.rb', line 33

def per_page
  return unless response.headers['PaginationPerPage']
  response.headers['PaginationPerPage'].to_i
end

#prev_pageRedbooth::Request::Collection

Performs the request for the next page if exists

Returns:

  • (Redbooth::Request::Collection)


67
68
69
70
71
# File 'lib/redbooth-ruby/request/collection.rb', line 67

def prev_page
  return nil unless paginated?
  return nil unless current_page > 1
  request_with(page: current_page - 1)
end

#total_pagesInteger

Returns total pages

Returns:

  • (Integer)


25
26
27
28
# File 'lib/redbooth-ruby/request/collection.rb', line 25

def total_pages
  return unless response.headers['PaginationTotalPages']
  response.headers['PaginationTotalPages'].to_i
end