{"id":15389479,"url":"https://github.com/jgarber623/link-header-parser-ruby","last_synced_at":"2025-10-09T15:10:18.071Z","repository":{"id":53171392,"uuid":"189108537","full_name":"jgarber623/link-header-parser-ruby","owner":"jgarber623","description":"A Ruby gem for parsing HTTP Link headers.","archived":false,"fork":false,"pushed_at":"2024-05-08T16:10:13.000Z","size":169,"stargazers_count":8,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T21:11:49.786Z","etag":null,"topics":["http-headers","link-header","ruby","rubygems"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/link-header-parser","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jgarber623.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-28T21:54:36.000Z","updated_at":"2024-05-08T16:10:11.000Z","dependencies_parsed_at":"2024-03-01T04:29:42.958Z","dependency_job_id":"ec5d1415-49f1-4318-92b2-5e8a36b2b067","html_url":"https://github.com/jgarber623/link-header-parser-ruby","commit_stats":{"total_commits":206,"total_committers":3,"mean_commits":68.66666666666667,"dds":"0.13592233009708743","last_synced_commit":"9eb079d73ba45a37679260f81d38d70e97d028bb"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgarber623%2Flink-header-parser-ruby","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgarber623%2Flink-header-parser-ruby/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgarber623%2Flink-header-parser-ruby/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgarber623%2Flink-header-parser-ruby/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgarber623","download_url":"https://codeload.github.com/jgarber623/link-header-parser-ruby/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248782260,"owners_count":21160717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http-headers","link-header","ruby","rubygems"],"created_at":"2024-10-01T15:01:45.739Z","updated_at":"2025-10-09T15:10:18.065Z","avatar_url":"https://github.com/jgarber623.png","language":"Ruby","readme":"\u003e [!IMPORTANT]\n\u003e This project has moved to [codeberg.org/jgarber/link-header-parser-ruby](https://codeberg.org/jgarber/link-header-parser-ruby).\n\n# link-header-parser-ruby\n\n**A Ruby gem for parsing HTTP Link headers.**\n\n[![Gem](https://img.shields.io/gem/v/link-header-parser.svg?logo=rubygems\u0026style=for-the-badge)](https://rubygems.org/gems/link-header-parser)\n[![Downloads](https://img.shields.io/gem/dt/link-header-parser.svg?logo=rubygems\u0026style=for-the-badge)](https://rubygems.org/gems/link-header-parser)\n[![Build](https://img.shields.io/github/actions/workflow/status/jgarber623/link-header-parser-ruby/ci.yml?branch=main\u0026logo=github\u0026style=for-the-badge)](https://github.com/jgarber623/link-header-parser-ruby/actions/workflows/ci.yml)\n\n## Getting Started\n\nBefore installing and using link-header-parser-ruby, you'll want to have [Ruby](https://www.ruby-lang.org) 3.0 (or newer) installed. Using a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm) is recommended.\n\nlink-header-parser-ruby is developed using Ruby 3.3.1 and is tested against additional Ruby versions using [GitHub Actions](https://github.com/jgarber623/link-header-parser-ruby/actions).\n\n## Installation\n\nAdd link-header-parser-ruby to your project's `Gemfile` and run `bundle install`:\n\n```ruby\nsource \"https://rubygems.org\"\n\ngem \"link-header-parser\"\n```\n\n## Usage\n\nWith link-header-parser-ruby added to your project's `Gemfile` and installed, you may parse a URL's HTTP Link headers by doing:\n\n```ruby\nrequire \"net/http\"\nrequire \"link-header-parser\"\n\nurl = \"https://sixtwothree.org\"\nlink_headers = Net::HTTP.get_response(URI.parse(url)).get_fields(\"Link\")\n\ncollection = LinkHeaderParser.parse(link_headers, base: url)\n```\n\nThe `parse` method accepts two arguments:\n\n1. an `Array` of strings representing HTTP Link headers (e.g. `['\u003c/\u003e; rel=\"home\"', '\u003c/chapters/1\u003e; anchor=\"#copyright\"; rel=\"license\"']`)\n1. a `String` (or any `String`-like object) representing the absolute URL of the resource providing the HTTP Link headers\n\nIn the example above, `collection` is an instance of `LinkHeadersCollection` which includes Ruby's [`Enumerable`](https://ruby-doc.org/core/Enumerable.html) mixin. This mixin allows for use of common methods like `each`, `first`/`last`, and `map`.\n\nFor example, you could retrieve an array of `target_uri`s:\n\n```ruby\nputs collection.map(\u0026:target_uri)\n#=\u003e [\"https://assets.sixtwothree.org/\", \"https://fonts.googleapis.com/\", \"https://fonts.gstatic.com/\", \"https://sixtwothree.org/webmentions\"]\n```\n\n### Working with a `LinkHeadersCollection`\n\nIn addition to the included `Enumerable` methods, the following methods may be used to interact with a `LinkHeadersCollection`:\n\n#### The `relation_types` Method\n\n```ruby\nputs collection.relation_types\n#=\u003e [\"preconnect\", \"webmention\"]\n```\n\n#### The `group_by_relation_type` Method\n\nUsing the `collection` from above, the `group_by_relation_type` method returns a `Hash`:\n\n```ruby\n{\n  preconnect: [\n    #\u003cLinkHeaderParser::LinkHeader target_uri: \"https://assets.sixtwothree.org/\", relation_types: [\"preconnect\"]\u003e,\n    #\u003cLinkHeaderParser::LinkHeader target_uri: \"https://fonts.googleapis.com/\", relation_types: [\"preconnect\"]\u003e,\n    #\u003cLinkHeaderParser::LinkHeader target_uri: \"https://fonts.gstatic.com/\", relation_types: [\"preconnect\"]\u003e\n  ],\n  webmention: [\n    #\u003cLinkHeaderParser::LinkHeader target_uri: \"https://sixtwothree.org/webmentions\", relation_types: [\"webmention\"]\u003e\n  ]\n}\n```\n\n### Working with a `LinkHeader`\n\nYou may interact with one or more `LinkHeader`s in a `LinkHeadersCollection` using the methods outlined below. The naming conventions for these methods draws heavily on the terminology established in [RFC-5988](https://tools.ietf.org/html/rfc5988) and [RFC-8288](https://tools.ietf.org/html/rfc8288).\n\n#### Link Target ([§ 3.1](https://tools.ietf.org/html/rfc8288#section-3.1))\n\n```ruby\nlink_header = LinkHeaderParser.parse(%(\u003c/index.html\u003e; rel=\"home\"), base: \"https://example.com/\").first\n\nlink_header.target_string\n#=\u003e \"/index.html\"\n\nlink_header.target_uri\n#=\u003e \"https://example.com/index.html\"\n```\n\nThe `target_string` method returns a string of the value between the opening and closing angle brackets at the beginning of the Link header. The `target_uri` method returns a string representing the resolved URL.\n\n#### Link Context ([§ 3.2](https://tools.ietf.org/html/rfc8288#section-3.2))\n\n```ruby\nlink_header = LinkHeaderParser.parse(%(\u003c/chapters/1\u003e; anchor=\"#copyright\"; rel=\"license\"), base: \"https://example.com/\").first\n\nlink_header.context_string\n#=\u003e \"#copyright\"\n\nlink_header.context_uri\n#=\u003e \"https://example.com/chapters/1#copyright\"\n```\n\nThe `anchor` parameter's value may be a fragment identifier (e.g. `#foo`), a relative URL (e.g. `/foo`), or an absolute URL (e.g. `https://context.example.com`). The `context_string` method returns the `anchor` parameter's value (when present) and defaults to the `target_string` value. The `context_uri` method returns a string representing the resolved URL.\n\n#### Relation Type ([§ 3.3](https://tools.ietf.org/html/rfc8288#section-3.3))\n\n```ruby\nlink_header = LinkHeaderParser.parse(%(\u003c/chapters/1\u003e; rel=\"prev start\"), base: \"https://example.com/\").first\n\nlink_header.relations_string\n#=\u003e \"prev start\"\n\nlink_header.relation_types\n#=\u003e [\"prev\", \"start\"]\n```\n\n#### Link Parameters ([Appendix B.3](https://tools.ietf.org/html/rfc8288#appendix-B.3))\n\n```ruby\nlink_header = LinkHeaderParser.parse(%(\u003c/posts.rss\u003e; rel=\"alternate\"; hreflang=\"en-US\"; title=\"sixtwothree.org: Posts\"; type=\"application/rss+xml\"), base: \"https://sixtwothree.org\").first\n\nlink_header.link_parameters\n#=\u003e [#\u003cLinkHeaderParser::LinkHeaderParameter name: \"rel\", value: \"alternate\"\u003e, #\u003cLinkHeaderParser::LinkHeaderParameter name: \"hreflang\", value: \"en-US\"\u003e, #\u003cLinkHeaderParser::LinkHeaderParameter name: \"title\", value: \"sixtwothree.org: Posts\"\u003e, #\u003cLinkHeaderParser::LinkHeaderParameter name: \"type\", value: \"application/rss+xml\"\u003e]\n```\n\nNote that the `Array` returned by the `link_parameters` method may include multiple `LinkHeaderParameter`s with the same name depending on the provided Link header. Certain methods on `LinkHeader` will return values from the first occurrence of a parameter name (e.g. `link_header.relations_string`) in accordance with [RFC-8288](https://tools.ietf.org/html/rfc8288).\n\n## Acknowledgments\n\nlink-header-parser-ruby is written and maintained by [Jason Garber](https://sixtwothree.org).\n\n## License\n\nlink-header-parser-ruby is freely available under the [MIT License](https://opensource.org/licenses/MIT). Use it, learn from it, fork it, improve it, change it, tailor it to your needs.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgarber623%2Flink-header-parser-ruby","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgarber623%2Flink-header-parser-ruby","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgarber623%2Flink-header-parser-ruby/lists"}