hostip 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/README.rdoc +54 -0
  2. data/Rakefile +35 -0
  3. data/lib/hostip.rb +69 -0
  4. data/tests/tc_hostip.rb +36 -0
  5. metadata +76 -0
data/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ == hostip
2
+
3
+ A simple Ruby Gem wrapper for hostip.info
4
+
5
+ == example
6
+
7
+ require 'rubygems'
8
+ require 'hostip'
9
+
10
+ hip = Hostip.new
11
+
12
+ # get current ip
13
+ hip.ip
14
+
15
+ # get current country
16
+ hip.country_name
17
+ # get country abbriviated
18
+ hip.country_abbrev
19
+ # get current geo location
20
+ hip.geo_location
21
+
22
+ # all this also works for a given ip like 74.125.77.104 (google.com)
23
+ hip.geo_location "74.125.77.104"
24
+ hip.country_name "74.125.77.104"
25
+
26
+ == license
27
+
28
+ (the BSD license)
29
+
30
+ Copyright 2010 Philipp Fehre. All rights reserved.
31
+
32
+ Redistribution and use in source and binary forms, with or without modification, are
33
+ permitted provided that the following conditions are met:
34
+
35
+ 1. Redistributions of source code must retain the above copyright notice, this list of
36
+ conditions and the following disclaimer.
37
+
38
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
39
+ of conditions and the following disclaimer in the documentation and/or other materials
40
+ provided with the distribution.
41
+
42
+ THIS SOFTWARE IS PROVIDED BY PHILIPP FEHRE ``AS IS'' AND ANY EXPRESS OR IMPLIED
43
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
44
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
45
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
48
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
50
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
+
52
+ The views and conclusions contained in the software and documentation are those of the
53
+ authors and should not be interpreted as representing official policies, either expressed
54
+ or implied, of Philipp Fehre.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
4
+ require 'rake/gempackagetask'
5
+ require 'rubygems/specification'
6
+
7
+
8
+ spec = Gem::Specification.new do |s|
9
+ s.name = "hostip"
10
+ s.version = "0.1.0"
11
+ s.authors = ['Philipp Fehre']
12
+ s.email = "philipp.fehre@googlemail.com"
13
+ s.homepage = "http://bitbucket.org/sideshowcoder/hostip-gem/"
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"
16
+
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README.rdoc"]
20
+
21
+ s.require_path = 'lib'
22
+ s.autorequire = 'hostip'
23
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,tests}/*")
24
+
25
+ s.test_files = Dir.glob('tests/*.rb')
26
+ s.add_dependency('httparty')
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.need_tar = true
31
+ end
32
+
33
+ task :default => "pkg/#{spec.name}-#{spec.version}.gem" do
34
+ puts "generated latest version"
35
+ end
data/lib/hostip.rb ADDED
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "httparty"
5
+
6
+ class Hostip
7
+ include HTTParty
8
+ base_uri('api.hostip.info')
9
+ format :xml
10
+
11
+ # get country name for ip, if no ip is passed use own ip
12
+ def country_name(ip=nil)
13
+ self.class.request(ip)["countryName"]
14
+ end
15
+
16
+ # get country abbreviation for ip, if no ip is passed use own ip
17
+ def country_abbrev(ip=nil)
18
+ self.class.request(ip)["countryAbbrev"]
19
+ end
20
+
21
+ # get city name for ip, if no ip is passed use own ip
22
+ def city(ip=nil)
23
+ self.class.request(ip)["gml:name"]
24
+ end
25
+
26
+ # get current ip
27
+ def ip
28
+ self.class.request["ip"]
29
+ end
30
+
31
+ # Returns a hash with
32
+ # long => "xxxx" and lat => "xxx"
33
+ # or raise exception if location is unknown
34
+ def geo_location(ip=nil)
35
+ begin
36
+ # Get Comma seperated coordinates and return as hash
37
+ coordinates = self.class.request(ip, true)["ipLocation"]["gml:pointProperty"]["gml:Point"]["gml:coordinates"].split(',')
38
+ return { "long" => coordinates[0], "lat" => coordinates[1] }
39
+ rescue
40
+ raise "geo location unknown"
41
+ end
42
+ end
43
+
44
+ class << self
45
+
46
+ # Make a request and strip unwanted XML before returning result
47
+ # options:
48
+ # ip => xxx.xxx.xxx.xxx
49
+ # postion => true (Documented but does nothing!)
50
+ def request(ip=nil, position=false)
51
+ # construct params hash
52
+ params = {}
53
+ if ip != nil
54
+ params["ip"] = ip
55
+ end
56
+ if position
57
+ params["position"] = "true"
58
+ end
59
+ # sent request
60
+ if params == {}
61
+ self.get('/get_xml.php')["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]
62
+ else
63
+ self.get('/get_xml.php', :query => params)["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "test/unit"
4
+ require "hostip"
5
+ require "ipaddr"
6
+
7
+
8
+ class HostipTest < Test::Unit::TestCase
9
+
10
+ def setup
11
+ @hip = Hostip.new
12
+ end
13
+
14
+ def test_ip
15
+ assert_nothing_raised() do
16
+ IPAddr.new(@hip.ip)
17
+ end
18
+ end
19
+
20
+ def test_country_name
21
+ assert_equal(@hip.country_name("66.102.13.103"), "UNITED STATES")
22
+ end
23
+
24
+ def test_country_abbrev
25
+ assert_equal(@hip.country_abbrev("66.102.13.103"), "US")
26
+ end
27
+
28
+ def test_city
29
+ assert_equal(@hip.city("66.102.13.103"), "Mountain View, CA")
30
+ end
31
+
32
+ def test_geo_location
33
+ assert_equal(@hip.geo_location("66.102.13.103"), {"lat"=>"37.402", "long"=>"-122.078"})
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hostip
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Philipp Fehre
13
+ autorequire: hostip
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-16 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: A simple Ruby wrapper for hostip.info
33
+ email: philipp.fehre@googlemail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README.rdoc
40
+ files:
41
+ - README.rdoc
42
+ - Rakefile
43
+ - lib/hostip.rb
44
+ - tests/tc_hostip.rb
45
+ has_rdoc: true
46
+ homepage: http://bitbucket.org/sideshowcoder/hostip-gem/
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Get geolocation, ip, country and city information for current or any other ip form hostip.info
75
+ test_files:
76
+ - tests/tc_hostip.rb