A summary of data about the Ruby ecosystem.

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 feature-flag rubygem ruby-gem ruby-client

Last synced: about 4 hours ago
JSON representation

Repository metadata

Parse Accept and Accept-Language HTTP headers in Ruby.

readme.md

HTTP::Accept

Provides a robust set of parsers for dealing with HTTP Accept, Accept-Language, Accept-Encoding, Accept-Charset headers.

Development Status

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

Here are some examples of how to parse various headers.

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

Contributing

We welcome contributions to this project.

  1. Fork it.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Create new Pull Request.

Developer Certificate of Origin

This project uses the Developer Certificate of Origin. All contributors to this project must agree to this document to have their contributions accepted.

Contributor Covenant

This project is governed by the Contributor Covenant. All contributors and participants agree to abide by its terms.


Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 2 days ago

Total Commits: 88
Total Committers: 9
Avg Commits per committer: 9.778
Development Distribution Score (DDS): 0.159

Commits in past year: 4
Committers in past year: 1
Avg Commits per committer in past year: 4.0
Development Distribution Score (DDS) in past year: 0.0

Name Email Commits
Samuel Williams s****s@o****z 74
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
Andy Brody g****t@a****m 1
Robert Pritzkow r****w@a****t 1
Alif Rachmawadi c****e@s****m 1

Committer domains:


Issue and Pull Request metadata

Last synced: 5 days ago

Total issues: 0
Total pull requests: 0
Average time to close issues: N/A
Average time to close pull requests: N/A
Total issue authors: 0
Total pull request authors: 0
Average comments per issue: 0
Average comments per pull request: 0
Merged pull request: 0
Bot issues: 0
Bot pull requests: 0

Past year issues: 0
Past year pull requests: 0
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 0
Past year pull request authors: 0
Past year average comments per issue: 0
Past year average comments per pull request: 0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/socketry/http-accept

Top Issue Authors

  • pdoconnell (1)

Top Pull Request Authors

  • alexisbernard (2)
  • ioquatix (1)
  • dependabot[bot] (1)

Top Issue Labels

Top Pull Request Labels

  • dependencies (1)
  • github_actions (1)

Package metadata

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.1+incompatible (published about 2 years ago)
  • Last Synced: 2026-02-28T00:01:37.804Z (3 days ago)
  • Versions: 18
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent packages count: 9.56%
    • Average: 10.169%
    • Dependent repos count: 10.779%

Dependencies

gems.rb rubygems
  • bake-gem >= 0
  • bake-modernize >= 0
http-accept.gemspec rubygems
  • bundler >= 0 development
  • covered >= 0 development
  • rspec ~> 3.0 development
.github/workflows/coverage.yaml actions
  • actions/checkout v4 composite
  • actions/download-artifact v3 composite
  • actions/upload-artifact v3 composite
  • ruby/setup-ruby v1 composite
.github/workflows/documentation.yaml actions
  • actions/checkout v4 composite
  • actions/deploy-pages v3 composite
  • actions/upload-pages-artifact v2 composite
  • ruby/setup-ruby v1 composite
.github/workflows/test-external.yaml actions
  • actions/checkout v4 composite
  • ruby/setup-ruby v1 composite
.github/workflows/test.yaml actions
  • actions/checkout v4 composite
  • ruby/setup-ruby v1 composite

Score: -Infinity