Created
June 30, 2013 20:14
-
-
Save davidcelis/5896686 to your computer and use it in GitHub Desktop.
Consuming Link header pagination in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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