Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ipinfo-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.homepage = 'https://ipinfo.io'
s.license = 'Apache-2.0'

s.add_dependency 'IPinfo', '~> 2.3'
s.add_dependency 'IPinfo', '~> 2.4'
s.add_dependency 'rack', '~> 2.0'

s.add_development_dependency 'mocha', '~> 2.7'
Expand Down
31 changes: 31 additions & 0 deletions lib/ipinfo-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rack'
require 'ipinfo'
require 'ipinfo_lite'
require 'ipinfo_core'
require 'ipinfo-rails/ip_selector/default_ip_selector'

def is_bot(request)
Expand Down Expand Up @@ -73,3 +74,33 @@ def call(env)
@app.call(env)
end
end

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

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
end