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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7561aef15597c229eade88c0f336e86cfc6d2894
4
- data.tar.gz: a85ff0572ef6aa59fe54daf3f56cb6413079e125
3
+ metadata.gz: 779670bdba2a9501832e6abe3f6d7e9af1b06e1d
4
+ data.tar.gz: 637e7857f219dcb6f253bda4910162a6ce42ad58
5
5
  SHA512:
6
- metadata.gz: 623a87b4e18f3d4eb85f2739e621337f5aa34f99454bcd36de44a5dc4409a330ec7ba013bc513c947c47609f288dc87f5fe0888dcbbdd70afb3c2f0d97d67055
7
- data.tar.gz: 41452d01235f3b3caaa6b444262c24f0698d49d54b1aef93cd5f02a6b6d9edab3a9ae84a2e85b9842715137322b7aae28104615aa1447500f4e2d9d318a40fb2
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
- - if [[ -n "$SXGEO_DB" ]]; then wget http://sypexgeo.net/files/SxGeoCountry.zip && unzip SxGeoCountry.zip; fi
14
- - if [[ -n "$SXGEO_CITY_DB" ]]; then wget http://sypexgeo.net/files/SxGeoCity_utf8.zip && unzip SxGeoCity_utf8.zip; fi
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:
@@ -1,108 +1,76 @@
1
1
  module SypexGeo
2
2
  class Pack
3
3
  def initialize(pack)
4
- @pack = pack
4
+ setup_pack(pack)
5
5
  end
6
6
 
7
7
  def parse(data)
8
- @data = 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 parse_chunk(chunk)
23
- case chunk[0]
24
- when 't' then read_int8(chunk)
25
- when 'T' then read_uint8(chunk)
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
- def read_int8(chunk)
51
- read(1).unpack('c')[0]
52
- end
18
+ pack.split('/').each do |part|
19
+ chunk, key = part.split(':')
20
+ parser = chunk_parser(chunk)
53
21
 
54
- def read_uint8(chunk)
55
- read(1).unpack('C')[0]
56
- end
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 read_float(chunk)
85
- read(4).unpack('f')[0]
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 read_double(chunk)
89
- read(8).unpack('d')[0]
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 read_decimal16(chunk)
93
- read(2).unpack('s')[0].to_f / (10 ** chunk[1].to_i)
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 read_decimal32(chunk)
97
- read(4).unpack('l')[0].to_f / (10 ** chunk[1].to_i)
64
+ def parse_uint24(val)
65
+ (val + "\x00").unpack('L')[0]
98
66
  end
99
67
 
100
- def read_chars(chunk)
101
- read_string(chunk[1..-1].to_i)
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 read_blob(chunk)
105
- read_string(@data.index("\0", @pos) - @pos + 1)
72
+ def parse_string(val)
73
+ val.strip.force_encoding('UTF-8')
106
74
  end
107
75
  end
108
76
  end
@@ -1,3 +1,3 @@
1
1
  module SypexGeo
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
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.1
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-22 00:00:00.000000000 Z
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler