hostip 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  == hostip
2
2
 
3
3
  A simple Ruby Gem wrapper for hostip.info
4
-
4
+ Includes a command line tool hostip to get all information from the command line as well
5
5
  == example
6
6
 
7
7
  require 'rubygems'
@@ -51,4 +51,4 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
51
 
52
52
  The views and conclusions contained in the software and documentation are those of the
53
53
  authors and should not be interpreted as representing official policies, either expressed
54
- or implied, of Philipp Fehre.
54
+ or implied, of Philipp Fehre.
data/Rakefile CHANGED
@@ -7,12 +7,12 @@ require 'rubygems/specification'
7
7
 
8
8
  spec = Gem::Specification.new do |s|
9
9
  s.name = "hostip"
10
- s.version = "0.1.0"
10
+ s.version = "0.2.0"
11
11
  s.authors = ['Philipp Fehre']
12
12
  s.email = "philipp.fehre@googlemail.com"
13
13
  s.homepage = "http://bitbucket.org/sideshowcoder/hostip-gem/"
14
14
  s.description = s.summary = "A simple Ruby wrapper for hostip.info"
15
- s.summary = "Get geolocation, ip, country and city information for current or any other ip form hostip.info"
15
+ s.summary = "Get geolocation, ip, country and city information for current or any other ip from hostip.info"
16
16
 
17
17
  s.platform = Gem::Platform::RUBY
18
18
  s.has_rdoc = true
@@ -20,8 +20,10 @@ spec = Gem::Specification.new do |s|
20
20
 
21
21
  s.require_path = 'lib'
22
22
  s.autorequire = 'hostip'
