class Ground::UrlSuffix

When request’s path contains a suffix like json, xml etc, it will git rid of the suffix and set the accept header by the suffix

Example:

get '/book/:id.json'

the request path ‘/book/2.json’ will become ‘/book/2’, and the request accept header will be added a ‘application/json’ mime type

Public Class Methods

new(app) click to toggle source
# File lib/ground/rack/url_suffix.rb, line 15
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/ground/rack/url_suffix.rb, line 19
def call(env)
  suffix = /\.(\w+)$/
  if env['PATH_INFO'].match(suffix)
    accept_type = suffix_to_accept_type $1
    if accept_type
      env['HTTP_ACCEPT'] = "#{accept_type}, #{env['HTTP_ACCEPT']}"
      env['PATH_INFO'] = env['PATH_INFO'].sub(suffix, '')
    end
  end

  @app.call(env)

end

Private Instance Methods

suffix_to_accept_type(suffix) click to toggle source
# File lib/ground/rack/url_suffix.rb, line 35
def suffix_to_accept_type(suffix)
  accept = Ground::MimeType.detect {|_, v| v == suffix.to_sym}
  accept[0] if accept
end