Class: Hijacker::Middleware
- Inherits:
-
Object
- Object
- Hijacker::Middleware
- Defined in:
- lib/hijacker/middleware.rb
Constant Summary collapse
- HEADER_KEY =
"HTTP_X_HIJACKER_DB".freeze
- DEFAULT_NOT_FOUND =
->(database, env) { [404, {}, ["Database #{database} not found"]] }
- DEFAULT_BAD_URL =
->(, env) { [404, {}, []] }
Instance Attribute Summary collapse
-
#bad_url ⇒ Object
readonly
Returns the value of attribute bad_url.
-
#not_found ⇒ Object
readonly
Returns the value of attribute not_found.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hijacker/middleware.rb', line 13 def initialize(app, = {}) = .dup @app = app @not_found = .delete(:not_found) || DEFAULT_NOT_FOUND @bad_url = .delete(:bad_url) || DEFAULT_BAD_URL unless .blank? raise "Unknown Hijacker::Middleware options #{.keys.join(",")}" end end |
Instance Attribute Details
#bad_url ⇒ Object (readonly)
Returns the value of attribute bad_url.
11 12 13 |
# File 'lib/hijacker/middleware.rb', line 11 def bad_url @bad_url end |
#not_found ⇒ Object (readonly)
Returns the value of attribute not_found.
11 12 13 |
# File 'lib/hijacker/middleware.rb', line 11 def not_found @not_found end |
Instance Method Details
#call(env) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hijacker/middleware.rb', line 24 def call(env) begin Hijacker.connect(*determine_databases(env)) rescue Hijacker::InvalidDatabase => e return not_found.call(e.database, env) rescue Hijacker::UnparseableURL => e return bad_url.call(e., env) end @app.call(env) end |