IPinfo 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8df58dc0d3caeada335bcd3a46e35ea5d533a3c47d8226817441d0395a0513a
4
+ data.tar.gz: 4cb9dd3927bbe7e7a88cc918cd08306705042cbf7971ba83bf33d0ad3f7d84fd
5
+ SHA512:
6
+ metadata.gz: f892448f0a5fa74f86de90763f9abeccbe97fccc12213e4aca94cc53f735ad1295b171f79cf94f259253ad782fdbf5b643c9dba082a35ebd5fc38d746647b699
7
+ data.tar.gz: bef7befefa3fffc171fbf443847e40f8a49b93ff9c5f712deed8ca8d2e28708250a28e4e44f78a08302bc93f60809d45e5d34265d6f6975b117074a8e814a57e
@@ -0,0 +1,13 @@
1
+ # top-most EditorConfig file
2
+ root = true
3
+
4
+ # Unix-style newlines with a newline ending every file
5
+ # Two spaces for indenting
6
+ [*]
7
+ end_of_line = lf
8
+ insert_final_newline = true
9
+ indent_style = space
10
+ indent_size = 2
11
+ charset = utf-8
12
+ trim_trailing_whitespace = true
13
+ max_line_length = 120
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.0
7
+ - 2.1
8
+ - 2.2
9
+ - 2.3
10
+ - 2.4
11
+
12
+ before_install:
13
+ - gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,34 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ipinfo.gemspec
4
+ gemspec
5
+
6
+
7
+ group :development do
8
+ # Rack 2.0+ requires Ruby >= 2.2.2 which is problematic for the test suite on
9
+ # older Ruby versions. Check Ruby the version here and put a maximum
10
+ # constraint on Rack if necessary.
11
+
12
+ if RUBY_VERSION >= "2.2.2"
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
34
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,194 @@
1
+ # [<img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/>](https://ipinfo.io/) IPinfo Ruby Client Library
2
+
3
+ This is the official Ruby client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to lookup your own IP address, or get any of the following details for an IP:
4
+ - IP geolocation (city, region, country, postal code, latitude and longitude)
5
+ - ASN details (ISP or network operator, associated domain name, and type, such as business, hosting or company)
6
+ - Company details (the name and domain of the business that uses the IP address)
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)
8
+
9
+
10
+ ### Getting Started
11
+
12
+ 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?ref=lib-Ruby).
13
+
14
+ The free plan is limited to 1,000 requests a day, 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?ref=lib-Ruby)
15
+
16
+ #### Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'ipinfo'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle install
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install ipinfo
31
+
32
+ #### Quick Start
33
+
34
+ ```ruby
35
+ require 'ipinfo'
36
+
37
+ access_token = '123456789abc'
38
+ handler = IPinfo::create(access_token)
39
+ ip_address = '216.239.36.21'
40
+
41
+ details = handler.details(ip_address)
42
+ city = details.city # Emeryville
43
+ loc = details.loc # 37.8342,-122.2900
44
+ ```
45
+
46
+ #### Usage
47
+
48
+ The `IPinfo.details()` method accepts an IP address as an optional, positional argument. If no IP address is specified, the API will return data for the IP address from which it receives the request.
49
+
50
+ ```ruby
51
+ require 'ipinfo'
52
+
53
+ access_token = '123456789abc'
54
+ handler = IPinfo::create(access_token)
55
+
56
+ details = handler.details()
57
+ city = details.city # "Emeryville"
58
+ loc = details.loc # 37.8342,-122.2900
59
+ ```
60
+
61
+ #### Authentication
62
+
63
+ The IPinfo library can be authenticated with your IPinfo API token, which is passed in as a positional argument. It also works without an authentication token, but in a more limited capacity.
64
+
65
+ ```ruby
66
+ access_token = '123456789abc'
67
+ handler = IPinfo::create(access_token)
68
+ ```
69
+
70
+ #### Details Data
71
+
72
+ `handler.details()` will return a `Response` object that contains all fields listed in the [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed directly.
73
+
74
+ ```ruby
75
+ hostname = details.hostname # cpe-104-175-221-247.socal.res.rr.com
76
+ ```
77
+
78
+ ##### Country Name
79
+
80
+ `details.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. `details.country` will still return country code.
81
+
82
+ ```ruby
83
+ country = details.country # US
84
+ country_name = details.country_name # United States
85
+ ```
86
+
87
+ #### IP Address
88
+
89
+ `details.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). `details.ip` will still return a string.
90
+
91
+ ```ruby
92
+ ip = details.ip # 104.175.221.247
93
+ ip_addr = details.ip_address # <IPAddr: IPv4:104.175.221.247/255.255.255.255>
94
+ ```
95
+
96
+ ##### Longitude and Latitude
97
+
98
+ `details.latitude` and `details.longitude` will return latitude and longitude, respectively, as strings. `details.loc` will still return a composite string of both values.
99
+
100
+ ```ruby
101
+ loc = details.loc # 34.0293,-118.3570
102
+ lat = details.latitude # 34.0293
103
+ lon = details.longitude # -118.3570
104
+ ```
105
+
106
+ ##### Accessing all properties
107
+
108
+ `details.all` will return all details data as a dictionary.
109
+
110
+ ```ruby
111
+ details.all = {
112
+ :asn => { :asn => 'AS20001',
113
+ :domain => 'twcable.com',
114
+ :name => 'Time Warner Cable Internet LLC',
115
+ :route => '104.172.0.0/14',
116
+ :type => 'isp'},
117
+ :city => 'Los Angeles',
118
+ :company => { :domain => 'twcable.com',
119
+ :name => 'Time Warner Cable Internet LLC',
120
+ :type => 'isp'},
121
+ :country => 'US',
122
+ :country_name => 'United States',
123
+ :hostname => 'cpe-104-175-221-247.socal.res.rr.com',
124
+ :ip => '104.175.221.247',
125
+ :ip_address => <IPAddr: IPv4:104.175.221.247/255.255.255.255>,
126
+ :loc => '34.0293,-118.3570',
127
+ :latitude => '34.0293',
128
+ :longitude => '-118.3570',
129
+ :phone => '323',
130
+ :postal => '90016',
131
+ :region => 'California'
132
+ }
133
+ ```
134
+
135
+ #### Caching
136
+
137
+ 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.
138
+
139
+ ##### Modifying cache options
140
+
141
+ Cache behavior can be modified by setting the `cache_options` keyword argument. `cache_options` is a dictionary in which the keys are keyword arguments specified in the `cachetools` library. The nesting of keyword arguments is to prevent name collisions between this library and its dependencies.
142
+
143
+ * Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)
144
+ * Default TTL: 24 hours (in seconds)
145
+
146
+ ```ruby
147
+ token = '1234'
148
+ handler = IPinfo::create(token, {:ttl => 30, :maxsize => 30})
149
+ ```
150
+
151
+ ##### Using a different cache
152
+
153
+ It's possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/jhtimmins/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).
154
+
155
+ ```ruby
156
+ handler = IPinfo.handler(token, {:cache => my_fancy_custom_class})
157
+ ```
158
+
159
+
160
+ ### Using a different HTTP library
161
+ 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.
162
+
163
+ ```ruby
164
+ access_token = '123456789abc'
165
+ handler = IPinfo::create(access_token, {:http_client => my_client})
166
+ ```
167
+
168
+ Don't forget to bundle the custom HTTP library as well.
169
+
170
+ #### Internationalization
171
+
172
+ When looking up an IP address, the response object includes a `details.country_name` attribute 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.
173
+
174
+ The file must be a `.json` file with the following structure:
175
+
176
+ ```
177
+ {
178
+ "BD": "Bangladesh",
179
+ "BE": "Belgium",
180
+ "BF": "Burkina Faso",
181
+ "BG": "Bulgaria"
182
+ ...
183
+ }
184
+ ```
185
+
186
+ ### Other Libraries
187
+
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 party libraries and integrations available for our API.
189
+
190
+ ### About IPinfo
191
+
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 12 billion requests a month for 100,000 businesses and developers.
193
+
194
+ [![image](https://avatars3.githubusercontent.com/u/15721521?s=128&u=7bb7dde5c4991335fb234e68a30971944abc6bf3&v=4)](https://ipinfo.io/)
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ipinfo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
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"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ipinfo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "IPinfo"
8
+ spec.version = IPinfo::VERSION
9
+ spec.required_ruby_version = ">= 2.0.0"
10
+ spec.authors = ["Stanislav K, James Timmins"]
11
+ spec.email = ["jameshtimmins@gmail.com"]
12
+
13
+ spec.summary = %q{ This is a ruby wrapper for http://ipinfo.io. }
14
+ spec.description = %q{ This is a ruby wrapper for http://ipinfo.io. }
15
+ spec.homepage = "https://ipinfo.io"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ #spec.metadata['allowed_push_host'] = "http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.add_runtime_dependency 'faraday', '~> 0'
27
+ spec.add_runtime_dependency 'lrucache', '~> 0.1.4'
28
+ spec.add_runtime_dependency 'json', '~> 2.1'
29
+
30
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
31
+ f.match(%r{^(test|spec|features)/})
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi'
4
+ require 'ipaddr'
5
+ require 'ipinfo/adapter'
6
+ require 'ipinfo/cache/default_cache'
7
+ require 'ipinfo/errors'
8
+ require 'ipinfo/response'
9
+ require 'ipinfo/version'
10
+ require 'json'
11
+
12
+ module IPinfo
13
+ DEFAULT_CACHE_MAXSIZE = 4096
14
+ DEFAULT_CACHE_TTL = 60 * 60 * 24
15
+ DEFAULT_COUNTRY_FILE = File.join(File.dirname(__FILE__), 'ipinfo/countries.json')
16
+ RATE_LIMIT_MESSAGE = "To increase your limits, please review our paid plans at https://ipinfo.io/pricing"
17
+
18
+ class << self
19
+ def create(access_token=nil, settings={})
20
+ IPinfo.new(access_token, settings)
21
+ end
22
+ end
23
+
24
+ class IPinfo
25
+ attr_accessor :access_token
26
+ attr_accessor :countries
27
+ attr_accessor :http_client
28
+
29
+ def initialize(access_token=nil, settings={})
30
+ @access_token = access_token
31
+ @http_client = http_client(settings.fetch("http_client", nil))
32
+
33
+ maxsize = settings.fetch("maxsize", DEFAULT_CACHE_MAXSIZE)
34
+ ttl = settings.fetch("ttl", DEFAULT_CACHE_TTL)
35
+ @cache = settings.fetch("cache", DefaultCache.new(ttl, maxsize))
36
+ @countries = countries(settings.fetch('countries', DEFAULT_COUNTRY_FILE))
37
+ end
38
+
39
+ def details(ip_address=nil)
40
+ details = request_details(ip_address)
41
+ if details.has_key? :country
42
+ details[:country_name] = @countries.fetch(details.fetch(:country), nil)
43
+ end
44
+
45
+ if details.has_key? :ip
46
+ details[:ip_address] = IPAddr.new(details.fetch(:ip))
47
+ end
48
+
49
+ if details.has_key? :loc
50
+ loc = details.fetch(:loc).split(",")
51
+ details[:latitude] = loc[0]
52
+ details[:longitude] = loc[1]
53
+ end
54
+
55
+ Response.new(details)
56
+ end
57
+
58
+ protected
59
+ def request_details(ip_address=nil)
60
+ if !@cache.contains?(ip_address)
61
+ response = @http_client.get(escape_path(ip_address))
62
+
63
+ raise RateLimitError.new(RATE_LIMIT_MESSAGE) if response.status.eql?(429)
64
+
65
+ details = JSON.parse(response.body, symbolize_names: true)
66
+ @cache.set(ip_address, details)
67
+ end
68
+ @cache.get(ip_address)
69
+ end
70
+
71
+ def http_client(http_client=nil)
72
+ if http_client
73
+ @http_client = Adapter.new(access_token, http_client)
74
+ else
75
+ @http_client = Adapter.new(access_token)
76
+ end
77
+ end
78
+
79
+ def countries(filename)
80
+ file = File.read(filename)
81
+ JSON.parse(file)
82
+ end
83
+
84
+ private
85
+ def escape_path(ip)
86
+ ip ? "/#{CGI::escape(ip)}" : '/'
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ require 'faraday'
3
+ require 'cgi'
4
+
5
+ module IPinfo
6
+ class Adapter
7
+ HOST = 'ipinfo.io'
8
+
9
+ attr_reader :conn
10
+
11
+ def initialize(token = nil, adapter = :net_http)
12
+ @token = token
13
+ @conn = connection(adapter)
14
+ end
15
+
16
+ def get(uri)
17
+ @conn.get(uri) do |req|
18
+ default_headers.each_pair do |key, value|
19
+ req.headers[key] = value
20
+ end
21
+ req.params['token'] = CGI::escape(token) if token
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :token
28
+
29
+ def connection(adapter)
30
+ Faraday.new(url: "https://#{HOST}") do |faraday|
31
+ faraday.adapter adapter
32
+ end
33
+ end
34
+
35
+ def default_headers
36
+ headers = {
37
+ 'User-Agent' => "IPinfoClient/Ruby/1.0",
38
+ 'Accept' => 'application/json'
39
+ }
40
+ if token
41
+ headers['Authorization'] = "Bearer #{CGI::escape(token)}"
42
+ end
43
+ headers
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ module IPinfo
2
+ class CacheInterface
3
+ class InterfaceNotImplemented < StandardError; end
4
+ def get(key)
5
+ raise InterfaceNotImplemented
6
+ end
7
+
8
+ def set(key, value)
9
+ raise InterfaceNotImplemented
10
+ end
11
+
12
+ def contains?(key)
13
+ raise InterfaceNotImplemented
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'ipinfo/cache/cache_interface'
2
+ require 'lrucache'
3
+
4
+ module IPinfo
5
+ class DefaultCache < CacheInterface
6
+
7
+ def initialize(ttl, max_size)
8
+ @cache = LRUCache.new(:ttl => ttl, :max_size => max_size)
9
+ end
10
+
11
+ def get(key)
12
+ @cache[key]
13
+ end
14
+
15
+ def set(key, value)
16
+ @cache[key] = value
17
+ end
18
+
19
+ def contains?(key)
20
+ !@cache[key].nil?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ {"BD": "Bangladesh", "BE": "Belgium", "BF": "Burkina Faso", "BG": "Bulgaria", "BA": "Bosnia and Herzegovina", "BB": "Barbados", "WF": "Wallis and Futuna", "BL": "Saint Barthelemy", "BM": "Bermuda", "BN": "Brunei", "BO": "Bolivia", "BH": "Bahrain", "BI": "Burundi", "BJ": "Benin", "BT": "Bhutan", "JM": "Jamaica", "BV": "Bouvet Island", "BW": "Botswana", "WS": "Samoa", "BQ": "Bonaire, Saint Eustatius and Saba ", "BR": "Brazil", "BS": "Bahamas", "JE": "Jersey", "BY": "Belarus", "BZ": "Belize", "RU": "Russia", "RW": "Rwanda", "RS": "Serbia", "TL": "East Timor", "RE": "Reunion", "TM": "Turkmenistan", "TJ": "Tajikistan", "RO": "Romania", "TK": "Tokelau", "GW": "Guinea-Bissau", "GU": "Guam", "GT": "Guatemala", "GS": "South Georgia and the South Sandwich Islands", "GR": "Greece", "GQ": "Equatorial Guinea", "GP": "Guadeloupe", "JP": "Japan", "GY": "Guyana", "GG": "Guernsey", "GF": "French Guiana", "GE": "Georgia", "GD": "Grenada", "GB": "United Kingdom", "GA": "Gabon", "SV": "El Salvador", "GN": "Guinea", "GM": "Gambia", "GL": "Greenland", "GI": "Gibraltar", "GH": "Ghana", "OM": "Oman", "TN": "Tunisia", "JO": "Jordan", "HR": "Croatia", "HT": "Haiti", "HU": "Hungary", "HK": "Hong Kong", "HN": "Honduras", "HM": "Heard Island and McDonald Islands", "VE": "Venezuela", "PR": "Puerto Rico", "PS": "Palestinian Territory", "PW": "Palau", "PT": "Portugal", "SJ": "Svalbard and Jan Mayen", "PY": "Paraguay", "IQ": "Iraq", "PA": "Panama", "PF": "French Polynesia", "PG": "Papua New Guinea", "PE": "Peru", "PK": "Pakistan", "PH": "Philippines", "PN": "Pitcairn", "PL": "Poland", "PM": "Saint Pierre and Miquelon", "ZM": "Zambia", "EH": "Western Sahara", "EE": "Estonia", "EG": "Egypt", "ZA": "South Africa", "EC": "Ecuador", "IT": "Italy", "VN": "Vietnam", "SB": "Solomon Islands", "ET": "Ethiopia", "SO": "Somalia", "ZW": "Zimbabwe", "SA": "Saudi Arabia", "ES": "Spain", "ER": "Eritrea", "ME": "Montenegro", "MD": "Moldova", "MG": "Madagascar", "MF": "Saint Martin", "MA": "Morocco", "MC": "Monaco", "UZ": "Uzbekistan", "MM": "Myanmar", "ML": "Mali", "MO": "Macao", "MN": "Mongolia", "MH": "Marshall Islands", "MK": "Macedonia", "MU": "Mauritius", "MT": "Malta", "MW": "Malawi", "MV": "Maldives", "MQ": "Martinique", "MP": "Northern Mariana Islands", "MS": "Montserrat", "MR": "Mauritania", "IM": "Isle of Man", "UG": "Uganda", "TZ": "Tanzania", "MY": "Malaysia", "MX": "Mexico", "IL": "Israel", "FR": "France", "IO": "British Indian Ocean Territory", "SH": "Saint Helena", "FI": "Finland", "FJ": "Fiji", "FK": "Falkland Islands", "FM": "Micronesia", "FO": "Faroe Islands", "NI": "Nicaragua", "NL": "Netherlands", "NO": "Norway", "NA": "Namibia", "VU": "Vanuatu", "NC": "New Caledonia", "NE": "Niger", "NF": "Norfolk Island", "NG": "Nigeria", "NZ": "New Zealand", "NP": "Nepal", "NR": "Nauru", "NU": "Niue", "CK": "Cook Islands", "XK": "Kosovo", "CI": "Ivory Coast", "CH": "Switzerland", "CO": "Colombia", "CN": "China", "CM": "Cameroon", "CL": "Chile", "CC": "Cocos Islands", "CA": "Canada", "CG": "Republic of the Congo", "CF": "Central African Republic", "CD": "Democratic Republic of the Congo", "CZ": "Czech Republic", "CY": "Cyprus", "CX": "Christmas Island", "CR": "Costa Rica", "CW": "Curacao", "CV": "Cape Verde", "CU": "Cuba", "SZ": "Swaziland", "SY": "Syria", "SX": "Sint Maarten", "KG": "Kyrgyzstan", "KE": "Kenya", "SS": "South Sudan", "SR": "Suriname", "KI": "Kiribati", "KH": "Cambodia", "KN": "Saint Kitts and Nevis", "KM": "Comoros", "ST": "Sao Tome and Principe", "SK": "Slovakia", "KR": "South Korea", "SI": "Slovenia", "KP": "North Korea", "KW": "Kuwait", "SN": "Senegal", "SM": "San Marino", "SL": "Sierra Leone", "SC": "Seychelles", "KZ": "Kazakhstan", "KY": "Cayman Islands", "SG": "Singapore", "SE": "Sweden", "SD": "Sudan", "DO": "Dominican Republic", "DM": "Dominica", "DJ": "Djibouti", "DK": "Denmark", "VG": "British Virgin Islands", "DE": "Germany", "YE": "Yemen", "DZ": "Algeria", "US": "United States", "UY": "Uruguay", "YT": "Mayotte", "UM": "United States Minor Outlying Islands", "LB": "Lebanon", "LC": "Saint Lucia", "LA": "Laos", "TV": "Tuvalu", "TW": "Taiwan", "TT": "Trinidad and Tobago", "TR": "Turkey", "LK": "Sri Lanka", "LI": "Liechtenstein", "LV": "Latvia", "TO": "Tonga", "LT": "Lithuania", "LU": "Luxembourg", "LR": "Liberia", "LS": "Lesotho", "TH": "Thailand", "TF": "French Southern Territories", "TG": "Togo", "TD": "Chad", "TC": "Turks and Caicos Islands", "LY": "Libya", "VA": "Vatican", "VC": "Saint Vincent and the Grenadines", "AE": "United Arab Emirates", "AD": "Andorra", "AG": "Antigua and Barbuda", "AF": "Afghanistan", "AI": "Anguilla", "VI": "U.S. Virgin Islands", "IS": "Iceland", "IR": "Iran", "AM": "Armenia", "AL": "Albania", "AO": "Angola", "AQ": "Antarctica", "AS": "American Samoa", "AR": "Argentina", "AU": "Australia", "AT": "Austria", "AW": "Aruba", "IN": "India", "AX": "Aland Islands", "AZ": "Azerbaijan", "IE": "Ireland", "ID": "Indonesia", "UA": "Ukraine", "QA": "Qatar", "MZ": "Mozambique"}
@@ -0,0 +1,3 @@
1
+ module IPinfo
2
+ class RateLimitError < StandardError; end
3
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddr'
4
+ require 'json'
5
+
6
+ module IPinfo
7
+ class Response
8
+
9
+ attr_reader :all
10
+
11
+ def initialize(response)
12
+ @all = response
13
+
14
+ @all.each do |name, value|
15
+ instance_variable_set("@#{name}", value)
16
+ self.class.send(:attr_accessor, name)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module IPinfo
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: IPinfo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Stanislav K, James Timmins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lrucache
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ description: " This is a ruby wrapper for http://ipinfo.io. "
56
+ email:
57
+ - jameshtimmins@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".editorconfig"
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - ipinfo.gemspec
72
+ - lib/ipinfo.rb
73
+ - lib/ipinfo/adapter.rb
74
+ - lib/ipinfo/cache/cache_interface.rb
75
+ - lib/ipinfo/cache/default_cache.rb
76
+ - lib/ipinfo/countries.json
77
+ - lib/ipinfo/errors.rb
78
+ - lib/ipinfo/response.rb
79
+ - lib/ipinfo/version.rb
80
+ homepage: https://ipinfo.io
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.0.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: This is a ruby wrapper for http://ipinfo.io.
103
+ test_files: []