easy_geoip 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +8 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +21 -0
- data/LICENSE +21 -0
- data/README.md +201 -0
- data/easy_geoip.gemspec +47 -0
- data/lib/easy_geoip.rb +35 -0
- data/lib/easy_geoip/api/freegeoip.rb +30 -0
- data/lib/easy_geoip/api/nekudo.rb +27 -0
- data/lib/easy_geoip/api/query.rb +16 -0
- data/lib/easy_geoip/api/telize.rb +32 -0
- data/lib/easy_geoip/apis.rb +3 -0
- data/lib/easy_geoip/client.rb +40 -0
- data/lib/easy_geoip/error.rb +15 -0
- data/lib/easy_geoip/geo_data.rb +39 -0
- data/lib/easy_geoip/version.rb +3 -0
- metadata +335 -0
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
1. Fork it ( https://github.com/gchan/easy_geoip/fork )
|
4
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
5
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
6
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
7
|
+
5. Create a new Pull Request
|
8
|
+
|
9
|
+
## Development
|
10
|
+
|
11
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
12
|
+
|
13
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
14
|
+
|
15
|
+
## Testing
|
16
|
+
|
17
|
+
Run `bundle exec rake test` to manually launch specs.
|
18
|
+
|
19
|
+
Run `bin/guard` to automatically launch specs when files are modified.
|
20
|
+
|
21
|
+
Run `rake rubocop` to analyze code for style offenses.
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Gordon Chan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
# EasyGeoip
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/easy_geoip.svg)](http://badge.fury.io/rb/easy_geoip) [![Dependency Status](https://gemnasium.com/gchan/easy_geoip.svg)](https://gemnasium.com/gchan/easy_geoip) ![License](https://img.shields.io/badge/license-MIT-blue.svg)
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/gchan/easy_geoip.svg?branch=master)](https://travis-ci.org/gchan/easy_geoip) [![Coverage Status](https://coveralls.io/repos/gchan/easy_geoip/badge.svg?branch=master)](https://coveralls.io/r/gchan/easy_geoip?branch=master) [![Code Climate](https://codeclimate.com/github/gchan/easy_geoip/badges/gpa.svg)](https://codeclimate.com/github/gchan/easy_geoip)
|
5
|
+
|
6
|
+
A common Ruby interface to retrieve IP-based [geolocation](https://en.wikipedia.org/wiki/Geolocation) information from various free and open source IP geolocation services.
|
7
|
+
|
8
|
+
No database downloads, no registration, no API keys, ~~no~~ [minimal](https://github.com/gchan/easy_geoip/blob/master/easy_geoip.gemspec#L27) dependencies. *Easy!*
|
9
|
+
|
10
|
+
![Easy!](https://github.com/gchan/easy_geoip/blob/master/easy.gif)
|
11
|
+
|
12
|
+
#### Quick Example
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
EasyGeoIP.query("8.8.8.8")
|
16
|
+
|
17
|
+
=> #<EasyGeoIP::GeoData:0x007f91b9512960
|
18
|
+
@city="Mountain View",
|
19
|
+
@continent_code="NA",
|
20
|
+
@country="United States",
|
21
|
+
@country_code="US",
|
22
|
+
@ip="8.8.8.8",
|
23
|
+
@isp="Google Inc.",
|
24
|
+
@latitude=37.386,
|
25
|
+
@longitude=-122.0838,
|
26
|
+
@postal_code="94040",
|
27
|
+
@region="California",
|
28
|
+
@region_code="CA",
|
29
|
+
@time_zone="America/Los_Angeles">
|
30
|
+
|
31
|
+
# IPv6 is also supported
|
32
|
+
EasyGeoIP.query("2001:4860:4860::8888")
|
33
|
+
```
|
34
|
+
|
35
|
+
See the 'Usage' section for more information.
|
36
|
+
|
37
|
+
## IP Geolocation Services
|
38
|
+
|
39
|
+
EasyGeoIP currently supports the geolocation services listed below. All these services are free, open source, and require no registration or authentication.
|
40
|
+
|
41
|
+
Regardless of which service is used, geolocation information is returned in the same format for ease of use. See the `EasyGeoIP::GeoData` section below for more information.
|
42
|
+
|
43
|
+
#### Telize (Default) `:telize`
|
44
|
+
* **HTTPS**: Yes
|
45
|
+
* **Limits**: None
|
46
|
+
* **Open Source License**: MIT
|
47
|
+
* **GitHub Project**: https://www.github.com/fcambus/telize
|
48
|
+
* **Homepage**: https://www.telize.com
|
49
|
+
|
50
|
+
#### Freegeoip `:freegeoip`
|
51
|
+
* **HTTPS**: Yes
|
52
|
+
* **Limits**: 10,000 queries per hour
|
53
|
+
* **Open Source License**: BSD 3-clause
|
54
|
+
* **GitHub Project**: https://www.github.com/fiorix/freegeoip
|
55
|
+
* **Homepage**: https://www.freegeoip.net
|
56
|
+
|
57
|
+
#### Nekudo `:nekudo`
|
58
|
+
* **HTTPS**: No
|
59
|
+
* **Limits**: None, but fair use policy applies
|
60
|
+
* **Open Source License**: BSD 3-clause
|
61
|
+
* **GitHub Project**: https://www.github.com/nekudo/shiny_geoip
|
62
|
+
* **Homepage**: http://geoip.nekudo.com
|
63
|
+
|
64
|
+
All of the above services use data provided by [MaxMind](http://www.maxmind.com).
|
65
|
+
|
66
|
+
### Reliability
|
67
|
+
|
68
|
+
The services listed above are free, require no registration, and supported by volunteer developers. It it generally understood that such services are traditionally more likely to experience downtime or disruption.
|
69
|
+
|
70
|
+
If IP geolocation is mission-critical in your application, consider using a paid service to mitigate the risk of downtime and disruption.
|
71
|
+
|
72
|
+
## Installation
|
73
|
+
|
74
|
+
Add this line to your application's Gemfile:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
gem 'easy_geoip'
|
78
|
+
```
|
79
|
+
|
80
|
+
And then execute:
|
81
|
+
|
82
|
+
$ bundle
|
83
|
+
|
84
|
+
Or install it yourself as:
|
85
|
+
|
86
|
+
$ gem install easy_geoip
|
87
|
+
|
88
|
+
|
89
|
+
#### Supported Ruby versions
|
90
|
+
EasyGeoIP supports MRI Ruby 1.9+ and the JRuby and Rubinius equivalent. The specific Ruby versions we build and test on can be found at [TravisCI](https://travis-ci.org/gchan/easy_geoip).
|
91
|
+
|
92
|
+
## Usage
|
93
|
+
|
94
|
+
#### Configuration
|
95
|
+
|
96
|
+
EasyGeoIP supports a few free IP geolocation services; the default is [Telize](https://www.telize.com) but EasyGeoIP can easily be configured to use another service.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# The default geolocation service is Telize
|
100
|
+
EasyGeoIP.api_service
|
101
|
+
=> :telize
|
102
|
+
|
103
|
+
# Variable assignment
|
104
|
+
EasyGeoIP.api_service = :nekudo
|
105
|
+
|
106
|
+
# Calling `.configure` with Hash argument
|
107
|
+
EasyGeoIP.configure(api_service: :freegeoip)
|
108
|
+
|
109
|
+
# Calling `.configure` with a block
|
110
|
+
EasyGeoIP.configure do |config|
|
111
|
+
config.api_service = :telize
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
#### Querying IPs - One Method to Rule Them All
|
116
|
+
|
117
|
+
Use the `query` method to retrieve geolocation information for a specified IPv4 or IPv6 address. An instance of `EasyGeoIP::GeoData` is returned.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
EasyGeoIP.query("8.8.8.8")
|
121
|
+
|
122
|
+
# `EasyGeoIP::GeoData` object is returned
|
123
|
+
=> #<EasyGeoIP::GeoData:0x007f91b9512960
|
124
|
+
@city="Mountain View",
|
125
|
+
@continent_code="NA",
|
126
|
+
@country="United States",
|
127
|
+
@country_code="US",
|
128
|
+
@ip="8.8.8.8",
|
129
|
+
@isp="Google Inc.",
|
130
|
+
@latitude=37.386,
|
131
|
+
@longitude=-122.0838,
|
132
|
+
@postal_code="94040",
|
133
|
+
@region="California",
|
134
|
+
@region_code="CA",
|
135
|
+
@time_zone="America/Los_Angeles">
|
136
|
+
```
|
137
|
+
|
138
|
+
**Bonus** - Pass in `nil`, an empty string or no arguments and the default geolocation service, [Telize](https://www.telize.com) will return the geolocation information for your IP address!
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
EasyGeoIP.query(nil)
|
142
|
+
EasyGeoIP.query("")
|
143
|
+
EasyGeoIP.query
|
144
|
+
```
|
145
|
+
|
146
|
+
#### EasyGeoIP::GeoData
|
147
|
+
|
148
|
+
Regardless of which service you use, geolocation information is returned as a `EasyGeoIP::GeoData` object where its attributes are easily accessible. Calling `#to_hash` returns all information as a `Hash`.
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
geo_data = EasyGeoIP.query("8.8.8.8")
|
152
|
+
|
153
|
+
geo_data.city # => "Mountain View"
|
154
|
+
geo_data.continent_code # => "NA"
|
155
|
+
geo_data.country # => "United States"
|
156
|
+
geo_data.country_code # => "US"
|
157
|
+
geo_data.ip # => "8.8.8.8"
|
158
|
+
geo_data.isp # => "Google Inc."
|
159
|
+
geo_data.latitude # => 37.386
|
160
|
+
geo_data.longitude # => -122.0838
|
161
|
+
geo_data.postal_code # => "94040"
|
162
|
+
geo_data.region # => "California"
|
163
|
+
geo_data.region_code # => "CA"
|
164
|
+
geo_data.time_zone # => "America/Los_Angeles"
|
165
|
+
|
166
|
+
# Return a Hash of all attributes
|
167
|
+
geo_data.to_hash
|
168
|
+
|
169
|
+
=> {:ip=>"8.8.8.8",
|
170
|
+
:country=>"United States",
|
171
|
+
:country_code=>"US",
|
172
|
+
:city=>"Mountain View",
|
173
|
+
:region=>"California",
|
174
|
+
:region_code=>"CA",
|
175
|
+
:postal_code=>"94040",
|
176
|
+
:continent_code=>"NA",
|
177
|
+
:latitude=>37.386,
|
178
|
+
:longitude=>-122.0838,
|
179
|
+
:time_zone=>"America/Los_Angeles",
|
180
|
+
:isp=>"Google Inc."}
|
181
|
+
```
|
182
|
+
|
183
|
+
#### Errors
|
184
|
+
|
185
|
+
In addition to the usual Ruby errors and exceptions, EasyGeoIP can raise the following errors:
|
186
|
+
|
187
|
+
* `EasyGeoIP::Error::ClientError`
|
188
|
+
* `EasyGeoIP::Error::ConnectionFailed`
|
189
|
+
* `EasyGeoIP::Error::TimeoutError`
|
190
|
+
* `EasyGeoIP::Error::ResourceNotFound`
|
191
|
+
* `EasyGeoIP::Error::ParsingError`
|
192
|
+
|
193
|
+
All of the above error classes inherit `EasyGeoIP::Error::ClientError`, `EasyGeoIP::Error`, and `StandardError`.
|
194
|
+
|
195
|
+
## Contributing & Development
|
196
|
+
|
197
|
+
Please view [CONTRIBUTING.md](https://github.com/gchan/easy_geoip/blob/master/CONTRIBUTING.md) for contributing and development information.
|
198
|
+
|
199
|
+
## License
|
200
|
+
|
201
|
+
EasyGeoIP is Copyright (c) 2015 Gordon Chan and is released under the MIT License. It is free software, and may be redistributed under the terms specified in the LICENSE file.
|
data/easy_geoip.gemspec
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "easy_geoip/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "easy_geoip"
|
8
|
+
spec.version = EasyGeoIP::VERSION
|
9
|
+
spec.authors = ["Gordon Chan"]
|
10
|
+
spec.email = ["developer.gordon@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A small, simple, and easy IP geolocation client."
|
13
|
+
spec.description = "A common Ruby interface for various free IP geolocation
|
14
|
+
services. Small, simple and easy to use."
|
15
|
+
spec.homepage = "https://www.github.com/gchan/easy_geoip"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = Dir["lib/**/*.rb"] +
|
19
|
+
%w(CHANGELOG.md LICENSE README.md CONTRIBUTING.md CODE_OF_CONDUCT.md
|
20
|
+
easy_geoip.gemspec)
|
21
|
+
spec.bindir = "bin"
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.platform = Gem::Platform::RUBY
|
24
|
+
|
25
|
+
spec.required_ruby_version = ">= 1.9.3"
|
26
|
+
|
27
|
+
spec.add_runtime_dependency "faraday", "~> 0.9.1"
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "pry", "~> 0.10.1"
|
32
|
+
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.7.0"
|
34
|
+
spec.add_development_dependency "mocha", "~> 1.1.0"
|
35
|
+
spec.add_development_dependency "minitest-profile", "~> 0.0.2"
|
36
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.0.19"
|
37
|
+
spec.add_development_dependency "guard", "~> 2.13.0"
|
38
|
+
spec.add_development_dependency "guard-minitest", "~> 2.4.4"
|
39
|
+
|
40
|
+
spec.add_development_dependency "vcr", "~> 2.9.3"
|
41
|
+
spec.add_development_dependency "webmock", "~> 1.21.0"
|
42
|
+
spec.add_development_dependency "multi_json", "~> 1.11.0"
|
43
|
+
spec.add_development_dependency "simplecov", "~> 0.10.0"
|
44
|
+
spec.add_development_dependency "rubocop", "~> 0.32.1"
|
45
|
+
spec.add_development_dependency "coveralls", "~> 0.8.2"
|
46
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
|
47
|
+
end
|
data/lib/easy_geoip.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "easy_geoip/version"
|
2
|
+
require "easy_geoip/apis"
|
3
|
+
|
4
|
+
module EasyGeoIP
|
5
|
+
extend self
|
6
|
+
|
7
|
+
attr_accessor :api_service
|
8
|
+
|
9
|
+
def configure(settings = {}, &block)
|
10
|
+
apply_configuration(settings)
|
11
|
+
|
12
|
+
yield self if block
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def query(ip)
|
17
|
+
api.query(ip)
|
18
|
+
end
|
19
|
+
|
20
|
+
def api_service
|
21
|
+
@api_service || :telize
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def api
|
27
|
+
API.const_get(api_service.capitalize)
|
28
|
+
end
|
29
|
+
|
30
|
+
def apply_configuration(settings)
|
31
|
+
settings.each do |attr, value|
|
32
|
+
send("#{attr}=", value)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "easy_geoip/api/query"
|
2
|
+
|
3
|
+
module EasyGeoIP
|
4
|
+
module API
|
5
|
+
module Freegeoip
|
6
|
+
extend Query
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.url(ip)
|
11
|
+
"https://freegeoip.net/json/#{ip}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.standardize_geodata(json)
|
15
|
+
{
|
16
|
+
ip: json["ip"],
|
17
|
+
country: json["country_name"],
|
18
|
+
country_code: json["country_code"],
|
19
|
+
city: json["city"],
|
20
|
+
region: json["region_name"],
|
21
|
+
region_code: json["region_code"],
|
22
|
+
postal_code: json["zip_code"],
|
23
|
+
latitude: json["latitude"],
|
24
|
+
longitude: json["longitude"],
|
25
|
+
time_zone: json["time_zone"]
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "easy_geoip/api/query"
|
2
|
+
|
3
|
+
module EasyGeoIP
|
4
|
+
module API
|
5
|
+
module Nekudo
|
6
|
+
extend Query
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.url(ip)
|
11
|
+
"http://geoip.nekudo.com/api/#{ip}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.standardize_geodata(json)
|
15
|
+
{
|
16
|
+
ip: json["ip"],
|
17
|
+
country: json["country"]["name"],
|
18
|
+
country_code: json["country"]["code"],
|
19
|
+
city: json["city"],
|
20
|
+
latitude: json["location"]["latitude"],
|
21
|
+
longitude: json["location"]["longitude"],
|
22
|
+
time_zone: json["location"]["time_zone"]
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "easy_geoip/client"
|
2
|
+
require "easy_geoip/geo_data"
|
3
|
+
|
4
|
+
module EasyGeoIP
|
5
|
+
module API
|
6
|
+
module Query
|
7
|
+
def query(ip = "")
|
8
|
+
url = url(ip)
|
9
|
+
json = EasyGeoIP::Client.get_json(url)
|
10
|
+
geo_data = standardize_geodata(json)
|
11
|
+
|
12
|
+
EasyGeoIP::GeoData.new(geo_data)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "easy_geoip/api/query"
|
2
|
+
|
3
|
+
module EasyGeoIP
|
4
|
+
module API
|
5
|
+
module Telize
|
6
|
+
extend Query
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.url(ip)
|
11
|
+
"https://www.telize.com/geoip/#{ip}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.standardize_geodata(json)
|
15
|
+
{
|
16
|
+
ip: json["ip"],
|
17
|
+
country: json["country"],
|
18
|
+
country_code: json["country_code"],
|
19
|
+
city: json["city"],
|
20
|
+
region: json["region"],
|
21
|
+
region_code: json["region_code"],
|
22
|
+
postal_code: json["postal_code"],
|
23
|
+
continent_code: json["continent_code"],
|
24
|
+
latitude: json["latitude"],
|
25
|
+
longitude: json["longitude"],
|
26
|
+
time_zone: json["timezone"],
|
27
|
+
isp: json["isp"]
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "json"
|
3
|
+
require "easy_geoip/error"
|
4
|
+
|
5
|
+
module EasyGeoIP
|
6
|
+
module Client
|
7
|
+
def self.get(*args, &block)
|
8
|
+
connection.get(*args, &block)
|
9
|
+
rescue Faraday::ClientError => error
|
10
|
+
handle_error(error)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.get_json(*args, &block)
|
14
|
+
body = get(*args, &block).body
|
15
|
+
JSON.parse(body)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def self.connection
|
21
|
+
@connection ||= Faraday::Connection.new do |connection|
|
22
|
+
connection.adapter Faraday.default_adapter
|
23
|
+
connection.use Faraday::Response::RaiseError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.handle_error(error)
|
28
|
+
case error
|
29
|
+
when Faraday::ConnectionFailed
|
30
|
+
raise EasyGeoIP::Error::ConnectionFailed.new error
|
31
|
+
when Faraday::ResourceNotFound
|
32
|
+
raise EasyGeoIP::Error::ResourceNotFound.new error
|
33
|
+
when Faraday::TimeoutError
|
34
|
+
raise EasyGeoIP::Error::TimeoutError.new error
|
35
|
+
else # Faraday::ClientError
|
36
|
+
raise EasyGeoIP::Error::ClientError.new error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module EasyGeoIP
|
2
|
+
class Error < StandardError
|
3
|
+
# Generic Client Error
|
4
|
+
ClientError = Class.new(Error)
|
5
|
+
|
6
|
+
# ConnectionFailed from Faraday
|
7
|
+
ConnectionFailed = Class.new(ClientError)
|
8
|
+
|
9
|
+
# TimeoutError from Faraday
|
10
|
+
TimeoutError = Class.new(ClientError)
|
11
|
+
|
12
|
+
# ResourceNotFound from Faraday
|
13
|
+
ResourceNotFound = Class.new(ClientError)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module EasyGeoIP
|
2
|
+
class GeoData
|
3
|
+
attr_reader :ip, :country, :country_code, :city, :region, :region_code,
|
4
|
+
:postal_code, :continent_code, :latitude, :longitude,
|
5
|
+
:time_zone, :isp
|
6
|
+
|
7
|
+
def initialize(attributes = {})
|
8
|
+
@ip = attributes[:ip]
|
9
|
+
@country = attributes[:country]
|
10
|
+
@country_code = attributes[:country_code]
|
11
|
+
@city = attributes[:city]
|
12
|
+
@region = attributes[:region]
|
13
|
+
@region_code = attributes[:region_code]
|
14
|
+
@postal_code = attributes[:postal_code]
|
15
|
+
@continent_code = attributes[:continent_code]
|
16
|
+
@latitude = attributes[:latitude]
|
17
|
+
@longitude = attributes[:longitude]
|
18
|
+
@time_zone = attributes[:time_zone]
|
19
|
+
@isp = attributes[:isp]
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_hash
|
23
|
+
{
|
24
|
+
ip: ip,
|
25
|
+
country: country,
|
26
|
+
country_code: country_code,
|
27
|
+
city: city,
|
28
|
+
region: region,
|
29
|
+
region_code: region_code,
|
30
|
+
postal_code: postal_code,
|
31
|
+
continent_code: continent_code,
|
32
|
+
latitude: latitude,
|
33
|
+
longitude: longitude,
|
34
|
+
time_zone: time_zone,
|
35
|
+
isp: isp
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,335 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_geoip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gordon Chan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-08-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.6'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.6'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.10.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.10.1
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: minitest
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 5.7.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 5.7.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mocha
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.1.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.1.0
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: minitest-profile
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.0.2
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.0.2
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: minitest-reporters
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 1.0.19
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: 1.0.19
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: guard
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ~>
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 2.13.0
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ~>
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 2.13.0
|
158
|
+
- !ruby/object:Gem::Dependency
|
159
|
+
name: guard-minitest
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ~>
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: 2.4.4
|
166
|
+
type: :development
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ~>
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 2.4.4
|
174
|
+
- !ruby/object:Gem::Dependency
|
175
|
+
name: vcr
|
176
|
+
requirement: !ruby/object:Gem::Requirement
|
177
|
+
none: false
|
178
|
+
requirements:
|
179
|
+
- - ~>
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: 2.9.3
|
182
|
+
type: :development
|
183
|
+
prerelease: false
|
184
|
+
version_requirements: !ruby/object:Gem::Requirement
|
185
|
+
none: false
|
186
|
+
requirements:
|
187
|
+
- - ~>
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: 2.9.3
|
190
|
+
- !ruby/object:Gem::Dependency
|
191
|
+
name: webmock
|
192
|
+
requirement: !ruby/object:Gem::Requirement
|
193
|
+
none: false
|
194
|
+
requirements:
|
195
|
+
- - ~>
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: 1.21.0
|
198
|
+
type: :development
|
199
|
+
prerelease: false
|
200
|
+
version_requirements: !ruby/object:Gem::Requirement
|
201
|
+
none: false
|
202
|
+
requirements:
|
203
|
+
- - ~>
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: 1.21.0
|
206
|
+
- !ruby/object:Gem::Dependency
|
207
|
+
name: multi_json
|
208
|
+
requirement: !ruby/object:Gem::Requirement
|
209
|
+
none: false
|
210
|
+
requirements:
|
211
|
+
- - ~>
|
212
|
+
- !ruby/object:Gem::Version
|
213
|
+
version: 1.11.0
|
214
|
+
type: :development
|
215
|
+
prerelease: false
|
216
|
+
version_requirements: !ruby/object:Gem::Requirement
|
217
|
+
none: false
|
218
|
+
requirements:
|
219
|
+
- - ~>
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: 1.11.0
|
222
|
+
- !ruby/object:Gem::Dependency
|
223
|
+
name: simplecov
|
224
|
+
requirement: !ruby/object:Gem::Requirement
|
225
|
+
none: false
|
226
|
+
requirements:
|
227
|
+
- - ~>
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: 0.10.0
|
230
|
+
type: :development
|
231
|
+
prerelease: false
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
233
|
+
none: false
|
234
|
+
requirements:
|
235
|
+
- - ~>
|
236
|
+
- !ruby/object:Gem::Version
|
237
|
+
version: 0.10.0
|
238
|
+
- !ruby/object:Gem::Dependency
|
239
|
+
name: rubocop
|
240
|
+
requirement: !ruby/object:Gem::Requirement
|
241
|
+
none: false
|
242
|
+
requirements:
|
243
|
+
- - ~>
|
244
|
+
- !ruby/object:Gem::Version
|
245
|
+
version: 0.32.1
|
246
|
+
type: :development
|
247
|
+
prerelease: false
|
248
|
+
version_requirements: !ruby/object:Gem::Requirement
|
249
|
+
none: false
|
250
|
+
requirements:
|
251
|
+
- - ~>
|
252
|
+
- !ruby/object:Gem::Version
|
253
|
+
version: 0.32.1
|
254
|
+
- !ruby/object:Gem::Dependency
|
255
|
+
name: coveralls
|
256
|
+
requirement: !ruby/object:Gem::Requirement
|
257
|
+
none: false
|
258
|
+
requirements:
|
259
|
+
- - ~>
|
260
|
+
- !ruby/object:Gem::Version
|
261
|
+
version: 0.8.2
|
262
|
+
type: :development
|
263
|
+
prerelease: false
|
264
|
+
version_requirements: !ruby/object:Gem::Requirement
|
265
|
+
none: false
|
266
|
+
requirements:
|
267
|
+
- - ~>
|
268
|
+
- !ruby/object:Gem::Version
|
269
|
+
version: 0.8.2
|
270
|
+
- !ruby/object:Gem::Dependency
|
271
|
+
name: codeclimate-test-reporter
|
272
|
+
requirement: !ruby/object:Gem::Requirement
|
273
|
+
none: false
|
274
|
+
requirements:
|
275
|
+
- - ~>
|
276
|
+
- !ruby/object:Gem::Version
|
277
|
+
version: 0.4.7
|
278
|
+
type: :development
|
279
|
+
prerelease: false
|
280
|
+
version_requirements: !ruby/object:Gem::Requirement
|
281
|
+
none: false
|
282
|
+
requirements:
|
283
|
+
- - ~>
|
284
|
+
- !ruby/object:Gem::Version
|
285
|
+
version: 0.4.7
|
286
|
+
description: ! "A common Ruby interface for various free IP geolocation\n services.
|
287
|
+
Small, simple and easy to use."
|
288
|
+
email:
|
289
|
+
- developer.gordon@gmail.com
|
290
|
+
executables: []
|
291
|
+
extensions: []
|
292
|
+
extra_rdoc_files: []
|
293
|
+
files:
|
294
|
+
- lib/easy_geoip/api/freegeoip.rb
|
295
|
+
- lib/easy_geoip/api/nekudo.rb
|
296
|
+
- lib/easy_geoip/api/query.rb
|
297
|
+
- lib/easy_geoip/api/telize.rb
|
298
|
+
- lib/easy_geoip/apis.rb
|
299
|
+
- lib/easy_geoip/client.rb
|
300
|
+
- lib/easy_geoip/error.rb
|
301
|
+
- lib/easy_geoip/geo_data.rb
|
302
|
+
- lib/easy_geoip/version.rb
|
303
|
+
- lib/easy_geoip.rb
|
304
|
+
- CHANGELOG.md
|
305
|
+
- LICENSE
|
306
|
+
- README.md
|
307
|
+
- CONTRIBUTING.md
|
308
|
+
- CODE_OF_CONDUCT.md
|
309
|
+
- easy_geoip.gemspec
|
310
|
+
homepage: https://www.github.com/gchan/easy_geoip
|
311
|
+
licenses:
|
312
|
+
- MIT
|
313
|
+
post_install_message:
|
314
|
+
rdoc_options: []
|
315
|
+
require_paths:
|
316
|
+
- lib
|
317
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
318
|
+
none: false
|
319
|
+
requirements:
|
320
|
+
- - ! '>='
|
321
|
+
- !ruby/object:Gem::Version
|
322
|
+
version: 1.9.3
|
323
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
324
|
+
none: false
|
325
|
+
requirements:
|
326
|
+
- - ! '>='
|
327
|
+
- !ruby/object:Gem::Version
|
328
|
+
version: '0'
|
329
|
+
requirements: []
|
330
|
+
rubyforge_project:
|
331
|
+
rubygems_version: 1.8.23.2
|
332
|
+
signing_key:
|
333
|
+
specification_version: 3
|
334
|
+
summary: A small, simple, and easy IP geolocation client.
|
335
|
+
test_files: []
|