IPinfo 2.2.4 → 2.3.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: ea1d36760a8a4b60cdc8f48946859ba2cf05d71d7f2c7efe11c7333563a9cb8e
4
- data.tar.gz: 49fef88c8e2df4c69006ef76b9d7ad86028dc0cc621e4cb4d529cad060f9802f
3
+ metadata.gz: 717b1a8d10a5372a15836b76df631750216fa668cc31cb582ba95066b39cde54
4
+ data.tar.gz: a57013f371ad393324dc0a52c775bec0fa6a703405b13346ec1cdb6157a273f5
5
5
  SHA512:
6
- metadata.gz: 1465262219a3a9d5a30d387f63fee35721bd667555eff9b6b25d131b1d58093a70087bf51bf6842bf22331573ef796e7a8ad3689539882976783f18e2233b244
7
- data.tar.gz: 97456bb0d405e97448bdb1f6b897914a1aebd749b8f348457e5447630e36a17d420e3e35e04eef565eef946cc4a90475e7c483b366ccd88a8f5d680201e0c1a3
6
+ metadata.gz: 6266cbf51286fe1e09b6c5a865bc6feb1bb3b61e4c29a0fb7114590795290ced5ed97506d7abb81bf94f0a426b085f3dd09e6e4a0ff8ff003c65118e05a81252
7
+ data.tar.gz: a8056d6a6c3627bfe89330a9818ccafde6fe79642c6f5b27d8d881bd410b744e3d638e5f02f7bd929ea61948f8b5851da64b894d2c56c3a34134d79a7b8ac458
@@ -1,7 +1,12 @@
1
1
  name: Unit Tests
2
2
 
3
3
  on:
4
+ push:
5
+ branches:
6
+ - master
4
7
  pull_request:
8
+ branches:
9
+ - master
5
10
 
6
11
  permissions:
7
12
  contents: read
@@ -12,7 +17,7 @@ jobs:
12
17
  runs-on: ubuntu-latest
13
18
  strategy:
14
19
  matrix:
15
- ruby-version: ['3.2', '3.1', '3.0', '2.7', '2.6']
20
+ ruby-version: ['2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4']
16
21
 
17
22
  steps:
18
23
  - name: Checkout
data/README.md CHANGED
@@ -14,6 +14,8 @@ You'll need an IPinfo API access token, which you can get by signing up for a fr
14
14
 
15
15
  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)
16
16
 
