Class: Hyperdrive::Middleware::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperdrive/middleware/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Pagination

Returns a new instance of Pagination.



7
8
9
10
# File 'lib/hyperdrive/middleware/pagination.rb', line 7

def initialize(app)
  @app = app
  @default_per_page = hyperdrive.config.fetch(:per_page) { 20 }
end

Instance Attribute Details

#default_per_pageObject (readonly)

Returns the value of attribute default_per_page.



6
7
8
# File 'lib/hyperdrive/middleware/pagination.rb', line 6

def default_per_page
  @default_per_page
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/hyperdrive/middleware/pagination.rb', line 12

def call(env)
  if %(GET HEAD).include? env['REQUEST_METHOD']
    params = Rack::Request.new(env).params
    env['hyperdrive.page']     = params.delete('page') { 1 }.to_i
    env['hyperdrive.page']     = 1 if env['hyperdrive.page'] < 1
    env['hyperdrive.per_page'] = params.delete('per_page') { default_per_page }.to_i
    env['hyperdrive.per_page'] = default_per_page if env['hyperdrive.per_page'] == 0
  end
  @app.call(env)
end