ipinfo-rails 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7afb846da30144077a602885bacbd5ae683083c7c5cf7ef96669b87ad1953e64
4
- data.tar.gz: 0cd3f7f0be43e6ea9bb5fe027efdc072d52ff23a4d2f725638014e58060f2fb4
3
+ metadata.gz: 48b6909038d2fd6aeba7e2ab4003e03488696606e4595e89c7b7984435ff4269
4
+ data.tar.gz: 35858d6386399aa7f553456d188006f1553ef1fad571aee3d590739bcca75090
5
5
  SHA512:
6
- metadata.gz: d5b289c284e54ec051c93c81fd5ef4cbf0a23e83bf621730e0bc3f6cc64908b711aa70445c48d5c0fd6a5aae919f9541ed68d5546642465d45f29072d3ffd28c
7
- data.tar.gz: 1a0cfd1a9379bd0c1542398f37c24a00ada8d28cb2462b70649dca1688edb5465d5886cbd4301645d9382678ed1346b0c4c278349bafe04c505e73b0170c8dd9
6
+ metadata.gz: 915e1bae34ebbbb5b52139bd4141e7b06b68578fcc6b03d9e71e40620c172b28b99e20f6f8713a5e40ef23e951483e67150e05cf0f298e18297812f9ac595ba9
7
+ data.tar.gz: 66e90173dc81ff9eee5d3a4022e40d87881a5e099699f7b24d187020d7f64f0175491259dd8e75436699be4a0c4a78ca1cbd851cb461f2545d036ae8b30899d8
@@ -0,0 +1,31 @@
1
+ name: Tests
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - master
10
+
11
+ jobs:
12
+ run:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby-version: ["3.2", "3.3", "3.4"]
17
+
18
+ steps:
19
+ - name: Checkout
20
+ uses: actions/checkout@v3
21
+
22
+ - name: Set up Ruby ${{ matrix.ruby-version }}
23
+ uses: ruby/setup-ruby@v1
24
+ with:
25
+ ruby-version: ${{ matrix.ruby-version }}
26
+
27
+ - name: Install dependencies
28
+ run: bundle install
29
+
30
+ - name: Run tests
31
+ run: bundle exec rake
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.2
1
+ 3.2.2
data/Gemfile CHANGED
@@ -8,5 +8,6 @@ group :development do
8
8
  gem 'bundler'
9
9
  gem 'minitest'
10
10
  gem 'minitest-reporters'
11
+ gem 'rake'
11
12
  gem 'rubocop'
12
13
  end
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # <img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/> IPinfo Rails Client Library
2
2
 
3
- This is the official Rails client library for the IPinfo.io IP address API, allowing you to lookup your own IP address, or get any of the following details for an IP:
3
+ This is the official Rails client library for the IPinfo.io IP address API, allowing you to look up your own IP address, or get any of the following details for an IP:
4
4
 
