sypex_geo 0.2.1 → 0.2.2
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 +4 -4
- data/.travis.yml +3 -4
- data/lib/sypex_geo/pack.rb +49 -81
- data/lib/sypex_geo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 779670bdba2a9501832e6abe3f6d7e9af1b06e1d
|
4
|
+
data.tar.gz: 637e7857f219dcb6f253bda4910162a6ce42ad58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81fe140b1d6d9dcc490caa7449c4909fd650458c2f15c64feda9cd242bf93d4c5548c4f4255d52cfaefc9773b2507b24aae6d61ed4500480f2f63f15c4884c7e
|
7
|
+
data.tar.gz: e3e7ff6ed788e1b2837a033ca85d20c75ff094c207a95fe8efc5412721af646b78a4c2a5f5b6452a3ed7d15a1924af5f79ad7e13803b5e1e93dbf863086323e7
|
data/.travis.yml
CHANGED
@@ -6,12 +6,11 @@ rvm:
|
|
6
6
|
- 2.1.0
|
7
7
|
|
8
8
|
env:
|
9
|
-
- SXGEO_DB=SxGeo.dat
|
10
|
-
- SXGEO_CITY_DB=SxGeoCity.dat
|
9
|
+
- SXGEO_DB=SxGeo.dat SXGEO_CITY_DB=SxGeoCity.dat
|
11
10
|
|
12
11
|
before_script:
|
13
|
-
-
|
14
|
-
-
|
12
|
+
- wget http://sypexgeo.net/files/SxGeoCountry.zip && unzip SxGeoCountry.zip
|
13
|
+
- wget http://sypexgeo.net/files/SxGeoCity_utf8.zip && unzip SxGeoCity_utf8.zip
|
15
14
|
|
16
15
|
addons:
|
17
16
|
code_climate:
|
data/lib/sypex_geo/pack.rb
CHANGED
@@ -1,108 +1,76 @@
|
|
1
1
|
module SypexGeo
|
2
2
|
class Pack
|
3
3
|
def initialize(pack)
|
4
|
-
|
4
|
+
setup_pack(pack)
|
5
5
|
end
|
6
6
|
|
7
7
|
def parse(data)
|
8
|
-
@data
|
9
|
-
@pos = 0
|
10
|
-
result = {}
|
11
|
-
|
12
|
-
@pack.split('/').each do |part|
|
13
|
-
chunk, name = part.split(':')
|
14
|
-
result[name.to_sym] = parse_chunk(chunk)
|
15
|
-
end
|
16
|
-
|
17
|
-
result
|
8
|
+
Hash[@keys.zip(process(data.unpack(@format)))]
|
18
9
|
end
|
19
10
|
|
20
11
|
protected
|
21
12
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
when 's' then read_int16(chunk)
|
27
|
-
when 'S' then read_uint16(chunk)
|
28
|
-
when 'm' then read_int24(chunk)
|
29
|
-
when 'M' then read_uint24(chunk)
|
30
|
-
when 'i' then read_int32(chunk)
|
31
|
-
when 'I' then read_uint32(chunk)
|
32
|
-
when 'f' then read_float(chunk)
|
33
|
-
when 'd' then read_double(chunk)
|
34
|
-
when 'n' then read_decimal16(chunk)
|
35
|
-
when 'N' then read_decimal32(chunk)
|
36
|
-
when 'c' then read_chars(chunk)
|
37
|
-
when 'b' then read_blob(chunk)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def read(len)
|
42
|
-
@pos += len
|
43
|
-
@data[@pos - len, len]
|
44
|
-
end
|
45
|
-
|
46
|
-
def read_string(len)
|
47
|
-
read(len).strip.force_encoding('UTF-8')
|
48
|
-
end
|
13
|
+
def setup_pack(pack)
|
14
|
+
@keys = []
|
15
|
+
@format = ''
|
16
|
+
@processors = []
|
49
17
|
|
50
|
-
|
51
|
-
|
52
|
-
|
18
|
+
pack.split('/').each do |part|
|
19
|
+
chunk, key = part.split(':')
|
20
|
+
parser = chunk_parser(chunk)
|
53
21
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
def read_int16(chunk)
|
59
|
-
read(2).unpack('s')[0]
|
60
|
-
end
|
61
|
-
|
62
|
-
def read_uint16(chunk)
|
63
|
-
read(2).unpack('S')[0]
|
64
|
-
end
|
65
|
-
|
66
|
-
def read_int24(chunk)
|
67
|
-
raw = read(3)
|
68
|
-
raw += (raw[2].unpack('C')[0] >> 7) > 0 ? "\xFF" : "\x00"
|
69
|
-
raw.unpack('l')[0]
|
70
|
-
end
|
71
|
-
|
72
|
-
def read_uint24(chunk)
|
73
|
-
(read(3) + "\x00").unpack('L')[0]
|
74
|
-
end
|
75
|
-
|
76
|
-
def read_int32(chunk)
|
77
|
-
read(4).unpack('l')[0]
|
78
|
-
end
|
79
|
-
|
80
|
-
def read_uint32(chunk)
|
81
|
-
read(4).unpack('L')[0]
|
22
|
+
@keys << key.to_sym
|
23
|
+
@format << parser.shift
|
24
|
+
@processors << (parser.empty? ? nil : parser)
|
25
|
+
end
|
82
26
|
end
|
83
27
|
|
84
|
-
def
|
85
|
-
|
28
|
+
def process(vals)
|
29
|
+
vals.each_with_index.map do |val, i|
|
30
|
+
if processor = @processors[i]
|
31
|
+
name = processor.shift
|
32
|
+
args = processor
|
33
|
+
args.unshift(val)
|
34
|
+
send(name, *args)
|
35
|
+
else
|
36
|
+
val
|
37
|
+
end
|
38
|
+
end
|
86
39
|
end
|
87
40
|
|
88
|
-
def
|
89
|
-
|
41
|
+
def chunk_parser(chunk)
|
42
|
+
case chunk[0]
|
43
|
+
when 't' then [ 'c' ]
|
44
|
+
when 'T' then [ 'C' ]
|
45
|
+
when 's' then [ 's' ]
|
46
|
+
when 'S' then [ 'S' ]
|
47
|
+
when 'm' then [ 'a3', :parse_int24 ]
|
48
|
+
when 'M' then [ 'a3', :parse_uint24 ]
|
49
|
+
when 'i' then [ 'l' ]
|
50
|
+
when 'I' then [ 'L' ]
|
51
|
+
when 'f' then [ 'f' ]
|
52
|
+
when 'd' then [ 'D' ]
|
53
|
+
when 'n' then [ 'a2', :parse_decimal, 's', chunk[1] ]
|
54
|
+
when 'N' then [ 'a4', :parse_decimal, 'l', chunk[1] ]
|
55
|
+
when 'c' then [ 'a' + chunk[1], :parse_string ]
|
56
|
+
when 'b' then [ 'Z*', :parse_string ]
|
57
|
+
end
|
90
58
|
end
|
91
59
|
|
92
|
-
def
|
93
|
-
|
60
|
+
def parse_int24(val)
|
61
|
+
(val + (val[2].ord >> 7 > 0 ? "\xFF" : "\x00").b).unpack('l')[0]
|
94
62
|
end
|
95
63
|
|
96
|
-
def
|
97
|
-
|
64
|
+
def parse_uint24(val)
|
65
|
+
(val + "\x00").unpack('L')[0]
|
98
66
|
end
|
99
67
|
|
100
|
-
def
|
101
|
-
|
68
|
+
def parse_decimal(val, format, fract)
|
69
|
+
val.unpack(format)[0].to_f / (10 ** fract.to_i)
|
102
70
|
end
|
103
71
|
|
104
|
-
def
|
105
|
-
|
72
|
+
def parse_string(val)
|
73
|
+
val.strip.force_encoding('UTF-8')
|
106
74
|
end
|
107
75
|
end
|
108
76
|
end
|
data/lib/sypex_geo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sypex_geo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kolesnikov Danil
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|