lib/raisin/middleware.rb in raisin-0.1.2 vs lib/raisin/middleware.rb in raisin-0.2.0
- old
+ new
@@ -2,31 +2,29 @@
#
# Middleware responsable to filter HTTP Accept header
# It stores version and format accepted by the client in the env.
#
class Middleware
- ACCEPT_REGEXP = /application\/vnd\.(?<vendor>[a-z]+)-(?<version>v[0-9]+)\+(?<format>[a-z]+)?/
+ ACCEPT_REGEXP = /\Aapplication\/vnd\.(?<vendor>[a-z]+).(?<version>v[0-9]+)\+(?<format>[a-z]+)\Z/
def initialize(app)
- @app = app
- @vendor = Configuration.version.vendor
+ @app = app
+ @vendor = Raisin.vendor
end
def call(env)
- @env = env
- verify_accept_header
- @app.call(@env)
+ extract_version_from_accept_header(ActionDispatch::Request.new(env))
+ @app.call(env)
end
private
- def verify_accept_header
- if (matches = ACCEPT_REGEXP.match(@env['HTTP_ACCEPT'])) && @vendor == matches[:vendor]
- @env['raisin.version'] = matches[:version]
- @env['raisin.format'] = "application/#{matches[:format]}"
- true
- else
- false
+ def extract_version_from_accept_header(req)
+ header = req.get_header('HTTP_ACCEPT'.freeze).to_s.strip
+
+ if (matches = ACCEPT_REGEXP.match(header)) && @vendor == matches[:vendor]
+ req.set_header('raisin.version'.freeze, matches[:version])
+ req.format = matches[:format]
end
end
end
-end
\ No newline at end of file
+end