Class: Rack::Entrance
- Inherits:
-
Object
- Object
- Rack::Entrance
- Defined in:
- lib/rack/entrance.rb,
lib/rack/entrance/version.rb
Defined Under Namespace
Modules: VERSION
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Entrance
constructor
A new instance of Entrance.
- #internal? ⇒ Boolean
- #internal_ips ⇒ Object
- #raw_user_ip ⇒ Object
- #request ⇒ Object
- #user_ip ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Entrance
Returns a new instance of Entrance.
7 8 9 |
# File 'lib/rack/entrance.rb', line 7 def initialize(app, = {}) @app = app end |
Class Method Details
.internal_cidrs ⇒ Object
11 12 13 |
# File 'lib/rack/entrance.rb', line 11 def self.internal_cidrs ENV['ENTRANCE_INTERNAL_CIDRS'].to_s.split ',' end |
Instance Method Details
#call(env) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/rack/entrance.rb', line 15 def call(env) @env = env request.env['entrance.user_ip'] = user_ip.to_s if user_ip.to_s != '' request.env['entrance.internal_cidrs'] = self.class.internal_cidrs request.env['entrance.internal'] = internal? @app.call env end |
#internal? ⇒ Boolean
37 38 39 |
# File 'lib/rack/entrance.rb', line 37 def internal? internal_ips.any? { |ip| ip.include?(user_ip) } end |
#internal_ips ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/rack/entrance.rb', line 41 def internal_ips @internal_ips ||= self.class.internal_cidrs.map do |cidr| begin ::IPAddr.new cidr rescue ArgumentError nil end end.compact end |
#raw_user_ip ⇒ Object
27 28 29 |
# File 'lib/rack/entrance.rb', line 27 def raw_user_ip (@env["action_dispatch.remote_ip"] || request.ip).to_s end |
#request ⇒ Object
23 24 25 |
# File 'lib/rack/entrance.rb', line 23 def request ::Rack::Request.new @env end |
#user_ip ⇒ Object
31 32 33 34 35 |
# File 'lib/rack/entrance.rb', line 31 def user_ip ::IPAddr.new raw_user_ip rescue ArgumentError nil end |