ipparse 0.0.5 → 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.
data/README CHANGED
@@ -1,6 +1,17 @@
1
1
  = IPParse
2
+ == Install
3
+ $ sudo gem install ipparse
4
+
2
5
  == Usage
6
+ require 'rubygems'
7
+ require 'ipparse'
3
8
  IPParse.parse('218.58.88.175')
4
9
 
5
10
  == Shell
6
11
  $ ipparse 218.58.88.175 212.1.1.1 192.245.148.0 116.254.254.0
12
+
13
+
14
+ Copyright (c) 2009 MIT-LICENSE
15
+ Author : Jinzhu Zhang
16
+ Web : http://www.zhangjinzhu.com
17
+ Email : wosmvp (no-spam) gmail (no-spam) com
data/bin/ipparse CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # Copyright (c) 2009. GPL3.
3
+ # Copyright (c) 2009. MIT-LICENSE.
4
4
  # Author: Zhang Jinzhu
5
5
 
6
6
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
data/ipparse.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{ipparse}
5
- s.version = "0.0.5"
5
+ s.version = "0.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Zhang Jinzhu"]
9
- s.date = %q{2009-02-19}
9
+ s.date = %q{2009-02-22}
10
10
  s.default_executable = %q{ipparse}
11
11
  s.description = %q{IP Parse}
12
12
  s.email = %q{wosmvp@gmail.com}
data/lib/ipparse.rb CHANGED
@@ -5,19 +5,9 @@ class IPParse
5
5
 
6
6
  def self.parse(ip)
7
7
  return false unless ip.to_s =~ /(\d+\.){3}\d+/
8
- ip,addr = format(ip) , ''
8
+ ip = ip.scan(/\d+/).map{|x| x.rjust(3, '0')}.join('.')
9
9
 
10
- [ip[0,3].to_i,0].each do |f|
11
- file = "#{File.dirname(__FILE__)}/../data/#{f.to_s}.txt"
12
- @@data[f] ||= File.exist?(file) ? File.open(file){|m| m.to_a} : false
13
-
14
- if @@data[f]
15
- addr = dichotomizing(@@data[f],ip)
16
- return addr if addr
17
- end
18
- end
19
-
20
- return "UNKNOW"
10
+ return dichotomizing(@@data[ip[0,3].to_i] ||= file_to_a("#{File.dirname(__FILE__)}/../data/#{ip[0,3].to_i}.txt"),ip) || dichotomizing(@@data[0] ||= file_to_a("#{File.dirname(__FILE__)}/../data/0.txt"),ip) || "Unknown"
21
11
  end
22
12
 
23
13
  protected
@@ -25,13 +15,14 @@ class IPParse
25
15
  cen = arg.size/2
26
16
  cur = arg[cen]
27
17
 
28
- return (cur[0,15]..cur[16,15]).include?(ip) ? cur[32...-1] : false if cen == 0
18
+ # arg.size = 0 || 1
19
+ return (cur && (cur[0,15]..cur[16,15]).include?(ip)) ? cur[32...-1] : false if cen == 0
29
20
  return dichotomizing(arg[0...cen],ip) if cur[0,15] > ip
30
- return dichotomizing(arg[cen..-1],ip) if cur[16,15] < ip
21
+ return dichotomizing(arg[cen+1..-1],ip) if cur[16,15] < ip
31
22
  return arg[cen][32...-1]
32
23
  end
33
24
 
34
- def self.format(ip)
35
- ip.to_s.scan(/\d+/).map{|x| x.rjust(3, '0')}.join('.')
25
+ def self.file_to_a(f)
26
+ File.exist?(f) ? File.open(f){|m| m.to_a} : []
36
27
  end
37
28
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module IPParse
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
data/test/test_ipparse.rb CHANGED
@@ -13,12 +13,9 @@ class TestIPParse < Test::Unit::TestCase
13
13
  "116.254.254.0" => "广东省广州市",
14
14
  "16.34.110.150" => "美国",
15
15
  "222.15.153.3" => "江西省云南市",
16
- "24.38.192.2" => "美国"
16
+ "24.38.192.2" => "美国",
17
+ "222.249.31.53" => "北京市"
17
18
  }
18
19
  ips.map {|k,v| assert_equal IPParse.parse(k),v}
19
20
  end
20
-
21
- def test_format
22
- assert_equal IPParse.format('12.3.4.23'),'012.003.004.023'
23
- end
24
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipparse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhang Jinzhu
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-19 00:00:00 +08:00
12
+ date: 2009-02-22 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15