Class: IPinfoLiteMiddleware

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of IPinfoLiteMiddleware.



50
51
52
53
54
55
56
# File 'lib/ipinfo-rails.rb', line 50

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

Instance Method Details

#call(env) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ipinfo-rails.rb', line 58

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