23
- s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,tests}/*")
23
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,tests,bin}/*")
24
24
 
25
+ s.bindir = 'bin'
26
+ s.executables = ['hostip']
25
27
  s.test_files = Dir.glob('tests/*.rb')
26
28
  s.add_dependency('httparty')
27
29
  end
@@ -0,0 +1,191 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # == Synopsis
4
+ # Get info from hostip.info service about current IP and Geo Location
5
+ #
6
+ # == Examples
7
+ # Get ip
8
+ # hostip
9
+ # Get city
10
+ # hostip --city
11
+ # Get longtitude and latitude
12
+ # hostip --geo
13
+ #
14
+ # Also works for any given IP
15
+ # hostip --city 123.456.789.1
16
+ #
17
+ # == Usage
18
+ # hostip [options] [ip]
19
+ #
20
+ # For help use: hostip -h
21
+ #
22
+ # == Options
23
+ # -h, --help Displays help message
24
+ # -v, --version Display version
25
+ # -V, --verbose Be more verbose
26
+ # --city Get city (Munich)
27
+ # --geo Get longtitude and latitude (12.4567,12.2345)
28
+ # --country Get country (ie Germany)
29
+ # --country-abbrev Get country abbreviation (ie US)
30
+ #
31
+ # == Author
32
+ # Philipp Fehre (philipp.fehre@googlemail.com)
33
+ #
34
+ # == Copyright
35
+ # (the BSD license)
36
+ #
37
+ # Copyright 2010 Philipp Fehre. All rights reserved.
38
+ #
39
+ # Redistribution and use in source and binary forms, with or without modification, are
40
+ # permitted provided that the following conditions are met:
41
+ #
42
+ # 1. Redistributions of source code must retain the above copyright notice, this list of
43
+ # conditions and the following disclaimer.
44
+ #
45
+ # 2. Redistributions in binary form must reproduce the above copyright notice, this list
46
+ # of conditions and the following disclaimer in the documentation and/or other materials
47
+ # provided with the distribution.
48
+ #
49
+ # THIS SOFTWARE IS PROVIDED BY PHILIPP FEHRE ``AS IS'' AND ANY EXPRESS OR IMPLIED
50
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
51
+ # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
52
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
55
+ # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
56
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
57
+ # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58
+ #
59
+ # The views and conclusions contained in the software and documentation are those of the
60
+ # authors and should not be interpreted as representing official policies,
61
+ # either expressed or implied, of Philipp Fehre.
62
+ #
63
+
64
+
65
+ require 'optparse'
66
+ require 'rdoc/usage'
67
+ require 'ostruct'
68
+ require 'date'
69
+ require 'hostip'
70
+ require 'ipaddr'
71
+
72
+ class App
73
+ VERSION = '0.2.0'
74
+
75
+ attr_reader :options
76
+
77
+ def initialize(arguments)
78
+ @arguments = arguments
79
+
80
+ # Set defaults
81
+ @options = OpenStruct.new
82
+ @options.verbose = false
83
+ @options.city = false
84
+ @options.geo = false
85
+ @options.country = false
86
+ @options.abbrev = false
87
+ @options.ip = nil
88
+ end
89
+
90
+ # Parse options, check arguments, then process the command
91
+ def run
92
+
93
+ if parsed_options? && arguments_valid?
94
+
95
+ puts "Start at #{DateTime.now}\n\n" if @options.verbose
96
+
97
+ process_command
98
+
99
+ puts "\nFinished at #{DateTime.now}" if @options.verbose
100
+
101
+ else
102
+ output_usage
103
+ end
104
+
105
+ end
106
+
107
+ protected
108
+
109
+ def parsed_options?
110
+
111
+ # Specify options
112
+ opts = OptionParser.new
113
+
114
+ opts.on('-v', '--version') { output_version ; exit 0 }
115
+ opts.on('-h', '--help') { output_help }
116
+ opts.on('-V', '--verbose') { @options.verbose = true }
117
+ opts.on('--city') { @options.city = true }
118
+ opts.on('--geo') { @options.geo = true }
119
+ opts.on('--country') { @options.country = true }
120
+ opts.on('--country-abbrev') { @options.abbrev = true }
121
+
122
+ opts.parse!(@arguments) rescue return false
123
+
124
+ process_options
125
+
126
+ true
127
+ end
128
+
129
+ # Performs post-parse processing on options
130
+ def process_options
131
+
132
+ end
133
+
134
+ # True if required arguments were provided
135
+ def arguments_valid?
136
+ # Check if IP is passed and IP is a valid IP
137
+ begin
138
+ if @arguments[0] && IPAddr.new(@arguments[0])
139
+ @options.ip = @arguments[0]
140
+ return true
141
+ end
142
+ rescue Exception
143
+ return false
144
+ end
145
+ # No IP passed, also good!
146
+ true
147
+ end
148
+
149
+ def output_help
150
+ output_version
151
+ RDoc::usage() #exits app
152
+ end
153
+
154
+ def output_usage
155
+ RDoc::usage('usage') # gets usage from comments above and exit
156
+ end
157
+
158
+ def output_version
159
+ puts "#{File.basename(__FILE__)} version #{VERSION}"
160
+ end
161
+
162
+ def process_command
163
+ hostip = Hostip.new
164
+
165
+ unless @options.ip
166
+ @options.ip = hostip.ip
167
+ puts @options.ip
168
+ end
169
+ if @options.city
170
+ puts hostip.city(@options.ip)
171
+ end
172
+ if @options.geo
173
+ puts "#{hostip.geo_location(@options.ip)["lat"]} #{hostip.geo_location(@options.ip)["long"]}"
174
+ end
175
+ if @options.country
176
+ puts hostip.country_name(@options.ip)
177
+ end
178
+ if @options.abbrev
179
+ puts hostip.country_abbrev(@options.ip)
180
+ end
181
+
182
+ end
183
+
184
+ end
185
+
186
+
187
+
188
+ # Create and run the application
189
+ app = App.new(ARGV)
190
+ app.run
191
+
@@ -66,4 +66,4 @@ class Hostip
66
66
 
67
67
  end
68
68
 
69
- end
69
+ end
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require "test_helper"
3
4
  require "test/unit"
4
5
  require "hostip"
5
6
  require "ipaddr"
@@ -32,5 +33,5 @@ class HostipTest < Test::Unit::TestCase
32
33
  def test_geo_location
33
34
  assert_equal(@hip.geo_location("66.102.13.103"), {"lat"=>"37.402", "long"=>"-122.078"})
34
35
  end
35
-
36
- end
36
+
37
+ end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "test_helper"
4
+ require "test/unit"
5
+ require "hostip"
6
+ require "ipaddr"
7
+
8
+
9
+ class HostipiBinTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @hostipbin = "../bin/hostip"
13
+ end
14
+
15
+ def test_get_ip
16
+ assert_nothing_raised() do
17
+ IPAddr.new(%x[ruby ../bin/hostip].chomp)
18
+ end
19
+ end
20
+
21
+ def test_city
22
+ city = %x[ruby ../bin/hostip --city 74.125.77.147].chomp
23
+ assert_equal(city, "Buren")
24
+ end
25
+
26
+ def test_multiple
27
+ mult = %x[ruby ../bin/hostip --geo --city --country 74.125.77.147].chomp
28
+ exp_res = "Buren\n51.9167 5.3333\nNETHERLANDS"
29
+ assert_equal(mult, exp_res)
30
+ end
31
+
32
+ def test_country
33
+ country = %x[ruby ../bin/hostip --country 74.125.77.147].chomp
34
+ assert_equal(country, "NETHERLANDS")
35
+ end
36
+
37
+ def test_country_abbrev
38
+ abbrev = %x[ruby ../bin/hostip --country-abbrev 74.125.77.147].chomp
39
+ assert_equal(abbrev, "NL")
40
+ end
41
+
42
+ def test_geo_location
43
+ geo = %x[ruby ../bin/hostip --geo 74.125.77.147].chomp
44
+ assert_equal(geo, "51.9167 5.3333")
45
+ end
46
+
47
+ end
48
+
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
3
+ $LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../bin' )
4
+
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require "test/unit"
3
+ require "tc_hostip"
4
+ require "tc_hostipbin"
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Philipp Fehre
@@ -14,7 +14,7 @@ autorequire: hostip
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-16 00:00:00 +01:00
17
+ date: 2010-04-01 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -31,8 +31,8 @@ dependencies:
31
31
  version_requirements: *id001
32
32
  description: A simple Ruby wrapper for hostip.info
33
33
  email: philipp.fehre@googlemail.com
34
- executables: []
35
-
34
+ executables:
35
+ - hostip
36
36
  extensions: []
37
37
 
38
38
  extra_rdoc_files:
@@ -42,6 +42,10 @@ files:
42
42
  - Rakefile
43
43
  - lib/hostip.rb
44
44
  - tests/tc_hostip.rb
45
+ - tests/tc_hostipbin.rb
46
+ - tests/test_helper.rb
47
+ - tests/ts_alltests.rb
48
+ - bin/hostip
45
49
  has_rdoc: true
46
50
  homepage: http://bitbucket.org/sideshowcoder/hostip-gem/
47
51
  licenses: []
@@ -71,6 +75,9 @@ rubyforge_project:
71
75
  rubygems_version: 1.3.6
72
76
  signing_key:
73
77
  specification_version: 3
74
- summary: Get geolocation, ip, country and city information for current or any other ip form hostip.info
78
+ summary: Get geolocation, ip, country and city information for current or any other ip from hostip.info
75
79
  test_files:
76
80
  - tests/tc_hostip.rb
81
+ - tests/tc_hostipbin.rb
82
+ - tests/test_helper.rb
83
+ - tests/ts_alltests.rb