Class: JsonApiable::PaginationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/json_apiable/pagination_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_arg, no_pagination_arg, default_page_size) ⇒ PaginationParser

Returns a new instance of PaginationParser.



11
12
13
14
15
# File 'lib/json_apiable/pagination_parser.rb', line 11

def initialize(page_arg, no_pagination_arg, default_page_size)
  @page_param = page_arg
  @no_pagination = no_pagination_arg
  @default_page_size = default_page_size
end

Instance Attribute Details

#default_page_sizeObject (readonly)

Returns the value of attribute default_page_size.



9
10
11
# File 'lib/json_apiable/pagination_parser.rb', line 9

def default_page_size
  @default_page_size
end

#no_paginationObject (readonly)

Returns the value of attribute no_pagination.



9
10
11
# File 'lib/json_apiable/pagination_parser.rb', line 9

def no_pagination
  @no_pagination
end

#page_paramObject (readonly)

Returns the value of attribute page_param.



9
10
11
# File 'lib/json_apiable/pagination_parser.rb', line 9

def page_param
  @page_param
end

Class Method Details

.parse_pagination!(query_params, default_page_size) ⇒ Object



5
6
7
# File 'lib/json_apiable/pagination_parser.rb', line 5

def self.parse_pagination!(query_params, default_page_size)
  PaginationParser.new(query_params[:page], query_params[:no_pagination], default_page_size).parse!
end

Instance Method Details

#parse!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/json_apiable/pagination_parser.rb', line 17

def parse!
  if no_pagination
    jsonapi_page_hash = nil
  elsif invalid_page_param?
    raise ArgumentError, 'page'
  elsif invalid_page_number?
    raise ArgumentError, 'page[number]'
  elsif invalid_page_size?
    raise ArgumentError, 'page[size]'
  else
    jsonapi_page_hash = page_param.presence.to_h.with_indifferent_access
    # convert values to integers
    jsonapi_page_hash = jsonapi_page_hash.merge(jsonapi_page_hash) { |_, v| v.to_i } if jsonapi_page_hash.present?
    jsonapi_page_hash = { number: Configuration::DEFAULT_PAGE_NUMBER, size: default_page_size } if jsonapi_page_hash.blank?
    jsonapi_page_hash[:number] = Configuration::DEFAULT_PAGE_NUMBER if jsonapi_page_hash[:number].blank?
    jsonapi_page_hash[:size] = default_page_size if jsonapi_page_hash[:size].blank?
  end
  jsonapi_page_hash
end