Class: Honeybadger::Api::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/honeybadger-api/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, filters, handler) ⇒ Paginator

Returns a new instance of Paginator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/honeybadger-api/paginator.rb', line 7

def initialize(path, filters, handler)
  @path = path
  @filters = filters
  @handler = handler

  @pages = {}

  @filters.merge!({ :page => 1 }) if !@filters.has_key?(:page)
  response = Honeybadger::Api.client.get(@path, @filters)

  @current_page = response[:current_page]
  @total_page_count = response[:num_pages]

  @pages[current_page] = response[:results].map do |r|
    @handler.call(r)
  end
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



5
6
7
# File 'lib/honeybadger-api/paginator.rb', line 5

def current_page
  @current_page
end

#pagesObject (readonly)

Returns the value of attribute pages.



5
6
7
# File 'lib/honeybadger-api/paginator.rb', line 5

def pages
  @pages
end

#total_page_countObject (readonly)

Returns the value of attribute total_page_count.



5
6
7
# File 'lib/honeybadger-api/paginator.rb', line 5

def total_page_count
  @total_page_count
end

Instance Method Details

#collectionObject



67
68
69
# File 'lib/honeybadger-api/paginator.rb', line 67

def collection
  @pages.values.flatten
end

#nextObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/honeybadger-api/paginator.rb', line 33

def next
  if next?
    response = Honeybadger::Api.client.get(@path, @filters.merge({:page => current_page + 1}))

    @current_page = response[:current_page]
    @total_page_count = response[:num_pages]

    @pages[current_page] = response[:results].map do |r|
      @handler.call(r)
    end

    @pages[current_page]
  else
    nil
  end
end

#next?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/honeybadger-api/paginator.rb', line 25

def next?
  current_page < total_page_count
end

#previousObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/honeybadger-api/paginator.rb', line 50

def previous
  if previous?
    response = Honeybadger::Api.client.get(@path, @filters.merge({:page => current_page - 1}))

    @current_page = response[:current_page]
    @total_page_count = response[:num_pages]

    @pages[current_page] = response[:results].map do |r|
      @handler.call(r)
    end

    @pages[current_page]
  else
    nil
  end
end

#previous?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/honeybadger-api/paginator.rb', line 29

def previous?
  current_page > 1
end