Sha256: 71a4f15d560a57249e525d590d5d8dc14ba020629445c4e7e8c2c09a33bfa14f

Contents?: true

Size: 766 Bytes

Versions: 1

Compression:

Stored size: 766 Bytes

Contents

require 'rack'
require 'ipinfo'

class IPinfoMiddleware
  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

  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

  private
    def is_bot(request)
      user_agent = request.user_agent.downcase
      user_agent.include?("bot") || user_agent.include?("spider")
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ipinfo-rails-0.1.1 lib/ipinfo-rails.rb