hostipinfo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +6 -0
- data/MIT-LICENSE +21 -0
- data/README +35 -0
- data/Rakefile +158 -0
- data/lib/host_ip_info.rb +36 -0
- data/test/test_host_ip_info.rb +51 -0
- metadata +55 -0
data/CHANGES
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2005 Aslak Hellesoy
|
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
|
+
|
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
= HostIpInfo
|
2
|
+
|
3
|
+
HostIpInfo is get host infomation from ip address.
|
4
|
+
(using http://hostip.info)
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
$ sudo gem install hostipinfo
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
|
12
|
+
require 'rubygems'
|
13
|
+
require 'host_ip_info'
|
14
|
+
|
15
|
+
h = HostIpInfo.new
|
16
|
+
ip = '12.215.42.19'
|
17
|
+
info = h.info(ip)
|
18
|
+
|
19
|
+
info['country']
|
20
|
+
#=> 'UNITED STATES'
|
21
|
+
info['country_code']
|
22
|
+
#=> 'US'
|
23
|
+
info['city']
|
24
|
+
#=> 'Sugar Grove'
|
25
|
+
info['region']
|
26
|
+
#=> 'IL'
|
27
|
+
info['latitude']
|
28
|
+
#=> 41.7696
|
29
|
+
info['longitude']
|
30
|
+
#=> -88.4588
|
31
|
+
|
32
|
+
Author:: gorou ( http://rails2u.com ) <hotchpotch@gmail.com.nospam>
|
33
|
+
Copyright:: Copyright (c) 2005 gorou
|
34
|
+
License:: MIT License
|
35
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
$:.unshift('lib')
|
2
|
+
require 'rubygems'
|
3
|
+
require 'meta_project'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/contrib/rubyforgepublisher'
|
6
|
+
require 'rake/contrib/xforge'
|
7
|
+
require 'rake/clean'
|
8
|
+
require 'rake/testtask'
|
9
|
+
require 'rake/rdoctask'
|
10
|
+
|
11
|
+
PKG_NAME = "hostipinfo"
|
12
|
+
# Versioning scheme: MAJOR.MINOR.PATCH
|
13
|
+
# MAJOR bumps when API is broken backwards
|
14
|
+
# MINOR bumps when the API is broken backwards in a very slight/subtle (but not fatal) way
|
15
|
+
# -OR when a new release is made and propaganda is sent out.
|
16
|
+
# PATCH is bumped for every API addition and/or bugfix (ideally for every commit)
|
17
|
+
# Later DamageControl can bump PATCH automatically.
|
18
|
+
#
|
19
|
+
# (This is subject to change - AH)
|
20
|
+
#
|
21
|
+
# REMEMBER TO KEEP PKG_VERSION IN SYNC WITH CHANGELOG
|
22
|
+
PKG_VERSION = "0.1.0"
|
23
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
24
|
+
PKG_FILES = FileList[
|
25
|
+
'[A-Z]*',
|
26
|
+
'lib/**/*.rb',
|
27
|
+
'test/**/*.rb',
|
28
|
+
'examples/**/*.rb',
|
29
|
+
'doc/**/*'
|
30
|
+
]
|
31
|
+
|
32
|
+
task :default => [:test]
|
33
|
+
|
34
|
+
Rake::TestTask.new(:test) do |t|
|
35
|
+
t.libs << "test"
|
36
|
+
t.test_files = FileList['**/test*.rb']
|
37
|
+
t.verbose = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# Create a task to build the RDOC documentation tree.
|
41
|
+
rd = Rake::RDocTask.new("rdoc") do |rdoc|
|
42
|
+
rdoc.rdoc_dir = 'html'
|
43
|
+
rdoc.title = "hostipinfo"
|
44
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'README'
|
45
|
+
rdoc.rdoc_files.include('README', 'CHANGES')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
|
47
|
+
rdoc.rdoc_files.exclude('doc/**/*_attrs.rdoc')
|
48
|
+
end
|
49
|
+
|
50
|
+
# ====================================================================
|
51
|
+
# Create a task that will package the Rake software into distributable
|
52
|
+
# tar, zip and gem files.
|
53
|
+
|
54
|
+
spec = Gem::Specification.new do |s|
|
55
|
+
|
56
|
+
#### Basic information.
|
57
|
+
|
58
|
+
s.name = PKG_NAME
|
59
|
+
s.version = PKG_VERSION
|
60
|
+
s.summary = "get host infomation from ipaddress"
|
61
|
+
s.description = <<-EOF
|
62
|
+
get host infomation from ipaddress
|
63
|
+
(using http://hostip.info/ )
|
64
|
+
EOF
|
65
|
+
|
66
|
+
s.files = PKG_FILES.to_a
|
67
|
+
s.require_path = 'lib'
|
68
|
+
|
69
|
+
#### Documentation and testing.
|
70
|
+
|
71
|
+
s.has_rdoc = true
|
72
|
+
s.extra_rdoc_files = rd.rdoc_files.reject { |fn| fn =~ /\.rb$/ }.to_a
|
73
|
+
s.rdoc_options <<
|
74
|
+
'--title' << 'hostipinfo' <<
|
75
|
+
'--main' << 'README' <<
|
76
|
+
'--line-numbers'
|
77
|
+
|
78
|
+
s.test_files = Dir.glob('test/tc_*.rb')
|
79
|
+
|
80
|
+
#### Make executable
|
81
|
+
s.require_path = 'lib'
|
82
|
+
s.autorequire = 'spec'
|
83
|
+
|
84
|
+
#### Author and project details.
|
85
|
+
|
86
|
+
s.author = "gorou"
|
87
|
+
s.email = "hotchpotch@gmail.com"
|
88
|
+
s.homepage = "http://hostipinfo.rubyforge.org"
|
89
|
+
s.rubyforge_project = "hostipinfo"
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Build Gem"
|
93
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
94
|
+
pkg.need_zip = true
|
95
|
+
pkg.need_tar = true
|
96
|
+
end
|
97
|
+
task :gem => [:test]
|
98
|
+
|
99
|
+
# Support Tasks ------------------------------------------------------
|
100
|
+
|
101
|
+
def egrep(pattern)
|
102
|
+
Dir['**/*.rb'].each do |fn|
|
103
|
+
count = 0
|
104
|
+
open(fn) do |f|
|
105
|
+
while line = f.gets
|
106
|
+
count += 1
|
107
|
+
if line =~ pattern
|
108
|
+
puts "#{fn}:#{count}:#{line}"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
desc "Look for TODO and FIXME tags in the code"
|
116
|
+
task :todo do
|
117
|
+
egrep /#.*(FIXME|TODO|TBD)/
|
118
|
+
end
|
119
|
+
|
120
|
+
task :release => [:verify_env_vars, :release_files, :publish_doc, :publish_news]
|
121
|
+
|
122
|
+
task :verify_env_vars do
|
123
|
+
raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
|
124
|
+
raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
|
125
|
+
end
|
126
|
+
|
127
|
+
task :publish_doc => [:rdoc] do
|
128
|
+
publisher = Rake::RubyForgePublisher.new(PKG_NAME, ENV['RUBYFORGE_USER'])
|
129
|
+
publisher.upload
|
130
|
+
end
|
131
|
+
|
132
|
+
desc "Release gem to RubyForge. MAKE SURE PKG_VERSION is aligned with the CHANGELOG file"
|
133
|
+
task :release_files => [:gem] do
|
134
|
+
release_files = FileList[
|
135
|
+
"pkg/#{PKG_FILE_NAME}.gem"
|
136
|
+
]
|
137
|
+
|
138
|
+
Rake::XForge::Release.new(MetaProject::Project::XForge::RubyForge.new(PKG_NAME)) do |xf|
|
139
|
+
# Never hardcode user name and password in the Rakefile!
|
140
|
+
xf.user_name = ENV['RUBYFORGE_USER']
|
141
|
+
xf.password = ENV['RUBYFORGE_PASSWORD']
|
142
|
+
xf.files = release_files.to_a
|
143
|
+
xf.release_name = "HostIpInfo #{PKG_VERSION}"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
desc "Publish news on RubyForge"
|
148
|
+
task :publish_news => [:gem] do
|
149
|
+
release_files = FileList[
|
150
|
+
"pkg/#{PKG_FILE_NAME}.gem"
|
151
|
+
]
|
152
|
+
|
153
|
+
Rake::XForge::NewsPublisher.new(MetaProject::Project::XForge::RubyForge.new(PKG_NAME)) do |news|
|
154
|
+
# Never hardcode user name and password in the Rakefile!
|
155
|
+
news.user_name = ENV['RUBYFORGE_USER']
|
156
|
+
news.password = ENV['RUBYFORGE_PASSWORD']
|
157
|
+
end
|
158
|
+
end
|
data/lib/host_ip_info.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
class HostIpInfo
|
5
|
+
def info(ip)
|
6
|
+
ip.strip!
|
7
|
+
raise 'invalid ipaddress' unless valid_ipv4?(ip)
|
8
|
+
get ip
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
HostIpGetUrl = 'http://www.hostip.info/api/get.html?position=true&ip='
|
13
|
+
def get(ip)
|
14
|
+
url = HostIpGetUrl + ip
|
15
|
+
parse YAML::load(open(url){|i| i.read })
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse(data)
|
19
|
+
info = {}
|
20
|
+
info['country'], info['country_code'] = data['Country'].match(/\A(.+?)\s*?(?:\((\w+)\))?\Z/)[1..2]
|
21
|
+
info['city'], info['region'] = data['City'].split(/\s*,\s*/)
|
22
|
+
info['city'] = nil if info['city'].include?('(Unknown city)')
|
23
|
+
info['latitude'] = data['Latitude']
|
24
|
+
info['longitude'] = data['Longitude']
|
25
|
+
info
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid_ipv4?(ip)
|
29
|
+
if ip.match(/^(\d{1,3}\.){3}\d{1,3}$/) and
|
30
|
+
ip.split('.').reject!{|i| i.to_i < 256 }.empty?
|
31
|
+
true
|
32
|
+
else
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'host_ip_info'
|
3
|
+
|
4
|
+
class TestHostIpInfo < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@host_ip_info = HostIpInfo.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_ip_12_215_42_19
|
11
|
+
ip = '12.215.42.19'
|
12
|
+
|
13
|
+
assert_equal({
|
14
|
+
'country' => 'UNITED STATES',
|
15
|
+
'city' => 'Sugar Grove',
|
16
|
+
'country_code' => 'US',
|
17
|
+
'region' => 'IL',
|
18
|
+
'latitude' => 41.7696 ,
|
19
|
+
'longitude' => -88.4588
|
20
|
+
},
|
21
|
+
@host_ip_info.info(ip)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_ip_221_243_169_85
|
26
|
+
ip = '221.243.169.85'
|
27
|
+
|
28
|
+
assert_equal({
|
29
|
+
'country' => 'JAPAN',
|
30
|
+
'country_code' => 'JP',
|
31
|
+
'city' => nil,
|
32
|
+
'region' => nil,
|
33
|
+
'latitude' => nil,
|
34
|
+
'longitude' => nil
|
35
|
+
},
|
36
|
+
@host_ip_info.info(ip)
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_ip_invalid
|
41
|
+
ip1 = '291.243.169.85'
|
42
|
+
ip2 = 'a221.243.169.85'
|
43
|
+
assert_raises(RuntimeError) do
|
44
|
+
@host_ip_info.info(ip1)
|
45
|
+
end
|
46
|
+
assert_raises(RuntimeError) do
|
47
|
+
@host_ip_info.info(ip2)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
!ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: hostipinfo
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2005-10-13 00:00:00 +09:00
|
8
|
+
summary: get host infomation from ipaddress
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: hotchpotch@gmail.com
|
12
|
+
homepage: http://hostipinfo.rubyforge.org
|
13
|
+
rubyforge_project: hostipinfo
|
14
|
+
description: get host infomation from ipaddress (using http://hostip.info/ )
|
15
|
+
autorequire: spec
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- gorou
|
30
|
+
files:
|
31
|
+
- MIT-LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- README
|
34
|
+
- CHANGES
|
35
|
+
- lib/host_ip_info.rb
|
36
|
+
- test/test_host_ip_info.rb
|
37
|
+
test_files: []
|
38
|
+
|
39
|
+
rdoc_options:
|
40
|
+
- --title
|
41
|
+
- hostipinfo
|
42
|
+
- --main
|
43
|
+
- README
|
44
|
+
- --line-numbers
|
45
|
+
extra_rdoc_files:
|
46
|
+
- README
|
47
|
+
- CHANGES
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
dependencies: []
|
55
|
+
|