Last active
July 30, 2021 18:02
-
-
Save thesowah/0ca5e1b4b3c61bfe8e13 to your computer and use it in GitHub Desktop.
Parse link headers from Github API 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
require 'rest_client' | |
class Plh | |
def self.parse_link_header(url, params={}) | |
response = RestClient.get url, params | |
links = Hash.new | |
parts = response.headers[:link].split(',') | |
# Parse each part into a named link | |
parts.each do |part, index| | |
section = part.split(';') | |
url = section[0][/<(.*)>/,1] | |
name = section[1][/rel="(.*)"/,1].to_sym | |
links[name] = url | |
end | |
return links | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! A small variation suggestion: