sypex_geo 0.2.0 → 0.2.1

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: 270f6a9db891c1f4f6235ca37888a22a1cb6aa18
4
- data.tar.gz: 2882b224bdbc064f518858245d670df34b08225b
3
+ metadata.gz: 7561aef15597c229eade88c0f336e86cfc6d2894
4
+ data.tar.gz: a85ff0572ef6aa59fe54daf3f56cb6413079e125
5
5
  SHA512:
6
- metadata.gz: 0cbf268f45fcf101a93e837a82567ce4b824be475e20c86c12e5b9d2211619c707b68d13ff1d209ce2d18b7f25966d26b0d3b2fe69f363004bb4a2e70551a67f
7
- data.tar.gz: 189c75e6d57c803d169c74f36f711b02a38545a132f5e81512775e14545e7af5a4436861aafa5caad480203930619b62a95e022014419be00a1773979ab2dbfc
6
+ metadata.gz: 623a87b4e18f3d4eb85f2739e621337f5aa34f99454bcd36de44a5dc4409a330ec7ba013bc513c947c47609f288dc87f5fe0888dcbbdd70afb3c2f0d97d67055
7
+ data.tar.gz: 41452d01235f3b3caaa6b444262c24f0698d49d54b1aef93cd5f02a6b6d9edab3a9ae84a2e85b9842715137322b7aae28104615aa1447500f4e2d9d318a40fb2
data/.travis.yml CHANGED
@@ -10,8 +10,8 @@ env:
10
10
  - SXGEO_CITY_DB=SxGeoCity.dat
11
11
 
12
12
  before_script:
13
- - 'wget http://sypexgeo.net/files/SxGeoCountry.zip && unzip SxGeoCountry.zip'
14
- - 'wget http://sypexgeo.net/files/SxGeoCity_utf8.zip && unzip SxGeoCity_utf8.zip'
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
15
15
 
16
16
  addons:
17
17
  code_climate:
data/README.md CHANGED
@@ -58,7 +58,11 @@ location.country_code
58
58
 
59
59
  ## Testing
60
60
 
61
- $ SXGEO_DB=./SxGeo.dat SXGEO_CITY_DB=./SxGeoCity.dat rspec
61
+ ```sh
62
+ $ wget http://sypexgeo.net/files/SxGeoCountry.zip && unzip SxGeoCountry.zip
63
+ $ wget http://sypexgeo.net/files/SxGeoCity_utf8.zip && unzip SxGeoCity_utf8.zip
64
+ $ SXGEO_DB=./SxGeo.dat SXGEO_CITY_DB=./SxGeoCity.dat rspec
65
+ ```
62
66
 
63
67
  ## License
64
68
 
@@ -47,7 +47,7 @@ module SypexGeo
47
47
  end
48
48
 
49
49
  def inspect
50
- "#<#{self.class}:0x#{object_id} @version=#{@version}>"
50
+ to_s
51
51
  end
52
52
 
53
53
  protected
@@ -1,9 +1,5 @@
1
1
  module SypexGeo
2
2
  class Pack
3
- def self.parse(pack, data)
4
- new(pack).parse(data)
5
- end
6
-
7
3
  def initialize(pack)
8
4
  @pack = pack
9
5
  end
@@ -69,12 +65,12 @@ module SypexGeo
69
65
 
70
66
  def read_int24(chunk)
71
67
  raw = read(3)
72
- raw = raw + (raw[2].ord >> 7) > 0 ? "\xFF" : "\0"
68
+ raw += (raw[2].unpack('C')[0] >> 7) > 0 ? "\xFF" : "\x00"
73
69
  raw.unpack('l')[0]
74
70
  end
75
71
 
76
72
  def read_uint24(chunk)
77
- (read(3) + "\0").unpack('L')[0]
73
+ (read(3) + "\x00").unpack('L')[0]
78
74
  end
79
75
 
80
76
  def read_int32(chunk)