5
- - [Geolocation](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude and longitude)
6
- - [ASN](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting or company)
5
+ - [Geolocation](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude, and longitude)
6
+ - [ASN](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
7
7
  - [Company](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)
8
8
  - [Carrier](https://ipinfo.io/ip-carrier-api) (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)
9
9
 
@@ -11,10 +11,12 @@ Check all the data we have for your IP address [here](https://ipinfo.io/what-is-
11
11
 
12
12
  ## Getting Started
13
13
 
14
- You'll need an IPinfo API access token, which you can get by singing up for a free account at [https://ipinfo.io/signup](https://ipinfo.io/signup).
14
+ You'll need an IPinfo API access token, which you can get by signing up for a free account at [https://ipinfo.io/signup](https://ipinfo.io/signup).
15
15
 
16
16
  The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see [https://ipinfo.io/pricing](https://ipinfo.io/pricing)
17
17
 
18
+ The library also supports the Lite API, see the [Lite API section](#lite-api) for more info.
19
+
18
20
  ### Installation
19
21
 
20
22
  1. Option 1) Add this line to your application's Gemfile:
@@ -118,9 +120,11 @@ request.env['ipinfo'].all ==
118
120
 
119
121
  ## Configuration
120
122
 
121
- In addition to the steps listed in the Installation section it is possible to configure the library with more detail. The following arguments are allowed and are described in detail below.
123
+ In addition to the steps listed in the Installation section, it is possible to configure the library with more detail. The following arguments are allowed and are described in detail below.
122
124
 
123
125
  ```ruby
126
+ require 'ipinfo-rails/ip_selector/xforwarded_ip_selector'
127
+
124
128
  config.middleware.use(IPinfoMiddleware, {
125
129
  token: "<your_token>",
126
130
  ttl: "",
@@ -129,6 +133,31 @@ config.middleware.use(IPinfoMiddleware, {
129
133
  http_client: "",
130
134
  countries: "",
131
135
  filter: "",
136
+ ip_selector: XForwardedIPSelector,
137
+ })
138
+ ```
139
+
140
+ ### IP Selection Mechanism
141
+
142
+ By default, the source IP on the request is used as the input to IP geolocation.
143
+
144
+ Since the actual desired IP may be something else, the IP selection mechanism is configurable.
145
+
146
+ Here are some built-in mechanisms:
147
+
148
+ - [DefaultIPSelector](./lib/ipinfo-rails/ip_selector/default_ip_selector.rb)
149
+ - [XForwardedIPSelector](./lib/ipinfo-rails/ip_selector/xforwarded_ip_selector.rb)
150
+
151
+ #### Using a custom IP selector
152
+
153
+ In case a custom IP selector is required, you may implement the `IPSelectorInterface` and pass the class to `ip_selector` in config.
154
+
155
+ ```ruby
156
+ require 'custom-package/custom_ip_selector'
157
+
158
+ config.middleware.use(IPinfoMiddleware, {
159
+ token: "<your_token>",
160
+ ip_selector: CustomIPSelector,
132
161
  })
133
162
  ```
134
163
 
@@ -217,9 +246,22 @@ config.middleware.use(IPinfoMiddleware, {
217
246
 
218
247
  This simple lambda function will filter out requests coming from your local computer.
219
248
 
249
+ ## Lite API
250
+
251
+ The library gives the possibility to use the [Lite API](https://ipinfo.io/developers/lite-api) too, authentication with your token is still required.
252
+
253
+ The returned details are slightly different from the Core API, but it has the same configurations options.
254
+
255
+ You can use it like so:
256
+
257
+ ```ruby
258
+ require 'ipinfo-rails'
259
+ config.middleware.use(IPinfoLiteMiddleware, {token: "<your_token>"})
260
+ ```
261
+
220
262
  ## Other Libraries
221
263
 
222
- There are official IPinfo client libraries available for many languages including PHP, Go, Java, Ruby, and many popular frameworks such as Django, Rails and Laravel. There are also many third party libraries and integrations available for our API.
264
+ There are official IPinfo client libraries available for many languages including PHP, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.
223
265
 
224
266
  ## About IPinfo
225
267
 
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/ipinfo-rails.gemspec CHANGED
@@ -9,7 +9,6 @@ Gem::Specification.new do |s|
9
9
  s.name = 'ipinfo-rails'
10
10
  s.version = IPinfoRails::VERSION
11
11
  s.required_ruby_version = '>= 2.5.0'
12
- s.date = '2018-12-10'
13
12
  s.summary = 'The official Rails gem for IPinfo. IPinfo prides itself on ' \
14
13
  'being the most reliable, accurate, and in-depth source of ' \
15
14
  'IP address data available anywhere. We process terabytes ' \
@@ -17,16 +16,21 @@ Gem::Specification.new do |s|
17
16
  'carrier and IP type data sets. You can visit our developer ' \
18
17
  'docs at https://ipinfo.io/developers.'
19
18
  s.description = s.summary
20
- s.authors = ['James Timmins', 'Uman Shahzad']
21
- s.email = ['jameshtimmins@gmail.com', 'uman@mslm.io']
19
+ s.authors = ['IPinfo releases']
20
+ s.email = ['releases@ipinfo.io']
22
21
  s.homepage = 'https://ipinfo.io'
23
22
  s.license = 'Apache-2.0'
24
23
 
25
- s.add_runtime_dependency 'IPinfo', '~> 1.0.1'
26
- s.add_runtime_dependency 'rack', '~> 2.0'
24
+ s.add_dependency 'IPinfo', '~> 2.3'
25
+ s.add_dependency 'rack', '~> 2.0'
26
+
27
+ s.add_development_dependency 'mocha', '~> 2.7'
27
28
 
28
29
  s.files = `git ls-files -z`.split("\x0").reject do |f|
29
30
  f.match(%r{^(test|spec|features)/})
30
31
  end
31
32
  s.require_paths = ['lib']
33
+ s.metadata = {
34
+ 'rubygems_mfa_required' => 'true'
35
+ }
32
36
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'ipinfo-rails/ip_selector/ip_selector_interface'
3
+
4
+ class DefaultIPSelector
5
+ include IPSelectorInterface
6
+
7
+ def initialize(request)
8
+ @request = request
9
+ end
10
+
11
+ def get_ip()
12
+ return @request.ip
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IPSelectorInterface
4
+ class InterfaceNotImplemented < StandardError; end
5
+ def get_ip()
6
+ raise InterfaceNotImplemented
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ require 'ipinfo-rails/ip_selector/ip_selector_interface'
3
+
4
+ class XForwardedIPSelector
5
+ include IPSelectorInterface
6
+
7
+ def initialize(request)
8
+ @request = request
9
+ end
10
+
11
+ def get_ip()
12
+ x_forwarded = @request.env['HTTP_X_FORWARDED_FOR']
13
+ if !x_forwarded || x_forwarded.empty?
14
+ return @request.ip
15
+ else
16
+ return x_forwarded.split(',' , -1)[0]
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IPinfoRails
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/ipinfo-rails.rb CHANGED
@@ -2,19 +2,31 @@
2
2
 
3
3
  require 'rack'
4
4
  require 'ipinfo'
5
+ require 'ipinfo_lite'
6
+ require 'ipinfo-rails/ip_selector/default_ip_selector'
7
+
8
+ def is_bot(request)
9
+ if request.user_agent
10
+ user_agent = request.user_agent.downcase
11
+ user_agent.include?('bot') || user_agent.include?('spider')
12
+ else
13
+ false
14
+ end
15
+ end
5
16
 
6
17
  class IPinfoMiddleware
7
- def initialize(app, cache_options = {})
18
+ def initialize(app, options = {})
8
19
  @app = app
9
- @token = cache_options.fetch(:token, nil)
10
- @ipinfo = IPinfo.create(@token, cache_options)
11
- @filter = cache_options.fetch(:filter, nil)
20
+ @token = options.fetch(:token, nil)
21
+ @ipinfo = IPinfo.create(@token, options)
22
+ @filter = options.fetch(:filter, nil)
23
+ @ip_selector = options.fetch(:ip_selector, DefaultIPSelector)
12
24
  end
13
25
 
14
26
  def call(env)
15
27
  env['called'] = 'yes'
16
28
  request = Rack::Request.new(env)
17
-
29
+ ip_selector = @ip_selector.new(request)
18
30
  filtered = if @filter.nil?
19
31
  is_bot(request)
20
32
  else
@@ -24,21 +36,40 @@ class IPinfoMiddleware
24
36
  if filtered
25
37
  env['ipinfo'] = nil
26
38
  else
27
- ip = request.ip
39
+ ip = ip_selector.get_ip()
28
40
  env['ipinfo'] = @ipinfo.details(ip)
29
41
  end
30
42
 
31
43
  @app.call(env)
32
44
  end
45
+ end
33
46
 
34
- private
47
+ class IPinfoLiteMiddleware
48
+ def initialize(app, options = {})
49
+ @app = app
50
+ @token = options.fetch(:token, nil)
51
+ @ipinfo = IPinfoLite.create(@token, options)
52
+ @filter = options.fetch(:filter, nil)
53
+ @ip_selector = options.fetch(:ip_selector, DefaultIPSelector)
54
+ end
35
55
 
36
- def is_bot(request)
37
- if request.user_agent
38
- user_agent = request.user_agent.downcase
39
- user_agent.include?('bot') || user_agent.include?('spider')
40
- else
41
- false
42
- end
56
+ def call(env)
57
+ env['called'] = 'yes'
58
+ request = Rack::Request.new(env)
59
+ ip_selector = @ip_selector.new(request)
60
+ filtered = if @filter.nil?
61
+ is_bot(request)
62
+ else
63
+ @filter.call(request)
64
+ end
65
+
66
+ if filtered
67
+ env['ipinfo'] = nil
68
+ else
69
+ ip = ip_selector.get_ip
70
+ env['ipinfo'] = @ipinfo.details(ip)
43
71
  end
72
+
73
+ @app.call(env)
74
+ end
44
75
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipinfo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - James Timmins
8
- - Uman Shahzad
9
- autorequire:
7
+ - IPinfo releases
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2025-09-10 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: IPinfo
@@ -17,14 +16,14 @@ dependencies:
17
16
  requirements:
18
17
  - - "~>"
19
18
  - !ruby/object:Gem::Version
20
- version: 1.0.1
19
+ version: '2.3'
21
20
  type: :runtime
22
21
  prerelease: false
23
22
  version_requirements: !ruby/object:Gem::Requirement
24
23
  requirements:
25
24
  - - "~>"
26
25
  - !ruby/object:Gem::Version
27
- version: 1.0.1
26
+ version: '2.3'
28
27
  - !ruby/object:Gem::Dependency
29
28
  name: rack
30
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,30 +38,49 @@ dependencies:
39
38
  - - "~>"
40
39
  - !ruby/object:Gem::Version
41
40
  version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
42
55
  description: The official Rails gem for IPinfo. IPinfo prides itself on being the
43
56
  most reliable, accurate, and in-depth source of IP address data available anywhere.
44
57
  We process terabytes of data to produce our custom IP geolocation, company, carrier
45
58
  and IP type data sets. You can visit our developer docs at https://ipinfo.io/developers.
46
59
  email:
47
- - jameshtimmins@gmail.com
48
- - uman@mslm.io
60
+ - releases@ipinfo.io
49
61
  executables: []
50
62
  extensions: []
51
63
  extra_rdoc_files: []
52
64
  files:
65
+ - ".github/workflows/test.yml"
53
66
  - ".gitignore"
54
67
  - ".rubocop.yml"
55
68
  - ".ruby-version"
56
69
  - Gemfile
57
70
  - README.md
71
+ - Rakefile
58
72
  - ipinfo-rails.gemspec
59
73
  - lib/ipinfo-rails.rb
74
+ - lib/ipinfo-rails/ip_selector/default_ip_selector.rb
75
+ - lib/ipinfo-rails/ip_selector/ip_selector_interface.rb
76
+ - lib/ipinfo-rails/ip_selector/xforwarded_ip_selector.rb
60
77
  - lib/ipinfo-rails/version.rb
61
78
  homepage: https://ipinfo.io
62
79
  licenses:
63
80
  - Apache-2.0
64
- metadata: {}
65
- post_install_message:
81
+ metadata:
82
+ rubygems_mfa_required: 'true'
83
+ post_install_message:
66
84
  rdoc_options: []
67
85
  require_paths:
68
86
  - lib
@@ -77,8 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
95
  - !ruby/object:Gem::Version
78
96
  version: '0'
79
97
  requirements: []
80
- rubygems_version: 3.1.4
81
- signing_key:
98
+ rubygems_version: 3.4.10
99
+ signing_key:
82
100
  specification_version: 4
83
101
  summary: The official Rails gem for IPinfo. IPinfo prides itself on being the most
84
102
  reliable, accurate, and in-depth source of IP address data available anywhere. We