datx_ruby 0.0.4

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4151cf0104952cf139dfeafdbc1b173e99932951
4
+ data.tar.gz: e5710aeb6b8bd8f22e1198002dee2cd099f2aea6
5
+ SHA512:
6
+ metadata.gz: fa2cf360bf1e4a97646cba96938f8ba4996fe219b281c0e2228e3772fa2d822c629f9804564c97d6fc2e12cf44ca371f1287922c45e81c654c91b755f34fd071
7
+ data.tar.gz: af5456cfa0663449907bceed5b92472a0e5ee5badc135151515a2ae51b9c41272e2184663aee5c11dcfa9d64ff074b416a84679191be48a4fde842daa9524e83
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .ruby-version
6
+
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ coverage
10
+ doc/
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ vendor
16
+ tmp
17
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://gems.ruby-china.com'
2
+
3
+ # Specify your gem's dependencies in seventeen_mom.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rake'
8
+ gem 'rubygems-tasks', '~> 0.2'
9
+
10
+ gem 'rspec', '~> 2.4'
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 limx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # DatxRuby
2
+
3
+ DatxRuby simply help you find location by IP address.
4
+
5
+ ## Compatibility
6
+
7
+ I have tested it on Ruby >= 1.9.3. Other versions are not tested but should work on well. Please contact me if not.
8
+
9
+ ## Installation
10
+
11
+ gem install datx_ruby
12
+
13
+
14
+ OR add this line to your application's Gemfile:
15
+
16
+ gem 'datx_ruby'
17
+
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+
24
+ ## Usage
25
+
26
+ ### In Ruby
27
+ ```(ruby)
28
+ require 'datx_ruby'
29
+ DatxRuby.city_datx_path= "/datapath/ipipnet.datx"
30
+ DatxRuby.city_find "106.75.109.221"
31
+ # => ["中国", "北京", "北京", "ucloud.cn", "联通"]
32
+ ```
33
+ ### In Rails
34
+ You need to do is set DatxRuby.city_datx_path= "/datapath/ipipnet.datx" inside the config/initializers/datx_ruby.rb file.
35
+
36
+
37
+ [Report bugs][issues] on GitHub.
38
+
39
+
40
+ [issues]: https://github.com/limanxian/datx-ruby/issues
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run the test suite"
7
+ RSpec::Core::RakeTask.new(:rspec) do |t|
8
+ t.pattern = FileList['spec/**/*_spec.rb']
9
+ t.rspec_opts = %w|--color|
10
+ end
11
+
12
+ task default: :spec
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'datx_ruby/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{datx_ruby}
8
+ s.version = DatxRuby::VERSION
9
+ s.date = %q{2018-10-23}
10
+ s.summary = %q{A Ruby library to parse ipipnet datx file}
11
+ s.description = %q{A Ruby library to parse ipipnet datx file}
12
+ s.authors = ["limanxian"]
13
+ s.email = ["limanxian_321@126.com"]
14
+ s.files = `git ls-files`.split("\n")
15
+
16
+ s.homepage = "https://github.com/limanxian/datx-ruby"
17
+ s.license = "MIT"
18
+
19
+ s.require_paths = ["lib"]
20
+ end
Binary file
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+
3
+ # 解析ipip.net库, city_find:查找ip的城市, district_find: 查找ip的区县
4
+ module DatxRuby
5
+ require "ipaddr"
6
+ require "datx_ruby/version"
7
+ require "datx_ruby/util"
8
+ require "datx_ruby/common"
9
+ require "datx_ruby/district"
10
+ require "datx_ruby/city"
11
+
12
+ # set city datx file path
13
+ # Example:
14
+ # >> DatxRuby.city_datx_path= "/path/data/17monipdb.datx"
15
+ # => /path/data/17monipdb.datx
16
+ # Arguments:
17
+ # path: (String)
18
+ def self.city_datx_path=(path)
19
+ City.datax_path= path
20
+ end
21
+
22
+ # set district datx file path
23
+ # Example:
24
+ # >> DatxRuby.district_datx_path= "/path/data/17monipdb.datx"
25
+ # => /path/data/17monipdb.datx
26
+ # Arguments:
27
+ # path: (String)
28
+ def self.district_datx_path=(path)
29
+ District.datax_path= path
30
+ end
31
+
32
+ # find a ip's location
33
+ # Example:
34
+ # >> DatxRuby.city_find("106.75.109.221")
35
+ # => ["中国", "北京", "北京"]
36
+ # Arguments:
37
+ # ip: (String)
38
+ def self.city_find(ip)
39
+ City.new.find(ip)
40
+ end
41
+
42
+ # find a ip's district
43
+ # Example:
44
+ # >> DatxRuby.district_find("106.75.109.221")
45
+ # => ["中国", "北京", "北京"]
46
+ # Arguments:
47
+ # ip: (String)
48
+ def self.district_find(ip)
49
+ District.new.find(ip)
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+ module DatxRuby
3
+
4
+ class City
5
+
6
+ include DatxRuby::Common
7
+
8
+ def initialize
9
+ @data = IO.binread(datx)
10
+ @index_size = Util.bytes2long(@data[0], @data[1], @data[2], @data[3])
11
+ end
12
+
13
+ def self.datax_path=(path)
14
+ $datx_path ||= path
15
+ end
16
+
17
+ def find(ip)
18
+ raise "Invalid IP address" unless ::IPAddr.new(ip).ipv4?
19
+ low = 0
20
+ high = (index_size - 262144 - 262148) / 9 - 1
21
+ val = Util.ip2long(ip)
22
+ while low <= high do
23
+ mid = (low + high) / 2
24
+ pos = mid * 9 + 262148
25
+ starts = 0
26
+ mid_new = mid - 1
27
+ if mid > 0
28
+ pos_new = mid_new * 9 + 262148
29
+ starts = Util.bytes2long(@data[pos_new], @data[pos_new + 1], @data[pos_new + 2], @data[pos_new + 3])
30
+ starts += 1
31
+ end
32
+ ends = Util.bytes2long(@data[pos], @data[pos + 1], @data[pos + 2], @data[pos + 3])
33
+ if val < starts
34
+ high = mid_new
35
+ elsif val > ends
36
+ low = mid + 1
37
+ else
38
+ empty = [0].pack('C*')
39
+ off = Util.bytes2long(empty, @data[pos + 6], @data[pos + 5], @data[pos + 4])
40
+ length = Util.bytes2long(empty, empty, @data[pos + 7], @data[pos + 8])
41
+ pos = off - 262144 + index_size
42
+ tmp = @data[pos...pos + length].force_encoding('utf-8')
43
+ return tmp.split("\t")
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,16 @@
1
+ module DatxRuby
2
+ module Common
3
+
4
+ def datx_path(path = (File.expand_path '../../data/17monipdb.datx', __FILE__))
5
+ $datx_path ||= path
6
+ end
7
+
8
+ def datx
9
+ @datx ||= File.open datx_path, 'rb'
10
+ end
11
+
12
+ def index_size
13
+ @index_size
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ module DatxRuby
2
+
3
+ class District
4
+
5
+ include DatxRuby::Common
6
+
7
+ def initialize
8
+ @data = IO.binread(datx)
9
+ @index_size = Util.bytes2long(@data[0], @data[1], @data[2], @data[3])
10
+ end
11
+
12
+ def self.datax_path=(path)
13
+ $datx_path ||= path
14
+ end
15
+
16
+ def find(ip)
17
+ raise "Invalid IP address" unless ::IPAddr.new(ip).ipv4?
18
+
19
+ low = 0
20
+ high = (index_size - 262148 - 262144) / 13 - 1
21
+ val = Util.ip2long(ip)
22
+
23
+ while low <= high do
24
+ mid = (low + high) / 2
25
+ pos = mid * 13 + 262148
26
+
27
+ starts = Util.bytes2long(@data[pos], @data[pos + 1], @data[pos + 2], @data[pos + 3])
28
+ ends = Util.bytes2long(@data[pos+4], @data[pos + 5], @data[pos + 6], @data[pos + 7])
29
+
30
+ if val > ends
31
+ low = mid + 1
32
+ elsif val < starts
33
+ high = mid - 1
34
+ else
35
+ off = Util.bytes2long(@data[pos + 11], @data[pos + 10], @data[pos + 9], @data[pos + 8])
36
+ l = @data[pos + 12].to_i
37
+ pos = off - 262144 + index_size
38
+
39
+ tmp = @data[pos...pos + l].force_encoding('utf-8')
40
+ return tmp.split("\t")
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,26 @@
1
+ module DatxRuby
2
+
3
+ # 工具
4
+ class Util
5
+
6
+ # class method
7
+ class << self
8
+ def ip2long(ip)
9
+ ::IPAddr.new(ip).to_i
10
+ end
11
+
12
+ def bytes2long(four, three, two, one)
13
+ four = check_code(four, 24)
14
+ three = check_code(three, 16)
15
+ two = check_code(two, 8)
16
+ one = check_code(one, 0)
17
+ four | three | two | one
18
+ end
19
+
20
+ def check_code(content, num)
21
+ content.unpack("C*")[0] << num
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module DatxRuby
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe DatxRuby do
5
+
6
+ describe "# query test" do
7
+ before :all do
8
+ @path = "/home/limx/myfiles/github/datx-ruby/lib/data/17monipdb.datx"
9
+ DatxRuby.city_datx_path = @path
10
+ end
11
+
12
+ it "path is setted" do
13
+ expect(DatxRuby::City.new.datx_path).to eq(@path)
14
+ end
15
+
16
+ it "can find location by city" do
17
+ result = DatxRuby.city_find("106.75.109.221")
18
+ expect(result).to eq(["中国", "北京", "北京"])
19
+ end
20
+
21
+ it "can find location by district" do
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require "datx_ruby"
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ config.order = 'random'
9
+
10
+ config.color_enabled = true
11
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datx_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - limanxian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby library to parse ipipnet datx file
14
+ email:
15
+ - limanxian_321@126.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - datx-ruby.gemspec
26
+ - lib/data/17monipdb.datx
27
+ - lib/datx_ruby.rb
28
+ - lib/datx_ruby/city.rb
29
+ - lib/datx_ruby/common.rb
30
+ - lib/datx_ruby/district.rb
31
+ - lib/datx_ruby/util.rb
32
+ - lib/datx_ruby/version.rb
33
+ - spec/datx_ruby_spec.rb
34
+ - spec/spec_helper.rb
35
+ homepage: https://github.com/limanxian/datx-ruby
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.5.1
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: A Ruby library to parse ipipnet datx file
59
+ test_files: []