Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Created June 30, 2013 20:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidcelis/5896686 to your computer and use it in GitHub Desktop.
Save davidcelis/5896686 to your computer and use it in GitHub Desktop.
Consuming Link header pagination in Ruby
# Link: <http://example.com/resources?page=2>; rel="next", <http://example.com/resources?page=5>; rel="last"
links = {}
headers['Link'].split(',').each do |link|
link.strip!
parts = link.match(/<(.+)>; *rel="(.+)"/)
links[parts[2]] = parts[1]
end
puts links.inspect
# {
# "next" => "http://example.com/resources?page=2",
# "last" => "http://example.com/resources?page=5"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment