datacenter_lookup 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +19 -0
- data/lib/datacenter_lookup/parser.rb +42 -0
- data/lib/datacenter_lookup.rb +5 -0
- data/vendor/ipcat/datacenters.csv +3429 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 661daba01d417381b4c0464d98a342009e98db17d5cc806a018b6b95684a2165
|
4
|
+
data.tar.gz: 570d1a394e89b0c2e9a53be5429cb90d08164918e41ac55516a25c483e812f54
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42ddb749f13d24241da4f425791c2841e257d669d0250d7714b350c9641d32dcc0659cff991ba4dabac62ce10caa4f33cc2494bfa0a71a960a83d11d75c84f49
|
7
|
+
data.tar.gz: 661b6c5d06566189163c1a7108ddb6fbed934de52d824efe0208ba708bbb046d536ecb4415c9e084f9c4a1401a9cdf0e41ea6945223f02f940907efe880d3d1b
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright 2019 Dan Benjamin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
The data is licensed under GPL-3.0 by IPCat (https://github.com/client9/ipcat).
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Datacenter Lookup
|
2
|
+
|
3
|
+
Datacenter Lookup is an IP address parser in Ruby using the datacenters list from [IPCat](https://github.com/client9/ipcat).
|
4
|
+
|
5
|
+
Installation:
|
6
|
+
|
7
|
+
```
|
8
|
+
gem 'datacenter-lookup', '~> 0.1.3'
|
9
|
+
```
|
10
|
+
|
11
|
+
Then, look up your IP address like so:
|
12
|
+
|
13
|
+
```
|
14
|
+
irb
|
15
|
+
require 'datacenter_lookup'
|
16
|
+
dc = DatacenterLookup::Parser.new()
|
17
|
+
ip = "5.79.26.0"
|
18
|
+
puts dc.find(ip) # => "http://www.rackspace.com/"
|
19
|
+
```
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "ipaddr"
|
2
|
+
require "csv"
|
3
|
+
|
4
|
+
module DatacenterLookup
|
5
|
+
class Parser
|
6
|
+
def initialize
|
7
|
+
@starts = Array.new
|
8
|
+
@ends = Array.new
|
9
|
+
@urls = Array.new
|
10
|
+
File.open(DatacenterLookup::DefaultDatacentersPath, "r").readlines.map do |line|
|
11
|
+
add(*CSV.parse_line(line))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def add(start, stop, name, url=nil)
|
16
|
+
@starts << IPAddr.new(start).to_i
|
17
|
+
@ends << IPAddr.new(stop).to_i
|
18
|
+
@urls << url
|
19
|
+
end
|
20
|
+
|
21
|
+
def length
|
22
|
+
@starts.length
|
23
|
+
end
|
24
|
+
|
25
|
+
def find(ipstring)
|
26
|
+
ip = IPAddr.new(ipstring).to_i
|
27
|
+
high = length
|
28
|
+
low = 0
|
29
|
+
while high >= low do
|
30
|
+
probe = ((high+low)/2).floor.to_i
|
31
|
+
if @starts[probe] > ip
|
32
|
+
high = probe - 1
|
33
|
+
elsif @ends[probe] < ip
|
34
|
+
low = probe + 1
|
35
|
+
else
|
36
|
+
return @urls[probe]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
return nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|