Class: Footrest::Pagination

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/footrest/pagination.rb

Defined Under Namespace

Classes: Links

Instance Method Summary collapse

Instance Method Details



29
30
31
32
# File 'lib/footrest/pagination.rb', line 29

def find_link(header, rel)
  link = ::LinkHeader.parse(header).links.find{ |link| link['rel'] == rel }
  link.to_a.first if link
end

#on_complete(response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/footrest/pagination.rb', line 15

def on_complete(response)
  if response[:response_headers]
    if link_header = response[:response_headers][:link]
      links = Links.new
      %w(prev next first last current).each do |page|
        link = find_link(link_header, page)
        response["#{page}_page".to_sym] = link
        links.public_send("#{ page }=", link)
      end
      response[:pagination_links] = links
    end
  end
end