17
+ The library also supports the Lite API, see the [Lite API section](#lite-api) for more info.
18
+
17
19
  #### Installation
18
20
 
19
21
  Add this line to your application's Gemfile:
@@ -215,6 +217,23 @@ details.all = {
215
217
  }
216
218
  ```
217
219
 
220
+ ### Lite API
221
+
222
+ The library gives the possibility to use the [Lite API](https://ipinfo.io/developers/lite-api) too, authentication with your token is still required.
223
+
224
+ The returned details are slightly different from the Core API.
225
+
226
+ ```ruby
227
+ require 'ipinfo_lite'
228
+
229
+ access_token = '123456789abc'
230
+ handler = IPinfoLite::create(access_token)
231
+
232
+ details = handler.details('8.8.8.8')
233
+ details.country_code # US
234
+ details.country # United States
235
+ ```
236
+
218
237
  #### Caching
219
238
 
220
239
  In-memory caching of `details` data is provided by default via the
data/ipinfo.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.name = 'IPinfo'
10
10
  spec.version = IPinfo::VERSION
11
11
  spec.required_ruby_version = '>= 2.5.0'
12
- spec.authors = ['Stanislav K, James Timmins', 'Uman Shahzad']
13
- spec.email = ['jameshtimmins@gmail.com', 'uman@mslm.io']
12
+ spec.authors = ['IPinfo releases']
13
+ spec.email = ['releases@ipinfo.io']
14
14
 
15
15
  spec.summary = ' This is a ruby wrapper for http://ipinfo.io. '
16
16
  spec.description = ' This is a ruby wrapper for http://ipinfo.io. '
@@ -52,3 +52,42 @@ class IPinfo::Adapter
52
52
  headers
53
53
  end
54
54
  end
55
+
56
+ class IPinfo::AdapterLite
57
+ HOST = 'https://api.ipinfo.io/lite/'
58
+
59
+ attr_reader :conn
60
+
61
+ def initialize(token = nil, adapter = :net_http)
62
+ @token = token
63
+ @conn = connection(adapter)
64
+ end
65
+
66
+ def get(uri)
67
+ @conn.get(HOST + uri) do |req|
68
+ default_headers.each_pair do |key, value|
69
+ req.headers[key] = value
70
+ end
71
+ req.params['token'] = CGI.escape(token) if token
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ attr_reader :token
78
+
79
+ def connection(adapter)
80
+ Faraday.new() do |conn|
81
+ conn.adapter(adapter)
82
+ end
83
+ end
84
+
85
+ def default_headers
86
+ headers = {
87
+ 'User-Agent' => "IPinfoClient/Ruby/#{IPinfo::VERSION}",
88
+ 'Accept' => 'application/json'
89
+ }
90
+ headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token
91
+ headers
92
+ end
93
+ end
data/lib/ipinfo/mod.rb CHANGED
@@ -2,3 +2,6 @@
2
2
 
3
3
  module IPinfo
4
4
  end
5
+
6
+ module IPinfoLite
7
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IPinfo
4
- VERSION = '2.2.4'
4
+ VERSION = '2.3.0'
5
5
  end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipinfo/adapter'
4
+ require 'ipinfo/cache/default_cache'
5
+ require 'ipinfo/errors'
6
+ require 'ipinfo/response'
7
+ require_relative 'ipinfo/ipAddressMatcher'
8
+ require_relative 'ipinfo/countriesData'
9
+ require 'ipaddr'
10
+ require 'cgi'
11
+
12
+ module IPinfoLite
13
+ include CountriesData
14
+ DEFAULT_CACHE_MAXSIZE = 4096
15
+ DEFAULT_CACHE_TTL = 60 * 60 * 24
16
+ RATE_LIMIT_MESSAGE = 'To increase your limits, please review our ' \
17
+ 'paid plans at https://ipinfo.io/pricing'
18
+ # Base URL to get country flag image link.
19
+ # "PK" -> "https://cdn.ipinfo.io/static/images/countries-flags/PK.svg"
20
+ COUNTRY_FLAGS_URL = 'https://cdn.ipinfo.io/static/images/countries-flags/'
21
+
22
+ class << self
23
+ def create(access_token = nil, settings = {})
24
+ IPinfo::IPinfoLite.new(access_token, settings)
25
+ end
26
+ end
27
+ end
28
+
29
+ class IPinfo::IPinfoLite
30
+ include IPinfoLite
31
+ attr_accessor :access_token, :countries, :httpc
32
+
33
+ def initialize(access_token = nil, settings = {})
34
+ @access_token = access_token
35
+ @httpc = IPinfo::AdapterLite.new(access_token, httpc || :net_http)
36
+
37
+ maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
38
+ ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
39
+ @cache = settings.fetch('cache', IPinfo::DefaultCache.new(ttl, maxsize))
40
+ @countries = settings.fetch('countries', DEFAULT_COUNTRY_LIST)
41
+ @eu_countries = settings.fetch('eu_countries', DEFAULT_EU_COUNTRIES_LIST)
42
+ @countries_flags = settings.fetch('countries_flags', DEFAULT_COUNTRIES_FLAG_LIST)
43
+ @countries_currencies = settings.fetch('countries_currencies', DEFAULT_COUNTRIES_CURRENCIES_LIST)
44
+ @continents = settings.fetch('continents', DEFAULT_CONTINENT_LIST)
45
+ end
46
+
47
+ def details(ip_address = nil)
48
+ details_base(ip_address)
49
+ end
50
+
51
+ def request_details(ip_address = nil)
52
+ if ip_address && ip_address != 'me' && isBogon(ip_address)
53
+ details[:ip] = ip_address
54
+ details[:bogon] = true
55
+ details[:ip_address] = IPAddr.new(ip_address)
56
+
57
+ return details
58
+ end
59
+
60
+ res = @cache.get(cache_key(ip_address))
61
+ return res unless res.nil?
62
+
63
+ ip_address ||= 'me'
64
+ response = @httpc.get(escape_path(ip_address))
65
+
66
+ if response.status.eql?(429)
67
+ raise RateLimitError,
68
+ RATE_LIMIT_MESSAGE
69
+ end
70
+
71
+ details = JSON.parse(response.body, symbolize_names: true)
72
+ @cache.set(cache_key(ip_address), details)
73
+ details
74
+ end
75
+
76
+ def details_base(ip_address)
77
+ details = request_details(ip_address)
78
+ if details.key? :country_code
79
+ details[:country_name] =
80
+ @countries.fetch(details.fetch(:country_code), nil)
81
+ details[:is_eu] =
82
+ @eu_countries.include?(details.fetch(:country_code))
83
+ details[:country_flag] =
84
+ @countries_flags.fetch(details.fetch(:country_code), nil)
85
+ details[:country_currency] =
86
+ @countries_currencies.fetch(details.fetch(:country_code), nil)
87
+ details[:continent] =
88
+ @continents.fetch(details.fetch(:country_code), nil)
89
+ details[:country_flag_url] = "#{COUNTRY_FLAGS_URL}#{details.fetch(:country_code)}.svg"
90
+ end
91
+
92
+ if details.key? :ip
93
+ details[:ip_address] =
94
+ IPAddr.new(details.fetch(:ip))
95
+ end
96
+
97
+ IPinfo::Response.new(details)
98
+ end
99
+
100
+ def isBogon(ip)
101
+ if ip.nil?
102
+ return false
103
+ end
104
+
105
+ matcher_object = IPinfo::IpAddressMatcher.new(ip)
106
+ matcher_object.matches
107
+ end
108
+
109
+ def escape_path(ip)
110
+ ip ? "/#{CGI.escape(ip)}" : '/'
111
+ end
112
+
113
+ def cache_key(ip)
114
+ "1:#{ip}"
115
+ end
116
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: IPinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.4
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Stanislav K, James Timmins
8
- - Uman Shahzad
9
- autorequire:
7
+ - IPinfo releases
8
+ autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2024-09-04 00:00:00.000000000 Z
11
+ date: 2025-07-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: faraday
@@ -139,8 +138,7 @@ dependencies:
139
138
  version: '1.1'
140
139
  description: " This is a ruby wrapper for http://ipinfo.io. "
141
140
  email:
142
- - jameshtimmins@gmail.com
143
- - uman@mslm.io
141
+ - releases@ipinfo.io
144
142
  executables: []
145
143
  extensions: []
146
144
  extra_rdoc_files: []
@@ -168,10 +166,11 @@ files:
168
166
  - lib/ipinfo/mod.rb
169
167
  - lib/ipinfo/response.rb
170
168
  - lib/ipinfo/version.rb
169
+ - lib/ipinfo_lite.rb
171
170
  homepage: https://ipinfo.io
172
171
  licenses: []
173
172
  metadata: {}
174
- post_install_message:
173
+ post_install_message:
175
174
  rdoc_options: []
176
175
  require_paths:
177
176
  - lib
@@ -187,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
186
  version: '0'
188
187
  requirements: []
189
188
  rubygems_version: 3.4.10
190
- signing_key:
189
+ signing_key:
191
190
  specification_version: 4
192
191
  summary: This is a ruby wrapper for http://ipinfo.io.
193
192
  test_files: []