Class: LightSide::Pager
- Inherits:
-
Object
- Object
- LightSide::Pager
- Includes:
- Enumerable
- Defined in:
- lib/lightside/pager.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#next_url ⇒ Object
Returns the value of attribute next_url.
-
#params ⇒ Object
Returns the value of attribute params.
-
#previous_url ⇒ Object
Returns the value of attribute previous_url.
-
#resource_builder ⇒ Object
Returns the value of attribute resource_builder.
-
#resource_url ⇒ Object
Returns the value of attribute resource_url.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #count ⇒ Object
- #each ⇒ Object
- #fetch ⇒ Object
- #fetch_page ⇒ Object
-
#initialize(model_class, params = {}) ⇒ Pager
constructor
A new instance of Pager.
- #less? ⇒ Boolean
- #more? ⇒ Boolean
- #next ⇒ Object
- #page_number ⇒ Object
- #previous ⇒ Object
- #resource_path ⇒ Object
Constructor Details
#initialize(model_class, params = {}) ⇒ Pager
Returns a new instance of Pager.
7 8 9 10 11 12 13 |
# File 'lib/lightside/pager.rb', line 7 def initialize(model_class, params={}) self.resource_url = model_class.resource_url self.resource_builder = lambda { |hash| model_class.new(hash) } self.params = { page: 1 }.merge(params) self.collection = [] self.url = resource_path end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def collection @collection end |
#next_url ⇒ Object
Returns the value of attribute next_url.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def next_url @next_url end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def params @params end |
#previous_url ⇒ Object
Returns the value of attribute previous_url.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def previous_url @previous_url end |
#resource_builder ⇒ Object
Returns the value of attribute resource_builder.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def resource_builder @resource_builder end |
#resource_url ⇒ Object
Returns the value of attribute resource_url.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def resource_url @resource_url end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/lightside/pager.rb', line 5 def url @url end |
Instance Method Details
#count ⇒ Object
15 16 17 |
# File 'lib/lightside/pager.rb', line 15 def count collection.length end |
#each ⇒ Object
19 20 21 |
# File 'lib/lightside/pager.rb', line 19 def each collection.each { |instance| yield instance } end |
#fetch ⇒ Object
23 24 25 26 27 |
# File 'lib/lightside/pager.rb', line 23 def fetch self.collection = [] fetch_page self end |
#fetch_page ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lightside/pager.rb', line 29 def fetch_page RestClient.get(url, Config.headers) do |response, request, result| case response.code when 200 parsed_response = JSON.parse(response) self.next_url = parsed_response["next"] self.previous_url = parsed_response["previous"] parsed_response["results"].each do |resource| collection << resource_builder.call(resource) end end end end |
#less? ⇒ Boolean
43 44 45 |
# File 'lib/lightside/pager.rb', line 43 def less? !!previous_url end |
#more? ⇒ Boolean
47 48 49 |
# File 'lib/lightside/pager.rb', line 47 def more? !!next_url end |
#next ⇒ Object
51 52 53 54 55 |
# File 'lib/lightside/pager.rb', line 51 def next raise "no more results" unless more? self.url = next_url fetch end |
#page_number ⇒ Object
57 58 59 |
# File 'lib/lightside/pager.rb', line 57 def page_number params.fetch(:page, 1).to_i end |
#previous ⇒ Object
61 62 63 64 65 |
# File 'lib/lightside/pager.rb', line 61 def previous raise "no more results" unless less? self.url = previous_url fetch end |
#resource_path ⇒ Object
67 68 69 |
# File 'lib/lightside/pager.rb', line 67 def resource_path URI.join(resource_url, "?#{URI.encode_www_form(params)}").to_s end |