sxgeo 0.0.1 → 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd697929b9ddf5145222d67da7902bfc90d3b9d5
4
+ data.tar.gz: 6db4e7baa20ab14f230e90034a42813d1ac26054
5
+ SHA512:
6
+ metadata.gz: 7233b86e15438ac02be0bb98a9dcd7ea3537563e748d969497626a39b5c639093edd80054aa924d1c1dd8ebc5385c3ddcf52fbb50e4e75feb8824e0ad4494eed
7
+ data.tar.gz: 866fcd43728e09f9a99167aa6ef61d97151870705d1cb63964ec4fe06a60450448c35b8abe7a864bce23d31c266f917a93b50d7c5d599b1ea078157805d5e5ef
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -1,29 +1,13 @@
1
1
  # Sxgeo
2
2
 
3
- TODO: Write a gem description
3
+ Порт Sypex Geo 2.1 - PHP класса для определение местоположения по IP-адресу заточенный для России.
4
4
 
5
- ## Installation
5
+ Гем содержит в себе бинарный файл БД по городам городов.
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Пример использования:
8
8
 
9
- gem 'sxgeo'
9
+ SxGeo.new.get(ip)['city'].force_encoding('UTF-8')
10
10
 
11
- And then execute:
11
+ Еще примеры http://sypexgeo.net/ru/docs/
12
12
 
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install sxgeo
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Contributing
24
-
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
13
+ batch mode пока не работает.
data/Rakefile CHANGED
@@ -1 +1,3 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new
data/lib/sxgeo.rb CHANGED
@@ -5,8 +5,9 @@ class SxGeo
5
5
  SXGEO_FILE = 0
6
6
  SXGEO_MEMORY = 1
7
7
  SXGEO_BATCH = 2
8
+ SXGEO_CITY_FILE = File.dirname(__FILE__) + "/sxgeo/SxGeoCity.dat"
8
9
 
9
- def initialize (db_file = File.dirname(__FILE__) + "/sxgeo/SxGeoCity.dat", type = SXGEO_FILE)
10
+ def initialize (db_file = SXGEO_CITY_FILE, mode = SXGEO_FILE)
10
11
  @cc2iso = [
11
12
  '', 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ',
12
13
  'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH',
@@ -30,8 +31,8 @@ class SxGeo
30
31
  'AX', 'GG', 'IM', 'JE', 'BL', 'MF'
31
32
  ]
32
33
 
33
- @batch_mode = false
34
- @memory_mode = false
34
+ @batch_mode = 0
35
+ @memory_mode = 0
35
36
  @debug_mode = false
36
37
 
37
38
  @fh = File.open(db_file, 'rb')
@@ -45,14 +46,14 @@ class SxGeo
45
46
  @b_idx_str = @fh.read @b_idx_len * 4
46
47
  @m_idx_str = @fh.read @m_idx_len * 4
47
48
  @block_len = 3 + @id_len;
48
- @batch_mode = type & SXGEO_BATCH
49
- @memory_mode = type & SXGEO_MEMORY
49
+ @batch_mode = mode & SXGEO_BATCH
50
+ @memory_mode = mode & SXGEO_MEMORY
50
51
  @db_begin = @fh.tell
51
- if (@batch_mode)
52
+ if @batch_mode != 0
52
53
  @b_idx_arr = @b_idx_str.unpack("N*")
53
54
  @m_idx_arr = @m_idx_str.scan(/.{1,4}/)
54
55
  end
55
- if (@memory_mode)
56
+ if @memory_mode != 0
56
57
  @db = @fh.read @db_items * @block_len
57
58
  @regions_db = @fh.read @region_size
58
59
  @cities_db = @fh.read @city_size
@@ -72,7 +73,7 @@ class SxGeo
72
73
  max = offset
73
74
  end
74
75
  end
75
- while ipn > @m_idx_arr[min] && min < max
76
+ while (ipn > @m_idx_arr[min]) && (min < max)
76
77
  min+=1
77
78
  end
78
79
  else
@@ -121,12 +122,11 @@ class SxGeo
121
122
  # if ip1n == 0 || ip1n == 10 || ip1n == 127 || ip1n >= @b_idx_len || false === (ipn = ip2long(ip)) # TODO
122
123
  # return false
123
124
  # end
124
- # binding.pry
125
125
  ipn = [ipn].pack 'N'
126
126
  @ip1c = ip1n.chr
127
127
  # Находим блок данных индексе первых байт
128
128
  blocks = {}
129
- if @batch_mode
129
+ if @batch_mode != 0
130
130
  blocks = {'min' => @b_idx_arr[ip1n.to_i-1], 'max' => @b_idx_arr[ip1n.to_i]}
131
131
  else
132
132
  blocks['min'], blocks['max'] = @b_idx_str[(ip1n - 1) * 4, (ip1n - 1) * 4+8].unpack 'NN'
@@ -143,8 +143,7 @@ class SxGeo
143
143
  max = blocks['max'] if max > blocks['max']
144
144
  len = max - min
145
145
  # Находим нужный диапазон в БД
146
- if @memory_mode
147
- # binding.pry
146
+ if @memory_mode != 0
148
147
  puts "#{ipn}, #{min}, #{max}" if @debug_mode
149
148
  return search_db(@db, ipn, min, max)
150
149
  else
@@ -161,17 +160,8 @@ class SxGeo
161
160
  long
162
161
  end
163
162
 
164
- # def long2ip(long)
165
- # ip = []
166
- # 4.times do |i|
167
- # ip.push(long.to_i & 255)
168
- # long = long.to_i >> 8
169
- # end
170
- # ip.join(".")
171
- # end
172
-
173
163
  def parseCity(seek)
