lib/ipinfo-rails.rb in ipinfo-rails-0.1.1 vs lib/ipinfo-rails.rb in ipinfo-rails-1.0.0

- old
+ new

@@ -1,38 +1,40 @@ +# frozen_string_literal: true + require 'rack' require 'ipinfo' class IPinfoMiddleware - def initialize(app, cache_options = {}) - @app = app + 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 - token = cache_options.fetch(:token, nil) - @ipinfo = IPinfo::create(@token, cache_options) - @filter = cache_options.fetch(:filter, nil) - end + def call(env) + env['called'] = 'yes' + request = Rack::Request.new(env) - def call(env) - env["called"] = "yes" - request = Rack::Request.new(env) + filtered = if @filter.nil? + is_bot(request) + else + @filter.call(request) + end - 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 - if filtered - env["ipinfo"] = nil - else - ip = request.ip - env["ipinfo"] = @ipinfo.details(ip) + @app.call(env) end - @app.call(env) - end + private - private def is_bot(request) - user_agent = request.user_agent.downcase - user_agent.include?("bot") || user_agent.include?("spider") + user_agent = request.user_agent.downcase + user_agent.include?('bot') || user_agent.include?('spider') end end