Class: Honeybadger::Api::Paginator
- Inherits:
-
Object
- Object
- Honeybadger::Api::Paginator
- Defined in:
- lib/honeybadger-api/paginator.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#next_page_link ⇒ Object
readonly
Returns the value of attribute next_page_link.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#prev_page_link ⇒ Object
readonly
Returns the value of attribute prev_page_link.
Instance Method Summary collapse
- #collection ⇒ Object
-
#initialize(path, filters, handler) ⇒ Paginator
constructor
A new instance of Paginator.
- #next ⇒ Object
- #next? ⇒ Boolean
- #previous ⇒ Object
- #previous? ⇒ Boolean
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 24 |
# File 'lib/honeybadger-api/paginator.rb', line 7 def initialize(path, filters, handler) @path = path @filters = filters @handler = handler @pages = {} @current_page = 1 response = Honeybadger::Api.client.get(@path, @filters) @next_page_link = response[:links][:next] @prev_page_link = response[:links][:prev] @pages[current_page] = response[:results].map do |r| @handler.call(r) end end |
Instance Attribute Details
#current_page ⇒ Object (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 |
#next_page_link ⇒ Object (readonly)
Returns the value of attribute next_page_link.
5 6 7 |
# File 'lib/honeybadger-api/paginator.rb', line 5 def next_page_link @next_page_link end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
5 6 7 |
# File 'lib/honeybadger-api/paginator.rb', line 5 def pages @pages end |
#prev_page_link ⇒ Object (readonly)
Returns the value of attribute prev_page_link.
5 6 7 |
# File 'lib/honeybadger-api/paginator.rb', line 5 def prev_page_link @prev_page_link end |
Instance Method Details
#collection ⇒ Object
78 79 80 |
# File 'lib/honeybadger-api/paginator.rb', line 78 def collection @pages.values.flatten end |
#next ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/honeybadger-api/paginator.rb', line 34 def next if next? encoded_query = URI.parse(@next_page_link).query decoded_query = URI.decode_www_form(encoded_query) next_page_filters = decoded_query.inject({}){ |h,(k,v)| h[k.to_sym] = v; h } response = Honeybadger::Api.client.get(@path, @filters.merge(next_page_filters)) @current_page = current_page + 1 @next_page_link = response[:links][:next] @prev_page_link = response[:links][:prev] @pages[current_page] = response[:results].map do |r| @handler.call(r) end @pages[current_page] else nil end end |
#next? ⇒ Boolean
26 27 28 |
# File 'lib/honeybadger-api/paginator.rb', line 26 def next? !@next_page_link.nil? end |
#previous ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/honeybadger-api/paginator.rb', line 56 def previous if previous? encoded_query = URI.parse(@prev_page_link).query decoded_query = URI.decode_www_form(encoded_query) prev_page_filters = decoded_query.inject({}){ |h,(k,v)| h[k.to_sym] = v; h } response = Honeybadger::Api.client.get(@path, @filters.merge(prev_page_filters)) @current_page = current_page - 1 @next_page_link = response[:links][:next] @prev_page_link = response[:links][:prev] @pages[current_page] = response[:results].map do |r| @handler.call(r) end @pages[current_page] else nil end end |
#previous? ⇒ Boolean
30 31 32 |
# File 'lib/honeybadger-api/paginator.rb', line 30 def previous? !@prev_page_link.nil? end |