ip_to_earth 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +140 -0
- data/lib/ip_to_earth/api_controller.rb +45 -0
- data/lib/ip_to_earth/api_exception.rb +11 -0
- data/lib/ip_to_earth/api_result.rb +11 -0
- data/lib/ip_to_earth/client.rb +15 -0
- data/lib/ip_to_earth/configuration.rb +11 -0
- data/lib/ip_to_earth.rb +20 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 881975620b315dd19ec558c791daf4bf28b1ef844b0bef6dc6fcf55d9b41db87
|
4
|
+
data.tar.gz: 37494f862b4768f38790f67398ace65cbbdde69f2bcead5e8ec84b38ec4b1719
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6026b34e21431fedee9c75bee945387c07121c686ed22aa0e31fef212f4f94d1685f173ae7313cc3cc4a8425290eeafcc6ea8ca031da7c8c0b2bec5be3c8ace
|
7
|
+
data.tar.gz: 02ca79cd2d32860e75560454583a1f3fa7d99ad35c567d7f252cd4ae264baf9408cc4c3a0ebac1eda551d5b1af93f86a09e054d20be806771c83aa63df84338d
|
data/README.md
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
|
2
|
+
<p align="center">
|
3
|
+
<img src="https://app.expeditedaddons.com/iptoearth.png"/>
|
4
|
+
|
5
|
+
</p>
|
6
|
+
|
7
|
+
<h1 align='center'>IP to Earth</h1>
|
8
|
+
|
9
|
+
## Find the Country and City of origin for an IP Address
|
10
|
+
|
11
|
+
## Requirements
|
12
|
+
|
13
|
+
- An API Key from Heroku
|
14
|
+
- Add from: https://elements.heroku.com/addons/iptoearth
|
15
|
+
|
16
|
+
- Ruby v2.x or greater
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### Input Parameter Descriptions
|
21
|
+
|
22
|
+
| Index | Name | Example | Description |
|
23
|
+
| --------------- | ------- | -------- |--------|
|
24
|
+
|0|`ip`|string|IPv4 or IPv6 address|
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
### Making a Request
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require "ip_to_earth"
|
32
|
+
|
33
|
+
IpToEarth.lookup('68.10.149.45') # Pass in any valid IPv4 or IPv6 value
|
34
|
+
```
|
35
|
+
|
36
|
+
### Using Results
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
result = IpToEarth.lookup("8.8.8.8")
|
40
|
+
# => #<struct IpToEarth::APIResult
|
41
|
+
# valid=true,
|
42
|
+
# country="United States",
|
43
|
+
# country_code="US",
|
44
|
+
# hostname="",
|
45
|
+
# city="Mountain View",
|
46
|
+
# ip="8.8.8.8",
|
47
|
+
# latitude=37.40599060058594,
|
48
|
+
# longitude=-122.0785140991211,
|
49
|
+
# region="California",
|
50
|
+
# continent_code="NA",
|
51
|
+
# country_code3="USA",
|
52
|
+
# currency_code="USD">
|
53
|
+
|
54
|
+
result.valid # => true
|
55
|
+
result.country # => "United States"
|
56
|
+
result.country_code # => "US"
|
57
|
+
result.city # => "Virginia Beach"
|
58
|
+
result.latitude # => 37.40599060058594
|
59
|
+
# ...
|
60
|
+
```
|
61
|
+
|
62
|
+
|
63
|
+
## Installation
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# In your Gemfile
|
67
|
+
gem 'ip_to_earth', git: 'https://github.com/LynxEyes/ip_to_earth.git'
|
68
|
+
```
|
69
|
+
|
70
|
+
|
71
|
+
## Result Attribute Descriptions
|
72
|
+
|
73
|
+
| Attribute | Type | Description |
|
74
|
+
| --------------- | ------- | -------- |
|
75
|
+
|`valid`|boolean|If the IP Address passed in is syntactically valid|
|
76
|
+
|`country`|string|Full country name where the IP address is located|
|
77
|
+
|`country_code`|string|ISO Country Code for the IP Address|
|
78
|
+
|`hostname`|string|Hostname - if any - for the IP address|
|
79
|
+
|`city`|string|City where the IP Address is located|
|
80
|
+
|`ip`|string|IP address that was evaluated|
|
81
|
+
|`latitude`|float|Geographic latitude of the identified IP address|
|
82
|
+
|`longitude`|float|Geographic longitude of the identified IP address|
|
83
|
+
|`region`|string|State or Region where the IP address is located|
|
84
|
+
|
85
|
+
|
86
|
+
## Live Interactive Testing
|
87
|
+
|
88
|
+
Doublecheck results, use a Live Proxy and check your API Key with the interactive documentation at:
|
89
|
+
|
90
|
+
http://docs.iptoearthexp.apiary.io/
|
91
|
+
|
92
|
+
You will need your `IPTOEARTH_API_KEY` from the setup screen where you've provisioned the IP to Earth add-on.
|
93
|
+
|
94
|
+
## Troubleshooting
|
95
|
+
|
96
|
+
As a sanity check it is sometimes useful to bypass your app stack and check the endpoint, your API Key and parameters directly.
|
97
|
+
|
98
|
+
*Test with your browser*
|
99
|
+
|
100
|
+
```
|
101
|
+
# Modify the following to use your actual API Key
|
102
|
+
'https://iptoearth.expeditedaddons.com/?api_key=REPLACE_WITH_YOUR_IPTOEARTH_API_KEY&ip=68.10.149.45'
|
103
|
+
```
|
104
|
+
|
105
|
+
A successful call will return your requested data with a HTTP result code of `200` and be in `JSON` format. We recommend the [JSON Formatter](https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa?hl=en) extension as a useful tool.
|
106
|
+
|
107
|
+
## In Development
|
108
|
+
|
109
|
+
The IP to Earth gem relies upon the environment variable `ENV['IPTOEARTH_API_KEY']` being present and correctly configured to authenticate to the service. You will need to have this variable present in your local environment for the gem to work correctly.
|
110
|
+
|
111
|
+
If you're using Heroku, please read their [Guide to Heroku Local](https://devcenter.heroku.com/articles/heroku-local) which has instructions on easily copying your config values to your development environment.
|
112
|
+
|
113
|
+
**DOTENV**
|
114
|
+
|
115
|
+
[https://github.com/bkeepers/dotenv](https://github.com/bkeepers/dotenv)
|
116
|
+
|
117
|
+
If you're trying to use IP to Earth in your local development or test environment with the [dotenv](https://github.com/bkeepers/dotenv) gem be sure that you are loading the `dotenv-rails` gem with the `rails-now` requirement.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
# Example Gemfile excerpt
|
121
|
+
|
122
|
+
gem 'dotenv-rails', :require => 'dotenv/rails-now'
|
123
|
+
gem 'iptoearth'
|
124
|
+
```
|
125
|
+
|
126
|
+
**FOREMAN**
|
127
|
+
|
128
|
+
[https://github.com/ddollar/foreman](https://github.com/ddollar/foreman)
|
129
|
+
|
130
|
+
If you're having issues with configuring `dotenv`, you might want to try [foreman](https://github.com/ddollar/foreman) which will also autoload `.env` files in your local environment.
|
131
|
+
|
132
|
+
|
133
|
+
**Test in the Rails console**
|
134
|
+
|
135
|
+
Launch `rails c` in your development project directory and at the prompt enter `ENV[IPTOEARTH_API_KEY]` which, if you've configured your development environment correctly should display your API Key.
|
136
|
+
|
137
|
+
## License
|
138
|
+
|
139
|
+
The IP to Earth gem is licensed under the MIT license.
|
140
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module IpToEarth
|
2
|
+
class APIController
|
3
|
+
HEADERS = {
|
4
|
+
accept: 'application/json'
|
5
|
+
}.freeze
|
6
|
+
|
7
|
+
def initialize(api_key: Configuration.api_key)
|
8
|
+
@lookup_url_template =
|
9
|
+
Addressable::Template.new("#{Configuration.base_uri}/{?ip,api_key}")
|
10
|
+
.partial_expand(api_key: api_key)
|
11
|
+
end
|
12
|
+
|
13
|
+
def lookup(ip)
|
14
|
+
query_url = lookup_url_template.expand(ip: ip)
|
15
|
+
|
16
|
+
response = Http.headers(HEADERS).get(query_url)
|
17
|
+
|
18
|
+
fail_on_bad_response(response)
|
19
|
+
parse_response(response)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :lookup_url_template
|
25
|
+
|
26
|
+
def fail_on_bad_response(response)
|
27
|
+
return unless [403, 404, 429, 500].include?(response.code)
|
28
|
+
|
29
|
+
error_message = case response.code
|
30
|
+
when 429 then "Rate limit exceeded"
|
31
|
+
when 404 then "Endpoint not found"
|
32
|
+
when 500 then "Endpoint application error"
|
33
|
+
when 403 then "API Key not found or invalid"
|
34
|
+
end
|
35
|
+
|
36
|
+
raise APIException.new(error_message, response.code, response.body)
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_response(response)
|
40
|
+
APIResult.new(response.parse(:json))
|
41
|
+
rescue JSON::ParserError
|
42
|
+
raise APIException.new("Invalid JSON returned.", response.code, response.to_s)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module IpToEarth
|
2
|
+
APIResult = Struct.new(:valid, :country, :country_code, :hostname,
|
3
|
+
:city, :ip, :latitude, :longitude, :region,
|
4
|
+
:continent_code, :country_code3, :currency_code) do
|
5
|
+
|
6
|
+
# NOTE: yes, keyword_init: true exists but raises an error on unknown keywords!
|
7
|
+
def initialize(args)
|
8
|
+
members.each { |field| send("#{field}=", args[field.to_s]) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module IpToEarth
|
2
|
+
class Client
|
3
|
+
def self.default
|
4
|
+
@default_instance ||= new
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(api_key: Configuration.api_key)
|
8
|
+
@api_controller = APIController.new(api_key: api_key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def lookup(ip)
|
12
|
+
@api_controller.lookup(ip)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/ip_to_earth.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'json'
|
3
|
+
require 'http'
|
4
|
+
require 'addressable'
|
5
|
+
|
6
|
+
require_relative 'ip_to_earth/api_exception.rb'
|
7
|
+
require_relative 'ip_to_earth/configuration.rb'
|
8
|
+
require_relative 'ip_to_earth/client.rb'
|
9
|
+
require_relative 'ip_to_earth/api_controller.rb'
|
10
|
+
require_relative 'ip_to_earth/api_result.rb'
|
11
|
+
|
12
|
+
module IpToEarth
|
13
|
+
def self.lookup(ip)
|
14
|
+
Client.default.lookup(ip)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.with_api_key(key)
|
18
|
+
Client.new(api_key: key)
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ip_to_earth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivo Jesus
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: addressable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.6'
|
41
|
+
description: Find the Country and City of origin for an IP Address
|
42
|
+
email: ivo.jesus@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- README.md
|
48
|
+
- lib/ip_to_earth.rb
|
49
|
+
- lib/ip_to_earth/api_controller.rb
|
50
|
+
- lib/ip_to_earth/api_exception.rb
|
51
|
+
- lib/ip_to_earth/api_result.rb
|
52
|
+
- lib/ip_to_earth/client.rb
|
53
|
+
- lib/ip_to_earth/configuration.rb
|
54
|
+
homepage: https://github.com/LynxEyes/ip_to_earth
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2.3'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.7.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: ip_to_earth
|
78
|
+
test_files: []
|