phonedata 0.3.0.2108 → 0.4.0.2302
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/data/phone.dat +0 -0
- data/lib/phonedata/version.rb +1 -1
- data/lib/phonedata.rb +30 -36
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ddac38cea34a21e3367dc7d5ed4c6b85a2eb4686ffeb1f0ba4e7ef03e292221
|
4
|
+
data.tar.gz: f9fcdb6cd1c66c2b1888b1625cec683c7603530a0b2ce3750c0584f1087c6a65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3bf3c86b67568f897bb07e40fc105c9af314ae0741f5140ad70bcd60b5374924eb068f124406bc0a0fc9329dbf9dda94d9fb7888fbd66ba6e80b9ed2e493808
|
7
|
+
data.tar.gz: 00c6931c52dffc29e3e9f93e44822b978eec7c39cff123ea5da51d68c8b6ce42e75dae3442beefb9d41933a73cc16a006dbabc9c0d49ecc22abf87b4854bcce5
|
data/README.md
CHANGED
data/data/phone.dat
CHANGED
Binary file
|
data/lib/phonedata/version.rb
CHANGED
data/lib/phonedata.rb
CHANGED
@@ -1,38 +1,37 @@
|
|
1
1
|
require 'singleton'
|
2
|
-
require
|
2
|
+
require 'phonedata/version'
|
3
3
|
|
4
4
|
module Phonedata
|
5
5
|
class Error < StandardError; end
|
6
|
-
|
7
|
-
|
6
|
+
|
8
7
|
def self.find(phone_num)
|
9
|
-
|
8
|
+
Pd.instance.find(phone_num)
|
10
9
|
end
|
11
10
|
|
12
11
|
def self.find_from_cli(phone_num)
|
13
|
-
result =
|
12
|
+
result = find(phone_num)
|
14
13
|
result.to_s
|
15
14
|
end
|
16
15
|
|
17
16
|
def self.file_version
|
18
|
-
|
17
|
+
Pd.instance.file_version
|
19
18
|
end
|
20
19
|
|
21
20
|
def self.total_record
|
22
|
-
|
21
|
+
Pd.instance.total_record
|
23
22
|
end
|
24
23
|
|
25
24
|
def self.first_record_offset
|
26
|
-
|
25
|
+
Pd.instance.first_record_offset
|
27
26
|
end
|
28
|
-
|
27
|
+
|
29
28
|
INT_LEN = 4
|
30
29
|
CHAR_LEN = 1
|
31
30
|
HEAD_LENGTH = 8
|
32
31
|
PHONE_INDEX_LENGTH = 9
|
33
|
-
PHONE_DAT =
|
32
|
+
PHONE_DAT = 'phone.dat'
|
34
33
|
|
35
|
-
DICT = %w[nil 中国移动 中国联通 中国电信 中国电信虚拟运营商 中国联通虚拟运营商 中国移动虚拟运营商]
|
34
|
+
DICT = %w[nil 中国移动 中国联通 中国电信 中国电信虚拟运营商 中国联通虚拟运营商 中国移动虚拟运营商 中国广电 广电虚拟运营商]
|
36
35
|
|
37
36
|
Result = Struct.new(:phone, :province, :city, :zipcode, :areazone, :card_type) do
|
38
37
|
def to_s
|
@@ -42,38 +41,33 @@ module Phonedata
|
|
42
41
|
class Pd
|
43
42
|
include ::Singleton
|
44
43
|
|
45
|
-
def initialize(dat_file_path = nil)
|
44
|
+
def initialize(dat_file_path = nil)
|
46
45
|
df = dat_file_path
|
47
46
|
if df.nil?
|
48
|
-
dir = ENV['PHONE_DATA_DIR'] || File.join(File.dirname(__FILE__),
|
47
|
+
dir = ENV['PHONE_DATA_DIR'] || File.join(File.dirname(__FILE__), '../data')
|
49
48
|
df = File.join("#{dir}/#{PHONE_DAT}")
|
50
|
-
end
|
51
|
-
f= File.open(df)
|
49
|
+
end
|
50
|
+
f = File.open(df)
|
52
51
|
@bytes_content = f.read.bytes
|
53
52
|
end
|
54
53
|
|
55
54
|
def find(phone_num)
|
56
|
-
if phone_num.size < 7 || phone_num.size > 11
|
57
|
-
|
58
|
-
end
|
55
|
+
raise ArgumentError, ' illegal phone length ' if phone_num.size < 7 || phone_num.size > 11
|
56
|
+
|
59
57
|
searched_phone = phone_num[0...7].to_i
|
60
|
-
#binary search
|
61
|
-
min = 0
|
58
|
+
# binary search
|
59
|
+
min = 0
|
62
60
|
max = total_record
|
63
61
|
while min <= max
|
64
62
|
mid = (min + max) / 2
|
65
63
|
current_offset = (first_record_offset + mid * PHONE_INDEX_LENGTH)
|
66
|
-
if current_offset >= _total_len
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
record_offset_start_idx = current_offset+INT_LEN
|
71
|
-
card_type_start_idx = current_offset+INT_LEN*2
|
72
|
-
#
|
64
|
+
break if current_offset >= _total_len
|
65
|
+
|
66
|
+
record_offset_start_idx = current_offset + INT_LEN
|
67
|
+
card_type_start_idx = current_offset + INT_LEN * 2
|
73
68
|
cur_phone_seg = _get4(@bytes_content[current_offset...record_offset_start_idx])
|
74
69
|
record_offset = _get4(@bytes_content[record_offset_start_idx...card_type_start_idx])
|
75
70
|
card_type = @bytes_content[card_type_start_idx]
|
76
|
-
#
|
77
71
|
if searched_phone > cur_phone_seg
|
78
72
|
min = mid + 1
|
79
73
|
elsif searched_phone < cur_phone_seg
|
@@ -81,18 +75,17 @@ module Phonedata
|
|
81
75
|
else
|
82
76
|
sub_arr = @bytes_content[record_offset...first_record_offset]
|
83
77
|
full_data_arr = sub_arr[0...sub_arr.index(0)]
|
84
|
-
full_data_str = full_data_arr.pack(
|
85
|
-
result = full_data_str.split(
|
86
|
-
|
78
|
+
full_data_str = full_data_arr.pack('C*').force_encoding('UTF-8')
|
79
|
+
result = full_data_str.split('|')
|
80
|
+
|
87
81
|
return Result.new(phone_num, result[0], result[1], result[2], result[3], DICT[card_type])
|
88
82
|
end
|
89
|
-
#
|
90
83
|
end
|
91
|
-
|
84
|
+
Result.new(phone_num, nil, nil, nil, nil, nil)
|
92
85
|
end
|
93
86
|
|
94
|
-
def file_version
|
95
|
-
@bytes_content[0...INT_LEN].pack(
|
87
|
+
def file_version
|
88
|
+
@bytes_content[0...INT_LEN].pack('C*')
|
96
89
|
end
|
97
90
|
|
98
91
|
def first_record_offset
|
@@ -105,6 +98,7 @@ module Phonedata
|
|
105
98
|
end
|
106
99
|
|
107
100
|
private
|
101
|
+
|
108
102
|
def _total_len
|
109
103
|
@bytes_content.size
|
110
104
|
end
|
@@ -114,7 +108,7 @@ module Phonedata
|
|
114
108
|
0
|
115
109
|
else
|
116
110
|
byte_arr[0] | byte_arr[1] << 8 | byte_arr[2] << 16 | byte_arr[3] << 24
|
117
|
-
end
|
111
|
+
end
|
118
112
|
end
|
119
113
|
end
|
120
114
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phonedata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0.2302
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Forwaard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|