Class: OpenCongressApi::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/opencongress_api/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

WillPaginate Helper Methods

These methods are implementded so that you may pass this collection to the will_paginate view helper to render pagination links.



28
29
30
# File 'lib/opencongress_api/collection.rb', line 28

def current_page
  @current_page
end

#page_sizeObject

Returns the value of attribute page_size.



3
4
5
# File 'lib/opencongress_api/collection.rb', line 3

def page_size
  @page_size
end

#total_resultsObject

Returns the value of attribute total_results.



3
4
5
# File 'lib/opencongress_api/collection.rb', line 3

def total_results
  @total_results
end

Class Method Details

.build(response) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/opencongress_api/collection.rb', line 6

def self.build(response)
  collection = Collection.new

  # Setup the attributes needed for WillPaginate style paging
  #request_url = response['meta']['url']
  #request_parameters = parse_parameters_from_url(request_url)
  #collection.page_size = request_parameters['page'] ? request_parameters['page'].to_i : nil
  #collection.total_results = response['meta']['total_count'].to_i
  #collection.current_page = request_parameters['offset'] ? (request_parameters['offset'].to_i + 1) : 1

  # Load the collection with all
  # of the results we passed in
  response.each do |result|
    collection << result
  end
  collection
end

Instance Method Details

#next_pageObject



40
41
42
# File 'lib/opencongress_api/collection.rb', line 40

def next_page
  self.current_page == self.total_pages ? nil : self.current_page.to_i+1
end

#previous_pageObject



36
37
38
# File 'lib/opencongress_api/collection.rb', line 36

def previous_page
  self.current_page == 1 ? nil : self.current_page.to_i-1
end

#total_pagesObject



32
33
34
# File 'lib/opencongress_api/collection.rb', line 32

def total_pages
  @page_size ? (@total_results.to_f / @page_size.to_f).ceil : 1
end