mobinfo 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/data/phone.dat +0 -0
- data/lib/mobinfo.rb +66 -0
- data/lib/mobinfo/version.rb +3 -0
- data/mobinfo.gemspec +23 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87da81f2e3e933595aea982cbe6e173f63e04d7a
|
4
|
+
data.tar.gz: 9e04e6be2a6a04bafe2f87b7315ff9ebfb1dc9b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c9afde941eae50fa35eff1f5311ea451fcba5a9fa92b4d42149cca56154e1cbad5bca730dc23c79316dd5f6a0170622ba49cff1180e3c406bbf2c1f90f8d1aa
|
7
|
+
data.tar.gz: d170e68fd1a35b58601d0f90a2a6732ddb483279edd0011e22534ce0c6e9ee11668929eaa63b8d22f9d0c44786c9c76de0c6aeb7e432b0c7d189e9347ca3013a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# MobInfo
|
2
|
+
|
3
|
+
lookup ISP and geo info (province, city etc) for mobile number in China
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mobinfo'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mobinfo
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
MobInfo.lookup('1862169')
|
25
|
+
# => {:province=>"上海", :city=>"上海", :postal_code=>"200000", :region_code=>"021", :isp=>"联通", :prefix=>1862169}
|
26
|
+
|
27
|
+
MobInfo.lookup(110)
|
28
|
+
# => nil
|
29
|
+
```
|
30
|
+
|
31
|
+
## Credits
|
32
|
+
|
33
|
+
`phone.dat`, idea and algorithm from [https://github.com/lovedboy/phone](https://github.com/lovedboy/phone)
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/forresty/mobinfo.
|
38
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mobinfo"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/data/phone.dat
ADDED
Binary file
|
data/lib/mobinfo.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "mobinfo/version"
|
2
|
+
|
3
|
+
module MobInfo
|
4
|
+
@@data = File.read(File.expand_path('../../data/phone.dat', __FILE__), mode: 'rb')
|
5
|
+
@@data_version = @@data[0..3]
|
6
|
+
@@first_index_offset = @@data[4..7].unpack('l').first
|
7
|
+
|
8
|
+
def self.record_at_offset(offset)
|
9
|
+
string = @@data[offset..-1].unpack('Z*').first.force_encoding('utf-8')
|
10
|
+
|
11
|
+
province, city, postal_code, region_code = string.split('|')
|
12
|
+
|
13
|
+
{ province: province, city: city, postal_code: postal_code, region_code: region_code }
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.binary_search(prefix, from=0, to=nil)
|
17
|
+
if to.nil?
|
18
|
+
to = ((@@data.size - @@first_index_offset) / 9).to_i
|
19
|
+
end
|
20
|
+
|
21
|
+
if from > to
|
22
|
+
return [prefix, nil, nil]
|
23
|
+
end
|
24
|
+
|
25
|
+
mid = (from + to) / 2
|
26
|
+
|
27
|
+
index_offset = mid * 9 + @@first_index_offset
|
28
|
+
|
29
|
+
first_7digits, record_offset, type = @@data[index_offset, index_offset+9].unpack('l2c')
|
30
|
+
|
31
|
+
if first_7digits.nil?
|
32
|
+
return [prefix, nil, nil]
|
33
|
+
end
|
34
|
+
|
35
|
+
if prefix < first_7digits
|
36
|
+
binary_search(prefix, from, mid - 1)
|
37
|
+
elsif prefix > first_7digits
|
38
|
+
binary_search(prefix, mid + 1, to)
|
39
|
+
else
|
40
|
+
[first_7digits, record_offset, type]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.lookup(number)
|
45
|
+
number = number.to_s
|
46
|
+
number = number[4..-1] if number =~ /\+86-/
|
47
|
+
|
48
|
+
prefix = number[0...7].to_i
|
49
|
+
|
50
|
+
first_7digits, record_offset, type = binary_search(prefix)
|
51
|
+
|
52
|
+
if type
|
53
|
+
isp = case type.to_i
|
54
|
+
when 1 then '移动'
|
55
|
+
when 2 then '联通'
|
56
|
+
when 3 then '电信'
|
57
|
+
when 4 then '电信虚拟运营商'
|
58
|
+
when 5 then '联通虚拟运营商'
|
59
|
+
when 6 then '移动虚拟运营商'
|
60
|
+
else '未知运营商'
|
61
|
+
end
|
62
|
+
|
63
|
+
record_at_offset(record_offset).merge(isp: isp, prefix: first_7digits)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/mobinfo.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mobinfo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mobinfo"
|
8
|
+
spec.version = MobInfo::VERSION
|
9
|
+
spec.authors = ["Forrest Ye"]
|
10
|
+
spec.email = ["afu@forresty.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{lookup ISP and geo info (province, city) for mobile number in China}
|
13
|
+
spec.homepage = "https://github.com/forresty/mobinfo"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mobinfo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Forrest Ye
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- afu@forresty.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- data/phone.dat
|
71
|
+
- lib/mobinfo.rb
|
72
|
+
- lib/mobinfo/version.rb
|
73
|
+
- mobinfo.gemspec
|
74
|
+
homepage: https://github.com/forresty/mobinfo
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.5
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: lookup ISP and geo info (province, city) for mobile number in China
|
97
|
+
test_files: []
|