Class: IPinfoMiddleware

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of IPinfoMiddleware.



20
21
22
23
24
25
26
# File 'lib/ipinfo-rails.rb', line 20

def initialize(app, options = {})
    @app = app
    @token = options.fetch(:token, nil)
    @ipinfo = IPinfo.create(@token, options)
    @filter = options.fetch(:filter, nil)
    @ip_selector = options.fetch(:ip_selector, DefaultIPSelector)
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ipinfo-rails.rb', line 28

def call(env)
    env['called'] = 'yes'
    request = Rack::Request.new(env)
    ip_selector = @ip_selector.new(request)
    filtered = if @filter.nil?
                   is_bot(request)
               else
                   @filter.call(request)
               end

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

    @app.call(env)
end