Search code examples
sinatrarack

Sinatra accept header option value


Given this accept header:

application/vnd.example.api.json; version=42

Is there a simple way to fetch the value of version, without parsing env['HTTP_ACCEPT']?

BTW:

Inspecting request.accept, we can found a @params instance method:

[#<Sinatra::Request::AcceptEntry:0x007fd214b3e500 @entry="application/vnd.example.api.json; version=42", @type="application/vnd.example.api.json", @params={"version"=>"42"}, @q=1.0>]

They may be a way to access it...


Solution

  • Short answer should be: YES

    Long answer: go here: https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L74 look at the code:

    class AcceptEntry
      attr_accessor :params
      [...]
    end
    

    So this should boil down to:

    request.accept[0].params['version'] #=> 42