@@ -1,3 +1,3 @@
1
1
  module SypexGeo
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'sypex_geo'
3
+
4
+ describe SypexGeo::Pack do
5
+ describe '#parse' do
6
+ it 'reads int8' do
7
+ expect(SypexGeo::Pack.new('t:value').parse("\xff")[:value]).to eq(-1)
8
+ end
9
+
10
+ it 'reads uint8' do
11
+ expect(SypexGeo::Pack.new('T:value').parse("\xff")[:value]).to eq(255)
12
+ end
13
+
14
+ it 'reads int16' do
15
+ expect(SypexGeo::Pack.new('s:value').parse("\xff\xff")[:value]).to eq(-1)
16
+ end
17
+
18
+ it 'reads uint16' do
19
+ expect(SypexGeo::Pack.new('S:value').parse("\xff\xff")[:value]).to eq(65535)
20
+ end
21
+
22
+ it 'reads int24' do
23
+ expect(SypexGeo::Pack.new('m:value').parse("\xff\xff\xff")[:value]).to eq(-1)
24
+ end
25
+
26
+ it 'reads uint24' do
27
+ expect(SypexGeo::Pack.new('M:value').parse("\xff\xff\xff")[:value]).to eq(16777215)
28
+ end
29
+
30
+ it 'reads int32' do
31
+ expect(SypexGeo::Pack.new('i:value').parse("\xff\xff\xff\xff")[:value]).to eq(-1)
32
+ end
33
+
34
+ it 'reads uint32' do
35
+ expect(SypexGeo::Pack.new('I:value').parse("\xff\x00\x00\x00")[:value]).to eq(255)
36
+ end
37
+
38
+ it 'reads float' do
39
+ expect(SypexGeo::Pack.new('f:value').parse("\x85\xEBUA")[:value].round(2)).to eq(13.37)
40
+ end
41
+
42
+ it 'reads double' do
43
+ expect(SypexGeo::Pack.new('d:value').parse("\xF6(\\\x8F\xC25E@")[:value].round(2)).to eq(42.42)
44
+ end
45
+
46
+ it 'reads decimal16' do
47
+ expect(SypexGeo::Pack.new('n2:value').parse("\x00\xff")[:value]).to eq(-2.56)
48
+ end
49
+
50
+ it 'reads decimal32' do
51
+ expect(SypexGeo::Pack.new('N2:value').parse("\xff\x00\xff\x00")[:value]).to eq(167119.35)
52
+ end
53
+
54
+ it 'reads chars' do
55
+ parsed = SypexGeo::Pack.new('c3:val1/c3:val2').parse('foobar')
56
+
57
+ expect(parsed[:val1]).to eq('foo')
58
+ expect(parsed[:val2]).to eq('bar')
59
+ end
60
+
61
+ it 'reads blob' do
62
+ parsed = SypexGeo::Pack.new('b:val1/b:val2').parse("foo\0bar\0")
63
+
64
+ expect(parsed[:val1]).to eq('foo')
65
+ expect(parsed[:val2]).to eq('bar')
66
+ end
67
+ end
68
+ end
@@ -1,20 +1,15 @@
1
1
  # coding: utf-8
2
2
 
3
- require './spec/spec_helper'
3
+ require 'spec_helper'
4
4
  require 'sypex_geo'
5
5
  require 'ipaddr'
6
6
 
7
7
  describe SypexGeo do
8
- let(:demo_ip) do
9
- # Random Moscow IP.
10
- '80.90.64.1'
11
- end
12
-
13
- let(:invalid_db_file) do
14
- File.expand_path(__FILE__ + '/../support/invalid.dat')
15
- end
16
-
17
8
  shared_examples 'geo db' do
9
+ let(:invalid_db_file) do
10
+ File.expand_path(__FILE__ + '/../support/invalid.dat')
11
+ end
12
+
18
13
  describe '#initialize' do
19
14
  it 'raises error if database is invalid' do
20
15
  expect do
@@ -43,11 +38,22 @@ describe SypexGeo do
43
38
  end.to raise_error(error)
44
39
  end
45
40
  end
41
+
42
+ describe '#inspect' do
43
+ it 'does not dump whole db' do
44
+ expect(subject.inspect).to eq(subject.to_s)
45
+ end
46
+ end
46
47
  end
47
48
 
48
49
  shared_examples 'city db' do
49
50
  it_behaves_like 'geo db'
50
51
 
52
+ let(:demo_ip) do
53
+ # Random Moscow IP.
54
+ '80.90.64.1'
55
+ end
56
+
51
57
  let(:city_info) do
52
58
  {
53
59
  id: 524901,
@@ -103,8 +109,13 @@ describe SypexGeo do
103
109
  shared_examples 'country db' do
104
110
  it_behaves_like 'geo db'
105
111
 
112
+ let(:demo_ip) do
113
+ # Google Public DNS.
114
+ '8.8.8.8'
115
+ end
116
+
106
117
  let(:country_code) do
107
- 'RU'
118
+ 'US'
108
119
  end
109
120
 
110
121
  it { should be_country }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sypex_geo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kolesnikov Danil
@@ -85,6 +85,7 @@ files:
85
85
  - lib/sypex_geo/pack.rb
86
86
  - lib/sypex_geo/result.rb
87
87
  - lib/sypex_geo/version.rb
88
+ - spec/lib/pack_spec.rb
88
89
  - spec/spec_helper.rb
89
90
  - spec/support/invalid.dat
90
91
  - spec/sypex_geo_spec.rb
@@ -109,11 +110,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  version: '0'
110
111
  requirements: []
111
112
  rubyforge_project:
112
- rubygems_version: 2.2.1
113
+ rubygems_version: 2.2.2
113
114
  signing_key:
114
115
  specification_version: 4
115
116
  summary: Sypex Geo IP database adapter for Ruby.
116
117
  test_files:
118
+ - spec/lib/pack_spec.rb
117
119
  - spec/spec_helper.rb
118
120
  - spec/support/invalid.dat
119
121
  - spec/sypex_geo_spec.rb