Class: LightSide::Pager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lightside/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#collectionObject

Returns the value of attribute collection.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def collection
  @collection
end

#next_urlObject

Returns the value of attribute next_url.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def next_url
  @next_url
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def params
  @params
end

#previous_urlObject

Returns the value of attribute previous_url.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def previous_url
  @previous_url
end

#resource_builderObject

Returns the value of attribute resource_builder.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def resource_builder
  @resource_builder
end

#resource_urlObject

Returns the value of attribute resource_url.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def resource_url
  @resource_url
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/lightside/pager.rb', line 5

def url
  @url
end

Instance Method Details

#countObject



15
16
17
# File 'lib/lightside/pager.rb', line 15

def count
  collection.length
end

#eachObject



19
20
21
# File 'lib/lightside/pager.rb', line 19

def each
  collection.each { |instance| yield instance }
end

#fetchObject



23
24
25
26
27
# File 'lib/lightside/pager.rb', line 23

def fetch
  self.collection = []
  fetch_page
  self
end

#fetch_pageObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/lightside/pager.rb', line 43

def less?
  !!previous_url
end

#more?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/lightside/pager.rb', line 47

def more?
  !!next_url
end

#nextObject



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_numberObject



57
58
59
# File 'lib/lightside/pager.rb', line 57

def page_number
  params.fetch(:page, 1).to_i
end

#previousObject



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_pathObject



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