Class: Uchi::Pagy::Request
- Inherits:
-
Object
- Object
- Uchi::Pagy::Request
- Defined in:
- lib/uchi/pagy/classes/request.rb
Overview
Decouple the reuest from the env, allowing non-rack apps to use pagy by passing a hash. Resolve :page and :limit, supporting the :jsonapi option. Support for URL composition.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#initialize(request, options = {}) ⇒ Request
constructor
A new instance of Request.
- #resolve_limit(options) ⇒ Object
- #resolve_page(options, force_integer: true) ⇒ Object
Constructor Details
#initialize(request, options = {}) ⇒ Request
Returns a new instance of Request.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/uchi/pagy/classes/request.rb', line 8 def initialize(request, = {}) @base_url, @path, @query, = if request.is_a?(Hash) request.values_at(:base_url, :path, :query, :cookie) else [request.base_url, request.path, request.GET, request.['pagy']] end @jsonapi = @query['page'] && [:jsonapi] raise JsonapiReservedParamError, @query['page'] if @jsonapi && !@query['page'].respond_to?(:fetch) end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
19 20 21 |
# File 'lib/uchi/pagy/classes/request.rb', line 19 def base_url @base_url end |
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
19 20 21 |
# File 'lib/uchi/pagy/classes/request.rb', line 19 def end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
19 20 21 |
# File 'lib/uchi/pagy/classes/request.rb', line 19 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
19 20 21 |
# File 'lib/uchi/pagy/classes/request.rb', line 19 def query @query end |
Instance Method Details
#resolve_limit(options) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/uchi/pagy/classes/request.rb', line 28 def resolve_limit() limit_key = [:limit_key] || DEFAULT[:limit_key] return [:limit] || DEFAULT[:limit] \ unless [:client_max_limit] && (requested_limit = @jsonapi ? @query['page'][limit_key] : @query[limit_key]) [requested_limit.to_i, [:client_max_limit]].min end |
#resolve_page(options, force_integer: true) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/uchi/pagy/classes/request.rb', line 21 def resolve_page(, force_integer: true) page_key = [:page_key] || DEFAULT[:page_key] page = @jsonapi ? @query['page'][page_key] : @query[page_key] page = nil if page == '' # fix for app-generated queries like ?page= force_integer ? (page || 1).to_i : page end |