ipinfo-rails 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 282cc121cb426e66103ab41be4e297631aefac73e0323f240437f66976231c78
4
- data.tar.gz: 6e63c66fbe6553921543b8f9b3fa8a5b4b79144376b83365a8058562cab774ca
3
+ metadata.gz: e7459fc2eefdb3cfbf3c24eab9deaf2a4ae6068c2851465d66d77febfc986ef7
4
+ data.tar.gz: 3295dcfb043cd87177f7766a37dee4255f0cab38f5fb73acf2bbf4184cb7275d
5
5
  SHA512:
6
- metadata.gz: 64f9ab7ba952f5db0723e4a01e6ce67e56bb1773fbd71c9ad240047938652bff9cb5a60e655552bed0550e4f7156a9208d228bca62137d1a09c94411ee2595e2
7
- data.tar.gz: 69f012908250b7e7f3dac85825745a517eeff6120594c925d362f5bfab37d44e002cf93f6b66d7d6e6e917d8d79a3099f9b8c8d5d4030522602a373a86dcdbf0
6
+ metadata.gz: cc2299aaa1715628fbe5151e937199fc0a8c5d2385a02e8ebc0b7ea6ee8cfe736b1a3e5d5da6e9abac77c35c612a5a4096440eed7e22b9c2a09332d198443c12
7
+ data.tar.gz: 4398d856c1d8ddc8f9a1ad556246df6cdc3a64ad1164212a7753cafd95c1e4c599d944d64d80423ebe601811d4ce38807ebfe7e99c1b5ec24d812a837fb515e7
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ ipinfo-rails-*.gem
@@ -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
@@ -0,0 +1 @@
1
+ 2.7.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ group :development do
8
+ gem 'bundler'
9
+ gem 'minitest'
10
+ gem 'minitest-reporters'
11
+ gem 'rubocop'
12
+ end
@@ -0,0 +1,228 @@
1
+ # <img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/> IPinfo Rails Client Library
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:
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)
7
+ - [Company](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)
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
+
10
+ Check all the data we have for your IP address [here](https://ipinfo.io/what-is-my-ip).
11
+
12
+ ## Getting Started
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).
15
+
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
+
18
+ ### Installation
19
+
20
+ 1. Option 1) Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'ipinfo-rails'
24
+ ```
25
+
26
+ Then execute:
27
+
28
+ ```bash
29
+ $ bundle install
30
+ ```
31
+
32
+ Option 2) Install it yourself by running the following command:
33
+
34
+ ```bash
35
+ $ gem install ipinfo-rails
36
+ ```
37
+
38
+ 1. Open your `config/environment.rb` file or your preferred file in the `config/environment` directory. Add the following code to your chosen configuration file.
39
+
40
+ ```ruby
41
+ require 'ipinfo-rails'
42
+ config.middleware.use(IPinfoMiddleware, {token: "<your_token>"})
43
+ ```
44
+
45
+ Note: if editing `config/environment.rb`, this needs to come before `Rails.application.initialize!` and with `Rails.application.` prepended to `config`, otherwise you'll get runtime errors.
46
+
47
+ 1. Restart your development server.
48
+
49
+ ### Quickstart
50
+
51
+ Once configured, `ipinfo-rails` will make IP address data accessible within Rail's `request` object. These values can be accessed at `request.env['ipinfo']`.
52
+
53
+ ## Details Data
54
+
55
+ `request.env['ipinfo']` is `Response` object that contains all fields listed [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed through methods of the same name.
56
+
57
+ ```ruby
58
+ request.env['ipinfo'].hostname == 'cpe-104-175-221-247.socal.res.rr.com'
59
+ ```
60
+
61
+ ### Country Name
62
+
63
+ `request.env['ipinfo'].country_name` will return the country name, as supplied by the `countries.json` file. See below for instructions on changing that file for use with non-English languages. `request.env['ipinfo'].country` will still return country code.
64
+
65
+ ```ruby
66
+ request.env['ipinfo'].country == 'US'
67
+ request.env['ipinfo'].country_name == 'United States'
68
+ ```
69
+
70
+ ### IP Address
71
+
72
+ `request.env['ipinfo'].ip_address` will return the an `IPAddr` object from the [Ruby Standard Library](https://ruby-doc.org/stdlib-2.5.1/libdoc/ipaddr/rdoc/IPAddr.html). `request.env['ipinfo'].ip` will still return a string.
73
+
74
+ ```ruby
75
+ request.env['ipinfo'].ip == '104.175.221.247'
76
+ request.env['ipinfo'].ip_address == <IPAddr: IPv4:104.175.221.247/255.255.255.255>
77
+ ```
78
+
79
+ ### Longitude and Latitude
80
+
81
+ `request.env['ipinfo'].latitude` and `request.env['ipinfo'].longitude` will return latitude and longitude, respectively, as strings. `request.env['ipinfo'].loc` will still return a composite string of both values.
82
+
83
+ ```ruby
84
+ request.env['ipinfo'].loc == '34.0293,-118.3570'
85
+ request.env['ipinfo'].latitude == '34.0293'
86
+ request.env['ipinfo'].longitude == '-118.3570'
87
+ ```
88
+
89
+ ### Accessing all properties
90
+
91
+ `request.env['ipinfo'].all` will return all details data as a hash.
92
+
93
+ ```ruby
94
+ request.env['ipinfo'].all ==
95
+ {
96
+ :asn => { :asn => 'AS20001',
97
+ :domain => 'twcable.com',
98
+ :name => 'Time Warner Cable Internet LLC',
99
+ :route => '104.172.0.0/14',
100
+ :type => 'isp'},
101
+ :city => 'Los Angeles',
102
+ :company => { :domain => 'twcable.com',
103
+ :name => 'Time Warner Cable Internet LLC',
104
+ :type => 'isp'},
105
+ :country => 'US',
106
+ :country_name => 'United States',
107
+ :hostname => 'cpe-104-175-221-247.socal.res.rr.com',
108
+ :ip => '104.175.221.247',
109
+ :ip_address => <IPAddr: IPv4:104.175.221.247/255.255.255.255>,
110
+ :loc => '34.0293,-118.3570',
111
+ :latitude => '34.0293',
112
+ :longitude => '-118.3570',
113
+ :phone => '323',
114
+ :postal => '90016',
115
+ :region => 'California'
116
+ }
117
+ ```
118
+
119
+ ## Configuration
120
+
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.
122
+
123
+ ```ruby
124
+ config.middleware.use(IPinfoMiddleware, {
125
+ token: "<your_token>",
126
+ ttl: "",
127
+ maxsize: "",
128
+ cache: "",
129
+ http_client: "",
130
+ countries: "",
131
+ filter: "",
132
+ })
133
+ ```
134
+
135
+ ### Authentication
136
+
137
+ The IPinfo library can be authenticated with your IPinfo API token, which is set in the environment file. It also works without an authentication token, but in a more limited capacity.
138
+
139
+ ```ruby
140
+ config.middleware.use(IPinfoMiddleware, {token: '123456789abc'})
141
+ ```
142
+
143
+ ### Caching
144
+
145
+ In-memory caching of `details` data is provided by default via the [lrucache](https://www.rubydoc.info/gems/lrucache/0.1.4/LRUCache) gem. This uses an LRU (least recently used) cache with a TTL (time to live) by default. This means that values will be cached for the specified duration; if the cache's max size is reached, cache values will be invalidated as necessary, starting with the oldest cached value.
146
+
147
+ #### Modifying cache options
148
+
149
+ Cache behavior can be modified by setting the `ttl` and `maxsize` options.
150
+
151
+ - Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)
152
+ - Default TTL: 24 hours (in seconds)
153
+
154
+ ```ruby
155
+ config.middleware.use(IPinfoMiddleware, {
156
+ ttl: 30,
157
+ maxsize: 40
158
+ })
159
+ ```
160
+
161
+ #### Using a different cache
162
+
163
+ It's possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/ipinfo/ruby/blob/master/lib/ipinfo/cache/cache_interface.rb) class and passing this into the handler object with the `cache` keyword argument. FYI this is known as [the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy).
164
+
165
+ ```ruby
166
+ config.middleware.use(IPinfoMiddleware, {:cache => my_fancy_custom_class})
167
+ ```
168
+
169
+ If a custom cache is used the `maxsize` and `ttl` settings will not be used.
170
+
171
+ ### Using a different HTTP library
172
+
173
+ Ruby is notorious for having lots of HTTP libraries. While `Net::HTTP` is a reasonable default, you can set any other that [Faraday supports](https://github.com/lostisland/faraday/tree/29feeb92e3413d38ffc1fd3a3479bb48a0915730#faraday) if you prefer.
174
+
175
+ ```ruby
176
+ config.middleware.use(IPinfoMiddleware, {:http_client => my_client})
177
+ ```
178
+
179
+ Don't forget to bundle the custom HTTP library as well.
180
+
181
+ ### Internationalization
182
+
183
+ When looking up an IP address, the response object includes a `Details.country_name` method which includes the country name based on American English. It is possible to return the country name in other languages by setting the countries setting when creating the IPinfo object.
184
+
185
+ The file must be a `.json` file with the following structure:
186
+
187
+ ```ruby
188
+ {
189
+ "BD": "Bangladesh",
190
+ "BE": "Belgium",
191
+ "BF": "Burkina Faso",
192
+ "BG": "Bulgaria"
193
+ ...
194
+ }
195
+ ```
196
+
197
+ ```ruby
198
+ config.middleware.use(IPinfoMiddleware, {:countries => <path_to_settings_file>})
199
+ ```
200
+
201
+ ### Filtering
202
+
203
+ By default, `ipinfo-rails` filters out requests that have `bot` or `spider` in the user-agent. Instead of looking up IP address data for these requests, the `request.env['ipinfo']` attribute is set to `nil`. This is to prevent you from unnecessarily using up requests on non-user traffic.
204
+
205
+ To set your own filtering rules, *thereby replacing the default filter*, you can set `:filter` to your own, custom callable function which satisfies the following rules:
206
+
207
+ - Accepts one request.
208
+ - Returns *True to filter out, False to allow lookup*
209
+
210
+ To use your own filter rules:
211
+
212
+ ```ruby
213
+ config.middleware.use(IPinfoMiddleware, {
214
+ filter: ->(request) {request.ip == '127.0.0.1'}
215
+ })
216
+ ```
217
+
218
+ This simple lambda function will filter out requests coming from your local computer.
219
+
220
+ ## Other Libraries
221
+
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.
223
+
224
+ ## About IPinfo
225
+
226
+ 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, privacy detection (VPN, proxy, Tor), hosted domains, and IP type data sets. Our API handles over 20 billion requests a month for 100,000 businesses and developers.
227
+
228
+ ![image](https://avatars3.githubusercontent.com/u/15721521?s=128&u=7bb7dde5c4991335fb234e68a30971944abc6bf3&v=4)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'ipinfo-rails/version'
7
+
8
+ Gem::Specification.new do |s|
9
+ s.name = 'ipinfo-rails'
10
+ s.version = IPinfoRails::VERSION
11
+ s.required_ruby_version = '>= 2.5.0'
12
+ s.date = '2018-12-10'
13
+ s.summary = 'The official Rails gem for IPinfo. IPinfo prides itself on ' \
14
+ 'being the most reliable, accurate, and in-depth source of ' \
15
+ 'IP address data available anywhere. We process terabytes ' \
16
+ 'of data to produce our custom IP geolocation, company, ' \
17
+ 'carrier and IP type data sets. You can visit our developer ' \
18
+ 'docs at https://ipinfo.io/developers.'
19
+ s.description = s.summary
20
+ s.authors = ['James Timmins', 'Uman Shahzad']
21
+ s.email = ['jameshtimmins@gmail.com', 'uman@mslm.io']
22
+ s.homepage = 'https://ipinfo.io'
23
+ s.license = 'Apache-2.0'
24
+
25
+ s.add_runtime_dependency 'IPinfo', '~> 1.0.1'
26
+ s.add_runtime_dependency 'rack', '~> 2.0'
27
+
28
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ s.require_paths = ['lib']
32
+ end
@@ -1,38 +1,40 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack'
2
4
  require 'ipinfo'
3
5
 
4
6
  class IPinfoMiddleware
5
- def initialize(app, cache_options = {})
6
- @app = app
7
-
8
- token = cache_options.fetch(:token, nil)
9
- @ipinfo = IPinfo::create(@token, cache_options)
10
- @filter = cache_options.fetch(:filter, nil)
11
- end
12
-
13
- def call(env)
14
- env["called"] = "yes"
15
- request = Rack::Request.new(env)
16
-
17
- if !@filter.nil?
18
- filtered = @filter.call(request)
19
- else
20
- filtered = is_bot(request)
7
+ def initialize(app, cache_options = {})
8
+ @app = app
9
+ @token = cache_options.fetch(:token, nil)
10
+ @ipinfo = IPinfo.create(@token, cache_options)
11
+ @filter = cache_options.fetch(:filter, nil)
21
12
  end
22
13
 
23
- if filtered
24
- env["ipinfo"] = nil
25
- else
26
- ip = request.ip
27
- env["ipinfo"] = @ipinfo.details(ip)
14
+ def call(env)
15
+ env['called'] = 'yes'
16
+ request = Rack::Request.new(env)
17
+
18
+ filtered = if @filter.nil?
19
+ is_bot(request)
20
+ else
21
+ @filter.call(request)
22
+ end
23
+
24
+ if filtered
25
+ env['ipinfo'] = nil
26
+ else
27
+ ip = request.ip
28
+ env['ipinfo'] = @ipinfo.details(ip)
29
+ end
30
+
31
+ @app.call(env)
28
32
  end
29
33
 
30
- @app.call(env)
31
- end
34
+ private
32
35
 
33
- private
34
36
  def is_bot(request)
35
- user_agent = request.user_agent.downcase
36
- user_agent.include?("bot") || user_agent.include?("spider")
37
+ user_agent = request.user_agent.downcase
38
+ user_agent.include?('bot') || user_agent.include?('spider')
37
39
  end
38
40
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IPinfoRails
4
+ VERSION = '1.0.0'
5
+ end
metadata CHANGED
@@ -1,10 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipinfo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Timmins
8
+ - Uman Shahzad
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
@@ -16,14 +17,14 @@ dependencies:
16
17
  requirements:
17
18
  - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: 0.1.2
20
+ version: 1.0.1
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: 0.1.2
27
+ version: 1.0.1
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: rack
29
30
  requirement: !ruby/object:Gem::Requirement
@@ -42,12 +43,21 @@ description: The official Rails gem for IPinfo. IPinfo prides itself on being th
42
43
  most reliable, accurate, and in-depth source of IP address data available anywhere.
43
44
  We process terabytes of data to produce our custom IP geolocation, company, carrier
44
45
  and IP type data sets. You can visit our developer docs at https://ipinfo.io/developers.
45
- email: jameshtimmins@gmail.com
46
+ email:
47
+ - jameshtimmins@gmail.com
48
+ - uman@mslm.io
46
49
  executables: []
47
50
  extensions: []
48
51
  extra_rdoc_files: []
49
52
  files:
53
+ - ".gitignore"
54
+ - ".rubocop.yml"
55
+ - ".ruby-version"
56
+ - Gemfile
57
+ - README.md
58
+ - ipinfo-rails.gemspec
50
59
  - lib/ipinfo-rails.rb
60
+ - lib/ipinfo-rails/version.rb
51
61
  homepage: https://ipinfo.io
52
62
  licenses:
53
63
  - Apache-2.0
@@ -60,15 +70,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
70
  requirements:
61
71
  - - ">="
62
72
  - !ruby/object:Gem::Version
63
- version: '0'
73
+ version: 2.5.0
64
74
  required_rubygems_version: !ruby/object:Gem::Requirement
65
75
  requirements:
66
76
  - - ">="
67
77
  - !ruby/object:Gem::Version
68
78
  version: '0'
69
79
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.7.8
80
+ rubygems_version: 3.1.4
72
81
  signing_key:
73
82
  specification_version: 4
74
83
  summary: The official Rails gem for IPinfo. IPinfo prides itself on being the most