File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
2121 s . homepage = 'https://ipinfo.io'
2222 s . license = 'Apache-2.0'
2323
24- s . add_dependency 'IPinfo' , '~> 2.3 '
24+ s . add_dependency 'IPinfo' , '~> 2.4 '
2525 s . add_dependency 'rack' , '~> 2.0'
2626
2727 s . add_development_dependency 'mocha' , '~> 2.7'
Original file line number Diff line number Diff line change 33require 'rack'
44require 'ipinfo'
55require 'ipinfo_lite'
6+ require 'ipinfo_core'
67require 'ipinfo-rails/ip_selector/default_ip_selector'
78
89def is_bot ( request )
@@ -73,3 +74,33 @@ def call(env)
7374 @app . call ( env )
7475 end
7576end
77+
78+ class IPinfoCoreMiddleware
79+ def initialize ( app , options = { } )
80+ @app = app
81+ @token = options . fetch ( :token , nil )
82+ @ipinfo = IPinfoCore . create ( @token , options )
83+ @filter = options . fetch ( :filter , nil )
84+ @ip_selector = options . fetch ( :ip_selector , DefaultIPSelector )
85+ end
86+
87+ def call ( env )
88+ env [ 'called' ] = 'yes'
89+ request = Rack ::Request . new ( env )
90+ ip_selector = @ip_selector . new ( request )
91+ filtered = if @filter . nil?
92+ is_bot ( request )
93+ else
94+ @filter . call ( request )
95+ end
96+
97+ if filtered
98+ env [ 'ipinfo' ] = nil
99+ else
100+ ip = ip_selector . get_ip
101+ env [ 'ipinfo' ] = @ipinfo . details ( ip )
102+ end
103+
104+ @app . call ( env )
105+ end
106+ end
You can’t perform that action at this time.
0 commit comments