174
- if @memory_mode
164
+ if @memory_mode != 0
175
165
  raw = @cities_db[seek, @max_city]
176
166
  else
177
167
  @fh.pos = @cities_begin + seek
@@ -179,10 +169,15 @@ class SxGeo
179
169
  end
180
170
  @city = {}
181
171
  @city['regid'], @city['cc'],
182
- @city['2fips'], @city['lat'], @city['lon'] = raw.unpack 'NCaNN'
172
+ @city['fips'], @city['lat'], @city['lon'] = raw.unpack 'NCa2NN'
183
173
  @city['country'] = @cc2iso[@city['cc']];
184
- @city['lat'] /= 1000000;
185
- @city['lon'] /= 1000000;
174
+
175
+ # convert unsigned int32 to signed int32
176
+ @city['lat'] -= 0xFFFF_FFFF if @city['lat'] >= 0x8000_0000
177
+ @city['lon'] -= 0xFFFF_FFFF if @city['lon'] >= 0x8000_0000
178
+
179
+ @city['lat'] /= 1000000.0;
180
+ @city['lon'] /= 1000000.0;
186
181
  @city['city'] = raw[15,raw.length].split("\0").first
187
182
  return @city;
188
183
  end
@@ -234,7 +229,7 @@ class SxGeo
234
229
  'Pacific/Auckland', 'Pacific/Chatham', 'Pacific/Efate', 'Pacific/Fiji', 'Pacific/Galapagos', 'Pacific/Guadalcanal', 'Pacific/Honolulu',
235
230
  'Pacific/Port_Moresby' ]
236
231
  if region_seek > 0
237
- if @memory_mode
232
+ if @memory_mode != 0
238
233
  region = @regions_db[region_seek, @max_region].split "\0"
239
234
  else
240
235
  @fh.pos = @info['regions_begin'] + region_seek
data/lib/sxgeo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sxgeo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,9 @@
1
+ require 'sxgeo'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ config.order = 'random'
8
+ config.mock_with :rr
9
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe SxGeo do
4
+ it 'works in normal mode' do
5
+ SxGeo.new.get('87.250.250.203')['city'].force_encoding('UTF-8').should == 'Москва'
6
+ end
7
+
8
+ it 'works in batch mode' do
9
+ pending 'Batch mode not work yet'
10
+ sxgeo = SxGeo.new SxGeo::SXGEO_CITY_FILE, SxGeo::SXGEO_BATCH
11
+ sxgeo.get('87.250.250.203')['city'].force_encoding('UTF-8').should == 'Москва'
12
+ end
13
+
14
+ it 'works in memory mode' do
15
+ sxgeo = SxGeo.new SxGeo::SXGEO_CITY_FILE, SxGeo::SXGEO_MEMORY
16
+ sxgeo.get('87.250.250.203')['city'].force_encoding('UTF-8').should == 'Москва'
17
+ end
18
+
19
+ it 'works in batch and memory modes' do
20
+ pending 'Batch mode not work yet'
21
+ sxgeo = SxGeo.new SxGeo::SXGEO_CITY_FILE, SxGeo::SXGEO_BATCH | SxGeo::SXGEO_MEMORY
22
+ sxgeo.get('87.250.250.203')['city'].force_encoding('UTF-8').should == 'Москва'
23
+ end
24
+
25
+ it '.ip2long works well' do
26
+ sxgeo = SxGeo.new
27
+ sxgeo.ip2long('87.250.250.203').should == 1476065995
28
+ end
29
+
30
+ end
data/sxgeo.gemspec CHANGED
@@ -15,4 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency "rspec-core", "~> 2.0"
20
+ gem.add_development_dependency "rspec-expectations", "~> 2.0"
21
+ gem.add_development_dependency "rr", "~> 1.0"
18
22
  end
metadata CHANGED
@@ -1,16 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sxgeo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - nleo
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-24 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-expectations
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rr
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
14
55
  description: Sypex Geo port
15
56
  email:
16
57
  executables: []
@@ -18,6 +59,7 @@ extensions: []
18
59
  extra_rdoc_files: []
19
60
  files:
20
61
  - .gitignore
62
+ - .rspec
21
63
  - Gemfile
22
64
  - LICENSE.txt
23
65
  - README.md
@@ -25,29 +67,32 @@ files:
25
67
  - lib/sxgeo.rb
26
68
  - lib/sxgeo/SxGeoCity.dat
27
69
  - lib/sxgeo/version.rb
70
+ - spec/spec_helper.rb
71
+ - spec/sxgeo_spec.rb
28
72
  - sxgeo.gemspec
29
73
  homepage: https://github.com/nleo/sxgeo
30
74
  licenses: []
75
+ metadata: {}
31
76
  post_install_message:
32
77
  rdoc_options: []
33
78
  require_paths:
34
79
  - lib
35
80
  required_ruby_version: !ruby/object:Gem::Requirement
36
- none: false
37
81
  requirements:
38
- - - ! '>='
82
+ - - '>='
39
83
  - !ruby/object:Gem::Version
40
84
  version: '0'
41
85
  required_rubygems_version: !ruby/object:Gem::Requirement
42
- none: false
43
86
  requirements:
44
- - - ! '>='
87
+ - - '>='
45
88
  - !ruby/object:Gem::Version
46
89
  version: '0'
47
90
  requirements: []
48
91
  rubyforge_project:
49
- rubygems_version: 1.8.24
92
+ rubygems_version: 2.2.1
50
93
  signing_key:
51
- specification_version: 3
94
+ specification_version: 4
52
95
  summary: Sypex Geo port
53
- test_files: []
96
+ test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/sxgeo_spec.rb