Class: IPinfoMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ipinfo-rails.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, cache_options = {}) ⇒ IPinfoMiddleware

Returns a new instance of IPinfoMiddleware.



5
6
7
8
9
10
11
# File 'lib/ipinfo-rails.rb', line 5

def initialize(app, cache_options = {})
  @app = app

  token = cache_options.fetch(:token, nil)
  @ipinfo = IPinfo::create(@token, cache_options)
  @filter = cache_options.fetch(:filter, nil)
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ipinfo-rails.rb', line 13

def call(env)
  env["called"] = "yes"
  request = Rack::Request.new(env)

  if !@filter.nil?
    filtered = @filter.call(request)
  else
    filtered = is_bot(request)
  end

  if filtered
    env["ipinfo"] = nil
  else
    ip = request.ip
    env["ipinfo"] = @ipinfo.details(ip)
  end

  @app.call(env)
end