https://github.com/socketry/http-accept
Parse Accept and Accept-Language HTTP headers in Ruby.
https://github.com/socketry/http-accept
Keywords
http http-client http-server parsing
Keywords from Contributors
documentation-tool ruby-cli ruby-gem rubygems feature-flag
Last synced: about 8 hours ago
JSON representation
Repository metadata
Parse Accept and Accept-Language HTTP headers in Ruby.
- Host: GitHub
- URL: https://github.com/socketry/http-accept
- Owner: socketry
- License: mit
- Created: 2016-02-03T13:41:56.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2026-09-06T10:14:34.000Z (29 days ago)
- Last Synced: 2026-09-06T12:23:45.271Z (29 days ago)
- Topics: http, http-client, http-server, parsing
- Language: Ruby
- Size: 114 KB
- Stars: 87
- Watchers: 7
- Forks: 12
- Open Issues: 4
- Releases: 2
-
Metadata Files:
- Readme: readme.md
- License: license.md
readme.md
HTTP::Accept
Provides a robust set of parsers for dealing with HTTP Accept, Accept-Language, Accept-Encoding, Accept-Charset headers.
Motivation
I've been developing some tools for building RESTful endpoints and part of that involved versioning. After reviewing the options, I settled on using the Accept: application/json;version=1 method as outlined here.
The version=1 part of the media-type is a parameter as defined by RFC7231 Section 3.1.1.1. After reviewing several existing different options for parsing the Accept: header, I noticed a disturbing trend: header.split(','). Because parameters may contain quoted strings which contain commas, this is clearly not an appropriate way to parse the header.
I am concerned about correctness, security and performance. As such, I implemented this gem to provide a simple high level interface for both parsing and correctly interpreting these headers.
Installation
Add this line to your application's Gemfile:
gem "http-accept"
And then execute:
$ bundle
Or install it yourself as:
$ gem install http-accept
You can then require it in your code like so:
require "http/accept"
Usage
Please see the project documentation for more details.
Parsing Accept: headers
You can parse the incoming Accept: header:
media_types = HTTP::Accept::MediaTypes.parse("text/html;q=0.5, application/json; version=1")
expect(media_types[0].mime_type).to be == "application/json"
expect(media_types[0].parameters).to be == {"version" => "1"}
expect(media_types[1].mime_type).to be == "text/html"
expect(media_types[1].parameters).to be == {"q" => "0.5"}
Normally, you'd want to match the media types against some set of available mime types:
module ToJSON
def content_type
HTTP::Accept::ContentType.new("application", "json", charset: "utf-8")
end
# Used for inserting into map.
def split(*args)
content_type.split(*args)
end
def convert(object, options)
object.to_json
end
end
module ToXML
# Are you kidding?
end
map = HTTP::Accept::MediaTypes::Map.new
map << ToJSON
map << ToXML
object, media_range = map.for(media_types)
content = object.convert(model, media_range.parameters)
response = [200, {"Content-Type" => object.content_type}, [content]]
Parsing Accept-Language: headers
You can parse the incoming Accept-Language: header:
languages = HTTP::Accept::Languages.parse("da, en-gb;q=0.8, en;q=0.7")
expect(languages[0].locale).to be == "da"
expect(languages[1].locale).to be == "en-gb"
expect(languages[2].locale).to be == "en"
Normally, you'd want to match the languages against some set of available localizations:
available_localizations = HTTP::Accept::Languages::Locales.new(["en-nz", "en-us"])
# Given the languages that the user wants, and the localizations available, compute the set of desired localizations.
desired_localizations = available_localizations & languages
The desired_localizations in the example above is a subset of available_localizations.
HTTP::Accept::Languages::Locales provides an efficient data-structure for matching the Accept-Languages header to set of available localizations according to https://tools.ietf.org/html/rfc7231#section-5.3.5 and https://tools.ietf.org/html/rfc4647#section-2.3
Releases
Please see the project releases for all releases.
v2.2.2
- Fix handling of quoted strings.
Contributing
We welcome contributions to this project.
- Fork the repository.
- Create your feature branch (
git checkout -b my-new-feature). - Commit your changes (
git commit -am 'Add some feature.'). - Push to the branch (
git push origin my-new-feature). - Create a new pull request.
Running Tests
To run the test suite:
$ bundle exec sus
Making Releases
To make a new release:
$ bundle exec bake gem:release:patch # or minor or major
Developer Certificate of Origin
In order to protect users of this project, we require all contributors to comply with the Developer Certificate of Origin. This ensures that all contributions are properly licensed and attributed.
Community Guidelines
This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
Owner metadata
- Name: Socketry
- Login: socketry
- Email: contact@socketry.io
- Kind: organization
- Description: Modern asynchronous and timeout-capable I/O for Ruby.
- Website: https://socketry.io
- Location:
- Twitter: ioquatix
- Company:
- Icon url: https://avatars.githubusercontent.com/u/22138037?v=4
- Repositories: 216
- Last ynced at: 2026-10-01T20:28:47.407Z
- Profile URL: https://github.com/socketry
GitHub Events
Total
- Fork event: 1
- Issue comment event: 3
- Member event: 2
- Pull request event: 4
- Pull request review comment event: 1
- Pull request review event: 3
- Push event: 2
- Release event: 1
- Watch event: 5
Last Year
- Issue comment event: 1
- Member event: 2
- Pull request review comment event: 1
- Pull request review event: 1
- Release event: 1
Committers metadata
Last synced: 1 day ago
Total Commits: 92
Total Committers: 10
Avg Commits per committer: 9.2
Development Distribution Score (DDS): 0.163
Commits in past year: 4
Committers in past year: 2
Avg Commits per committer in past year: 2.0
Development Distribution Score (DDS) in past year: 0.25
| Name | Commits | |
|---|---|---|
| Samuel Williams | s****s@o****z | 77 |
| Matthew Kerwin | m****n@q****u | 4 |
| Alexis Bernard | a****s@b****m | 4 |
| Olle Jonsson | o****n@g****m | 1 |
| Khaled Hassan Hussein | k****7@g****m | 1 |
| Ian Oxley | i****y@s****m | 1 |
| Dushan Karovich-Wynne | d****n@r****g | 1 |
| Andy Brody | g****t@a****m | 1 |
| Robert Pritzkow | r****w@a****t | 1 |
| Alif Rachmawadi | c****e@s****m | 1 |
Committer domains:
- subosito.com: 1
- aboutsource.net: 1
- abrody.com: 1
- rubygems.org: 1
- sage.com: 1
- basesecrete.com: 1
- qut.edu.au: 1
- oriontransfer.co.nz: 1
Issue and Pull Request metadata
Last synced: 1 day ago
Total issues: 1
Total pull requests: 5
Average time to close issues: over 4 years
Average time to close pull requests: about 8 hours
Total issue authors: 1
Total pull request authors: 4
Average comments per issue: 4.0
Average comments per pull request: 0.4
Merged pull request: 3
Bot issues: 0
Bot pull requests: 1
Past year issues: 0
Past year pull requests: 1
Past year average time to close issues: N/A
Past year average time to close pull requests: about 23 hours
Past year issue authors: 0
Past year pull request authors: 1
Past year average comments per issue: 0
Past year average comments per pull request: 1.0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- pdoconnell (1)
Top Pull Request Authors
- alexisbernard (2)
- ioquatix (1)
- dependabot[bot] (1)
- dkw-oss (1)
Top Issue Labels
Top Pull Request Labels
- github_actions (1)
- dependencies (1)
Package metadata
- Total packages: 3
- Total downloads: unknown
- Total dependent packages: 0 (may contain duplicates)
- Total dependent repositories: 0 (may contain duplicates)
- Total versions: 23
proxy.golang.org: github.com/socketry/http-accept
- Homepage:
- Documentation: https://pkg.go.dev/github.com/socketry/http-accept#section-documentation
- Licenses: mit
- Latest release: v2.2.2+incompatible (published 28 days ago)
- Last Synced: 2026-10-04T06:32:57.320Z (1 day ago)
- Versions: 19
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 9.56%
- Average: 10.169%
- Dependent repos count: 10.779%
gentoo-portage: dev-ruby/http-accept
Parse Accept and Accept-Language HTTP headers
- Homepage: https://github.com/socketry/http-accept
- Documentation: https://packages.gentoo.org/packages/dev-ruby/http-accept
- Licenses: MIT
- Latest release: 2.2.1 (published 6 months ago)
- Last Synced: 2026-05-27T02:46:00.129Z (4 months ago)
- Versions: 2
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
guix: ruby-http-accept
Parse Accept and Accept-Language HTTP headers
- Homepage: https://github.com/socketry/http-accept
- Documentation: https://git.savannah.gnu.org/cgit/guix.git/tree/gnu/packages/ruby-xyz.scm#n11089
- Licenses: expat
- Latest release: 2.2.0 (published 7 months ago)
- Last Synced: 2026-04-27T16:19:28.230Z (5 months ago)
- Versions: 2
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
Dependencies
- bake-gem >= 0
- bake-modernize >= 0
- bundler >= 0 development
- covered >= 0 development
- rspec ~> 3.0 development
- actions/checkout v4 composite
- actions/download-artifact v3 composite
- actions/upload-artifact v3 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- actions/deploy-pages v3 composite
- actions/upload-pages-artifact v2 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
Score: -Infinity