IPinfo 0.1.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +1 -1
- data/.github/workflows/cd_rubygems.yaml +34 -0
- data/.github/workflows/unittest.yaml +31 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +48 -0
- data/.ruby-version +1 -0
- data/Gemfile +7 -28
- data/README.md +126 -24
- data/Rakefile +8 -6
- data/bin/console +4 -10
- data/bin/setup +1 -2
- data/ipinfo.gemspec +21 -29
- data/lib/ipinfo/adapter.rb +23 -18
- data/lib/ipinfo/cache/cache_interface.rb +10 -9
- data/lib/ipinfo/cache/default_cache.rb +8 -9
- data/lib/ipinfo/continent.json +252 -0
- data/lib/ipinfo/currency.json +253 -0
- data/lib/ipinfo/errors.rb +3 -3
- data/lib/ipinfo/eu.json +1 -0
- data/lib/ipinfo/flags.json +252 -0
- data/lib/ipinfo/ipAddressMatcher.rb +92 -0
- data/lib/ipinfo/mod.rb +4 -0
- data/lib/ipinfo/response.rb +8 -9
- data/lib/ipinfo/version.rb +3 -1
- data/lib/ipinfo.rb +168 -52
- metadata +27 -24
- data/.travis.yml +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd107edec906894bab2445a4d7bc9b551fc79778c7b941a2dfa486d32c969e47
|
4
|
+
data.tar.gz: 1a67c2f0a6c7bbb57209982e1c1e92868aa218deec445826cef2f5ecb91eac5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe484b50604b7ce3dd70aa8cddc0165b0416b358933ef932d2bd93d83587ab6b4892e4e721be2ca53687cba8bc6c849cd9aab67c1aff11e69fd022d62a26faf1
|
7
|
+
data.tar.gz: b934e0bb1291651ebffe1628ae344294f4a7be1e6ba6c9354eaf9770f8c218e6209df843e2d4d891cd6bce35aee1c090c880fda2e850e0727ee87e954b2d6fd9
|
data/.editorconfig
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
name: Release package to rubygems.org
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
tags:
|
6
|
+
- 'v*'
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
publish:
|
10
|
+
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- name: Checkout
|
15
|
+
uses: actions/checkout@v3
|
16
|
+
|
17
|
+
- name: Set up Ruby
|
18
|
+
uses: ruby/setup-ruby@v1
|
19
|
+
|
20
|
+
- name: Install dependencies
|
21
|
+
run: bundle install
|
22
|
+
|
23
|
+
- name: Run tests
|
24
|
+
run: bundle exec rake
|
25
|
+
env:
|
26
|
+
IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }}
|
27
|
+
|
28
|
+
- name: Build
|
29
|
+
run: gem build *.gemspec
|
30
|
+
|
31
|
+
- name: Publish
|
32
|
+
run: gem push *.gem
|
33
|
+
env:
|
34
|
+
GEM_HOST_API_KEY: ${{ secrets.GEM_HOST_API_KEY }}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Unit Tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
|
6
|
+
permissions:
|
7
|
+
contents: read
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
run:
|
11
|
+
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby-version: ['3.1', '3.0', '2.7', '2.6']
|
16
|
+
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v3
|
19
|
+
|
20
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
21
|
+
uses: ruby/setup-ruby@359bebbc29cbe6c87da6bc9ea3bc930432750108
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby-version }}
|
24
|
+
|
25
|
+
- name: Install dependencies
|
26
|
+
run: bundle install
|
27
|
+
|
28
|
+
- name: Run tests
|
29
|
+
env:
|
30
|
+
IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }}
|
31
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Layout/IndentationWidth:
|
6
|
+
Width: 4
|
7
|
+
|
8
|
+
Layout/LineLength:
|
9
|
+
Enabled: true
|
10
|
+
Max: 80
|
11
|
+
|
12
|
+
Lint/MissingSuper:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/AbcSize:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/ClassLength:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Lint/DuplicateMethods:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/Documentation:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/ClassAndModuleChildren:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Style/MethodCallWithArgsParentheses:
|
34
|
+
EnforcedStyle: require_parentheses
|
35
|
+
IgnoreMacros: false
|
36
|
+
IgnoredPatterns: []
|
37
|
+
AllowParenthesesInMultilineCall: true
|
38
|
+
AllowParenthesesInChaining: true
|
39
|
+
AllowParenthesesInCamelCaseMethod: true
|
40
|
+
|
41
|
+
Style/MethodCallWithoutArgsParentheses:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Naming/FileName:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Naming/PredicateName:
|
48
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.2
|
data/Gemfile
CHANGED
@@ -1,34 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'https://rubygems.org'
|
2
4
|
|
3
|
-
# Specify your gem's dependencies in ipinfo.gemspec
|
4
5
|
gemspec
|
5
6
|
|
6
|
-
|
7
7
|
group :development do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
gem "rack", ">= 1.5"
|
14
|
-
else
|
15
|
-
gem "rack", ">= 1.5", "< 2.0"
|
16
|
-
end
|
17
|
-
|
18
|
-
gem "bundler"
|
19
|
-
gem "rake"
|
20
|
-
gem "minitest"
|
21
|
-
gem 'minitest-vcr'
|
22
|
-
gem 'minitest-reporters'
|
23
|
-
gem 'webmock'
|
24
|
-
|
25
|
-
platforms :mri do
|
26
|
-
# to avoid problems, bring Byebug in on just versions of Ruby under which
|
27
|
-
# it's known to work well
|
28
|
-
if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("2.0.0")
|
29
|
-
gem "byebug"
|
30
|
-
gem "pry"
|
31
|
-
gem "pry-byebug"
|
32
|
-
end
|
33
|
-
end
|
8
|
+
gem 'bundler'
|
9
|
+
gem 'minitest'
|
10
|
+
gem 'minitest-reporters'
|
11
|
+
gem 'rake'
|
12
|
+
gem 'rubocop'
|
34
13
|
end
|
data/README.md
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
# [<img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/>](https://ipinfo.io/) IPinfo Ruby Client Library
|
2
2
|
|
3
|
-
This is the official Ruby client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to
|
4
|
-
- IP geolocation (city, region, country, postal code, latitude and longitude)
|
5
|
-
- ASN
|
6
|
-
-
|
7
|
-
- Carrier details (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)
|
3
|
+
This is the official Ruby client library for the [IPinfo.io](https://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
|
+
- [IP geolocation data](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude, and longitude)
|
5
|
+
- [ASN information](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
|
6
|
+
- [Firmographic data](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)
|
7
|
+
- [Carrier details](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)
|
8
8
|
|
9
|
+
Check all the data we have for your IP address [here](https://ipinfo.io/what-is-my-ip).
|
9
10
|
|
10
11
|
### Getting Started
|
11
12
|
|
12
|
-
You'll need an IPinfo API access token, which you can get by
|
13
|
+
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).
|
13
14
|
|
14
|
-
The free plan is limited to
|
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)
|
15
16
|
|
16
17
|
#### Installation
|
17
18
|
|
18
19
|
Add this line to your application's Gemfile:
|
19
20
|
|
20
21
|
```ruby
|
21
|
-
gem '
|
22
|
+
gem 'IPinfo'
|
22
23
|
```
|
23
24
|
|
24
25
|
And then execute:
|
@@ -27,7 +28,7 @@ And then execute:
|
|
27
28
|
|
28
29
|
Or install it yourself as:
|
29
30
|
|
30
|
-
$ gem install
|
31
|
+
$ gem install IPinfo
|
31
32
|
|
32
33
|
#### Quick Start
|
33
34
|
|
@@ -43,9 +44,23 @@ city = details.city # Emeryville
|
|
43
44
|
loc = details.loc # 37.8342,-122.2900
|
44
45
|
```
|
45
46
|
|
47
|
+
##### Note about Rails 6+
|
48
|
+
|
49
|
+
If using this package in Rails 6+, the Zeitwerk auto-loader may not properly
|
50
|
+
recognize the gem due to its naming conventions (uppercased gem/module name).
|
51
|
+
See issue https://github.com/ipinfo/ruby/issues/24.
|
52
|
+
|
53
|
+
A workaround is to insert this in `application.rb`:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
require 'ipinfo' unless defined?(IPinfo)
|
57
|
+
```
|
58
|
+
|
46
59
|
#### Usage
|
47
60
|
|
48
|
-
The `IPinfo.details()` method accepts an IP address as an optional, positional
|
61
|
+
The `IPinfo.details()` method accepts an IP address as an optional, positional
|
62
|
+
argument. If no IP address is specified, the API will return data for the IP
|
63
|
+
address from which it receives the request.
|
49
64
|
|
50
65
|
```ruby
|
51
66
|
require 'ipinfo'
|
@@ -60,7 +75,9 @@ loc = details.loc # 37.8342,-122.2900
|
|
60
75
|
|
61
76
|
#### Authentication
|
62
77
|
|
63
|
-
The IPinfo library can be authenticated with your IPinfo API token, which is
|
78
|
+
The IPinfo library can be authenticated with your IPinfo API token, which is
|
79
|
+
passed in as a positional argument. It also works without an authentication
|
80
|
+
token, but in a more limited capacity.
|
64
81
|
|
65
82
|
```ruby
|
66
83
|
access_token = '123456789abc'
|
@@ -69,7 +86,9 @@ handler = IPinfo::create(access_token)
|
|
69
86
|
|
70
87
|
#### Details Data
|
71
88
|
|
72
|
-
`handler.details()` will return a `Response` object that contains all fields
|
89
|
+
`handler.details()` will return a `Response` object that contains all fields
|
90
|
+
listed in the [IPinfo developerdocs](https://ipinfo.io/developers/responses#full-response)
|
91
|
+
with a few minor additions. Properties can be accessed directly.
|
73
92
|
|
74
93
|
```ruby
|
75
94
|
hostname = details.hostname # cpe-104-175-221-247.socal.res.rr.com
|
@@ -77,16 +96,78 @@ hostname = details.hostname # cpe-104-175-221-247.socal.res.rr.com
|
|
77
96
|
|
78
97
|
##### Country Name
|
79
98
|
|
80
|
-
`details.country_name` will return the country name, as supplied by the
|
99
|
+
`details.country_name` will return the country name, as supplied by the
|
100
|
+
`countries.json` file. See below for instructions on changing that file for use
|
101
|
+
with non-English languages. `details.country` will still return the country code.
|
81
102
|
|
82
103
|
```ruby
|
83
104
|
country = details.country # US
|
84
105
|
country_name = details.country_name # United States
|
85
106
|
```
|
86
107
|
|
108
|
+
##### European Union (EU) Country
|
109
|
+
|
110
|
+
`details.is_eu` will return `true` if the country is a member of the European Union (EU), as supplied by the `eu.json` file.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
is_eu = details.is_eu # false
|
114
|
+
```
|
115
|
+
|
116
|
+
It is possible to change the file by setting the `eu_countries` setting when creating the `IPinfo` object.
|
117
|
+
|
118
|
+
The file must be a `.json` file with the [following structure](lib/ipinfo/eu.json).
|
119
|
+
|
120
|
+
##### Country Flag
|
121
|
+
|
122
|
+
`details.country_flag` will return `emoji` and `unicode` of a country's flag, as supplied by the `eu.json` file.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
country_flag = details.country_flag # {"emoji"=>"🇺🇸", "unicode"=>"U+1F1FA U+1F1F8"}
|
126
|
+
country_flag_emoji = details.country_flag['emoji'] # 🇺🇸
|
127
|
+
country_flag_unicode = details.country_flag['unicode'] # U+1F1FA U+1F1F8
|
128
|
+
```
|
129
|
+
|
130
|
+
##### Country Flag URL
|
131
|
+
|
132
|
+
`details.country_flag_url` will return a public link to the country's flag image as an SVG which can be used anywhere.
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
country_flag = details.country_flag_url # {"https://cdn.ipinfo.io/static/images/countries-flags/US.svg"}
|
136
|
+
```
|
137
|
+
|
138
|
+
##### Country Currency
|
139
|
+
|
140
|
+
`details.country_currency` will return `code` and `symbol` of a country's currency, as supplied by the `currency.json` file.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
country_currency = details.country_currency # {"code"=>"USD", "symbol"=>"$"}
|
144
|
+
country_currency_code = details.country_currency['code'] # USD
|
145
|
+
country_currency_symbol = details.country_currency['symbol'] # $
|
146
|
+
```
|
147
|
+
|
148
|
+
It is possible to change the file by setting the `countries_currencies` setting when creating the `IPinfo` object.
|
149
|
+
|
150
|
+
The file must be a `.json` file with the [following structure](lib/ipinfo/currency.json).
|
151
|
+
|
152
|
+
##### Continent
|
153
|
+
|
154
|
+
`details.continent` will return `code` and `name` of the continent, as supplied by the `continent.json` file.
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
continent = details.continent # {"code"=>"NA", "name"=>"North America"}
|
158
|
+
continent_code = details.continent['code'] # NA
|
159
|
+
continent_name = details.continent['name'] # North America
|
160
|
+
```
|
161
|
+
|
162
|
+
It is possible to change the file by setting the `continents` setting when creating the `IPinfo` object.
|
163
|
+
|
164
|
+
The file must be a `.json` file with the [following structure](lib/ipinfo/continent.json).
|
165
|
+
|
87
166
|
#### IP Address
|
88
167
|
|
89
|
-
`details.ip_address` will return the
|
168
|
+
`details.ip_address` will return the `IPAddr` object from the
|
169
|
+
[Ruby Standard Library](https://ruby-doc.org/stdlib-2.5.1/libdoc/ipaddr/rdoc/IPAddr.html).
|
170
|
+
`details.ip` will still return a string.
|
90
171
|
|
91
172
|
```ruby
|
92
173
|
ip = details.ip # 104.175.221.247
|
@@ -95,7 +176,9 @@ ip_addr = details.ip_address # <IPAddr: IPv4:104.175.221.247/255.255.255.255>
|
|
95
176
|
|
96
177
|
##### Longitude and Latitude
|
97
178
|
|
98
|
-
`details.latitude` and `details.longitude` will return latitude and longitude,
|
179
|
+
`details.latitude` and `details.longitude` will return latitude and longitude,
|
180
|
+
respectively, as strings. `details.loc` will still return a composite string of
|
181
|
+
both values.
|
99
182
|
|
100
183
|
```ruby
|
101
184
|
loc = details.loc # 34.0293,-118.3570
|
@@ -134,13 +217,22 @@ details.all = {
|
|
134
217
|
|
135
218
|
#### Caching
|
136
219
|
|
137
|
-
In-memory caching of `details` data is provided by default via the
|
220
|
+
In-memory caching of `details` data is provided by default via the
|
221
|
+
[`lru_redux`](https://github.com/SamSaffron/lru_redux) gem. This uses an LRU
|
222
|
+
(least recently used) cache with a TTL (time to live) by default. This means
|
223
|
+
that values will be cached for the specified duration; if the cache's max size
|
224
|
+
is reached, cache values will be invalidated as necessary, starting with the
|
225
|
+
oldest cached value.
|
138
226
|
|
139
227
|
##### Modifying cache options
|
140
228
|
|
141
|
-
Cache behavior can be modified by setting the `cache_options` keyword argument.
|
229
|
+
Cache behavior can be modified by setting the `cache_options` keyword argument.
|
230
|
+
`cache_options` is a dictionary in which the keys are keyword arguments
|
231
|
+
specified in the `cachetools` library. The nesting of keyword arguments is to
|
232
|
+
prevent name collisions between this library and its dependencies.
|
142
233
|
|
143
|
-
* Default maximum cache size: 4096 (multiples of 2 are recommended to increase
|
234
|
+
* Default maximum cache size: 4096 (multiples of 2 are recommended to increase
|
235
|
+
efficiency)
|
144
236
|
* Default TTL: 24 hours (in seconds)
|
145
237
|
|
146
238
|
```ruby
|
@@ -150,15 +242,22 @@ handler = IPinfo::create(token, {:ttl => 30, :maxsize => 30})
|
|
150
242
|
|
151
243
|
##### Using a different cache
|
152
244
|
|
153
|
-
It's possible to use a custom cache by creating a child class of the
|
245
|
+
It's possible to use a custom cache by creating a child class of the
|
246
|
+
[CacheInterface](https://github.com/jhtimmins/ruby/blob/master/lib/ipinfo/cache/cache_interface.rb)
|
247
|
+
class and passing this into the handler object with the `cache` keyword
|
248
|
+
argument. FYI this is known as
|
249
|
+
[the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy).
|
154
250
|
|
155
251
|
```ruby
|
156
252
|
handler = IPinfo.handler(token, {:cache => my_fancy_custom_class})
|
157
253
|
```
|
158
254
|
|
159
|
-
|
160
255
|
### Using a different HTTP library
|
161
|
-
|
256
|
+
|
257
|
+
Ruby is notorious for having lots of HTTP libraries. While `Net::HTTP` is a
|
258
|
+
reasonable default, you can set any other that
|
259
|
+
[Faradaysupports](https://github.com/lostisland/faraday/tree/29feeb92e3413d38ffc1fd3a3479bb48a0915730#faraday)
|
260
|
+
if you prefer.
|
162
261
|
|
163
262
|
```ruby
|
164
263
|
access_token = '123456789abc'
|
@@ -169,7 +268,10 @@ Don't forget to bundle the custom HTTP library as well.
|
|
169
268
|
|
170
269
|
#### Internationalization
|
171
270
|
|
172
|
-
When looking up an IP address, the response object includes a
|
271
|
+
When looking up an IP address, the response object includes a
|
272
|
+
`details.country_name` attribute which includes the country name based on
|
273
|
+
American English. It is possible to return the country name in other languages
|
274
|
+
by setting the `countries` setting when creating the `IPinfo` object.
|
173
275
|
|
174
276
|
The file must be a `.json` file with the following structure:
|
175
277
|
|
@@ -185,10 +287,10 @@ The file must be a `.json` file with the following structure:
|
|
185
287
|
|
186
288
|
### Other Libraries
|
187
289
|
|
188
|
-
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
|
290
|
+
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.
|
189
291
|
|
190
292
|
### About IPinfo
|
191
293
|
|
192
|
-
Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier and IP type data sets. Our API handles over
|
294
|
+
Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for 100,000 businesses and developers.
|
193
295
|
|
194
296
|
[![image](https://avatars3.githubusercontent.com/u/15721521?s=128&u=7bb7dde5c4991335fb234e68a30971944abc6bf3&v=4)](https://ipinfo.io/)
|
data/Rakefile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
3
5
|
|
4
6
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
10
|
end
|
9
11
|
|
10
|
-
task :
|
12
|
+
task default: :test
|
data/bin/console
CHANGED
@@ -1,14 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'ipinfo'
|
5
6
|
|
6
|
-
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
7
|
+
require 'irb'
|
14
8
|
IRB.start
|
data/bin/setup
CHANGED
data/ipinfo.gemspec
CHANGED
@@ -1,37 +1,29 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
4
6
|
require 'ipinfo/version'
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
spec.summary = %q{ The official Python library for IPinfo. IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier and IP type data sets. You can visit our developer docs at https://ipinfo.io/developers. }
|
14
|
-
spec.description = %q{ The official Python library for IPinfo. IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier and IP type data sets. You can visit our developer docs at https://ipinfo.io/developers. }
|
15
|
-
spec.homepage = "https://ipinfo.io"
|
16
|
-
spec.license = "Apache-2.0"
|
9
|
+
spec.name = 'IPinfo'
|
10
|
+
spec.version = IPinfo::VERSION
|
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']
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
#spec.metadata['allowed_push_host'] = "http://mygemserver.com'"
|
22
|
-
else
|
23
|
-
raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
-
"public gem pushes."
|
25
|
-
end
|
15
|
+
spec.summary = ' This is a ruby wrapper for http://ipinfo.io. '
|
16
|
+
spec.description = ' This is a ruby wrapper for http://ipinfo.io. '
|
17
|
+
spec.homepage = 'https://ipinfo.io'
|
26
18
|
|
27
|
-
|
28
|
-
|
29
|
-
|
19
|
+
spec.add_runtime_dependency 'faraday', '~> 1.0'
|
20
|
+
spec.add_runtime_dependency 'json', '~> 2.1'
|
21
|
+
spec.add_runtime_dependency 'lru_redux', '~> 1.1'
|
30
22
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
f.match(%r{^(test|spec|features)/})
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
37
29
|
end
|
data/lib/ipinfo/adapter.rb
CHANGED
@@ -1,25 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'faraday'
|
3
4
|
require 'cgi'
|
5
|
+
require 'ipinfo/mod'
|
4
6
|
|
5
|
-
|
6
|
-
class Adapter
|
7
|
+
class IPinfo::Adapter
|
7
8
|
HOST = 'ipinfo.io'
|
8
9
|
|
9
10
|
attr_reader :conn
|
10
11
|
|
11
12
|
def initialize(token = nil, adapter = :net_http)
|
12
|
-
|
13
|
-
|
13
|
+
@token = token
|
14
|
+
@conn = connection(adapter)
|
14
15
|
end
|
15
16
|
|
16
17
|
def get(uri)
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
@conn.get(uri) do |req|
|
19
|
+
default_headers.each_pair do |key, value|
|
20
|
+
req.headers[key] = value
|
21
|
+
end
|
22
|
+
req.params['token'] = CGI.escape(token) if token
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def post(uri, body, timeout = 2)
|
27
|
+
@conn.post(uri) do |req|
|
28
|
+
req.body = body
|
29
|
+
req.options.timeout = timeout
|
20
30
|
end
|
21
|
-
req.params['token'] = CGI::escape(token) if token
|
22
|
-
end
|
23
31
|
end
|
24
32
|
|
25
33
|
private
|
@@ -27,20 +35,17 @@ module IPinfo
|
|
27
35
|
attr_reader :token
|
28
36
|
|
29
37
|
def connection(adapter)
|
30
|
-
|
31
|
-
|
32
|
-
|
38
|
+
Faraday.new(url: "https://#{HOST}") do |conn|
|
39
|
+
conn.adapter(adapter)
|
40
|
+
end
|
33
41
|
end
|
34
42
|
|
35
43
|
def default_headers
|
36
44
|
headers = {
|
37
|
-
|
38
|
-
|
45
|
+
'User-Agent' => 'IPinfoClient/Ruby/1.1.0',
|
46
|
+
'Accept' => 'application/json'
|
39
47
|
}
|
40
|
-
if token
|
41
|
-
headers['Authorization'] = "Bearer #{CGI::escape(token)}"
|
42
|
-
end
|
48
|
+
headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token
|
43
49
|
headers
|
44
50
|
end
|
45
|
-
end
|
46
51
|
end
|
@@ -1,16 +1,17 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class IPinfo::CacheInterface
|
3
4
|
class InterfaceNotImplemented < StandardError; end
|
4
|
-
|
5
|
-
|
5
|
+
|
6
|
+
def get(_key)
|
7
|
+
raise InterfaceNotImplemented
|
6
8
|
end
|
7
9
|
|
8
|
-
def set(
|
9
|
-
|
10
|
+
def set(_key, _value)
|
11
|
+
raise InterfaceNotImplemented
|
10
12
|
end
|
11
13
|
|
12
|
-
def contains?(
|
13
|
-
|
14
|
+
def contains?(_key)
|
15
|
+
raise InterfaceNotImplemented
|
14
16
|
end
|
15
|
-
end
|
16
17
|
end
|