Class: RedboothRuby::Request::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



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

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.



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

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Instance Method Details

#allArray(resource)

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

Returns:



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

def all
  results = []
  response.data.each do |obj|
    results << resource.new(obj)
  end
  results
end

#countInteger

Return total elements

Returns:

  • (Integer)


52
53
54
55
56
# File 'lib/redbooth-ruby/request/collection.rb', line 52

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)


44
45
46
47
# File 'lib/redbooth-ruby/request/collection.rb', line 44

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)


61
62
63
64
65
# File 'lib/redbooth-ruby/request/collection.rb', line 61

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)


36
37
38
39
# File 'lib/redbooth-ruby/request/collection.rb', line 36

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)


70
71
72
73
74
# File 'lib/redbooth-ruby/request/collection.rb', line 70

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)


28
29
30
31
# File 'lib/redbooth-ruby/request/collection.rb', line 28

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