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.



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

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



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

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

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

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

    @app.call(env)
end