Class: Grape::Formatter::Paginater::PageLinks

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/paginater/page/page_links.rb

Constant Summary collapse

",".freeze

Constants included from Constants

Constants::DefaultPage, Constants::DefaultPageSize, Constants::HEADER_LAST, Constants::HEADER_LINK, Constants::HEADER_NEXT, Constants::KaminariParam, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::OneHour, Constants::PARAM_PAGE, Constants::PARAM_PER_PAGE, Constants::PARAM_START_PAGE, Constants::SetCookie, Constants::SimpleParam, Constants::WrapperParam

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_headers) ⇒ PageLinks

let(:header) { => “<api.com/users?page=4&size=20>; rel="next", <…>; rel="last", <…>; rel="first", <…>; rel="prev"”



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/paginater/page/page_links.rb', line 20

def initialize(response_headers)
  link_header = response_headers[HEADER_LINK]
  if link_header
    return unless link_header =~ /(next|first|last|prev)/

    link_header.split(DELIM_LINKS).each do |link|
      if link.strip =~ /<([^>]+)>; rel=\"([^\"]+)\"/
        url_part, meta_part = $1, $2
        next if !url_part || !meta_part
        case meta_part
        when META_FIRST
          self.first = url_part
        when META_LAST
          self.last = url_part
        when META_NEXT
          self.next = url_part
        when META_PREV
          self.prev = url_part
        end
      end
    end
  else
    # When on the first page
    self.next = response_headers[HEADER_NEXT]
    self.last = response_headers[HEADER_LAST]
  end
end

Instance Attribute Details

#firstObject

Returns the value of attribute first.



11
12
13
# File 'lib/paginater/page/page_links.rb', line 11

def first
  @first
end

#lastObject

Returns the value of attribute last.



11
12
13
# File 'lib/paginater/page/page_links.rb', line 11

def last
  @last
end

#nextObject

Returns the value of attribute next.



11
12
13
# File 'lib/paginater/page/page_links.rb', line 11

def next
  @next
end

#prevObject

Returns the value of attribute prev.



11
12
13
# File 'lib/paginater/page/page_links.rb', line 11

def prev
  @prev
end