Class: Hijacker::Middleware

Inherits:
Object
  • Object
show all
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 =
->(message, env) {
  [404, {}, [message]]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  options = options.dup
  @app = app
  @not_found = options.delete(:not_found) || DEFAULT_NOT_FOUND
  @bad_url   = options.delete(:bad_url)   || DEFAULT_BAD_URL

  unless options.blank?
    raise "Unknown Hijacker::Middleware options #{options.keys.join(",")}"
  end
end

Instance Attribute Details

#bad_urlObject (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_foundObject (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.message, env)
  end

  @app.call(env)
end