city_search 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b80b8084db7d36585b0b89383d7536a7726522dbad08a9ee3874c2dcac6bfa32
4
- data.tar.gz: cfe894eeadc1ce0aea59fc2811c9ca49e0edf66221d0f62e69abd5386e2bd335
3
+ metadata.gz: ea57bb866eda5cda691cb33823af5de355d8b4d00ba6070df1bc2059c05c4979
4
+ data.tar.gz: 5456b55982bee572a540a3f2906249ad076f7837b592f38b737b1b607f82b7ef
5
5
  SHA512:
6
- metadata.gz: a7d4c9274bce0ae8f097763e5c73d145a24ff5539da62c71cdda9008edcbc5f3fc8f57053964c52c1a84669420fcf40735aa0ffb2864b6cec79a0e1401b45b1e
7
- data.tar.gz: c6d71984278c657663659c4bcb14e1b6a0b741a6d865caa5b5366616372731d68efe90d36a9610d38d8c727071e8f11b0cc6d4572a42c9feb424d3be7f60703f
6
+ metadata.gz: 1e27af2e20e2ed7bd0227ecf278a17d8cd9b916ce1f8375cf084bc7c1f20b01a9acc545a7b47d59e7d8d996b3c7ceb73271fd17689ec70aeb3fe6a5090d95056
7
+ data.tar.gz: e6b5845867e3912c30ff3df5d09083b18650c846abd18ed4d3e55f3ffee5949f183100023893ac29ffe1faf22b6ad9ad9edbd0d0713eb51dd8f3fa96191a0faa
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  *.gem
2
- cities.txt
2
+ GeoLite2-City-Locations-ru.csv
3
+ russia.bin
4
+ subdivisions.bin
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # City Search
2
+
3
+ Need automcomplete search for cities?
4
+ **You are in right place**
5
+ Sadly for now there is only russia.
6
+
7
+ This product includes GeoLite2 data created by MaxMind, available from
8
+ <a href="https://www.maxmind.com">https://www.maxmind.com</a>.
9
+
10
+ ```ruby
11
+ CitySearch.new.search('нижн')
12
+ => [["Нижнеангарск", "Бурятия"], ["Нижневартовск", "Ханты-Мансийский АО"], ["Нижнекамск", "Татарстан"], ["Нижнесортымский", "Ханты-Мансийский АО"], ["Нижнеудинск", "Иркутская область"], ["Нижний Бестях", "Саха (Якутия)"], ["Нижний Куранах", "Саха (Якутия)"], ["Нижний Ломов", "Пензенская область"], ["Нижний Новгород", "Нижегородская область"], ["Нижний Тагил", "Свердловская область"], ["Нижняя Салда", "Свердловская область"]]
13
+ ```
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'dawg'
2
+
3
+ desc 'create divisions.bin from source'
4
+ task :create_divisions do
5
+ subdivisions = []
6
+
7
+ IO.foreach('GeoLite2-City-Locations-ru.csv') do |line|
8
+ geoname_id, locale_code, continent_code, continent_name,
9
+ country_iso_code, country_name, subdivision_1_iso_code,
10
+ subdivision_1_name, subdivision_2_iso_code, subdivision_2_name,
11
+ city_name, metro_code, time_zone, is_in_european_union = line.split(',')
12
+ subdivisions << subdivision_1_name.strip.delete('\"')
13
+ end
14
+
15
+ subdivisions.uniq!
16
+ subdivisions.sort!
17
+ subdivisions.reject! {|d| d.empty? }
18
+ subdivisions_hash = {}
19
+ subdivisions.each_with_index do |subdivision, index|
20
+ subdivisions_hash[index] = subdivision
21
+ end
22
+
23
+ File.open('subdivisions.bin', 'wb') { |f| f.write(Marshal.dump(subdivisions_hash)) }
24
+
25
+ end
26
+
27
+
28
+ desc 'create dawg file with cities for russia'
29
+ task :create_dawg_russia do
30
+ cities = []
31
+ subdivisions = Marshal.load(File.read('subdivisions.bin'))
32
+ subdivisions_inverted = subdivisions.invert
33
+
34
+ IO.foreach('GeoLite2-City-Locations-ru.csv') do |line|
35
+ geoname_id, locale_code, continent_code, continent_name,
36
+ country_iso_code, country_name, subdivision_1_iso_code,
37
+ subdivision_1_name, subdivision_2_iso_code, subdivision_2_name,
38
+ city_name, metro_code, time_zone, is_in_european_union = line.split(',')
39
+
40
+ next if country_iso_code != 'RU'
41
+
42
+ subdivision_1_name = subdivision_1_name.strip.delete('\"')
43
+ city_name = city_name.strip.delete('\"').downcase
44
+ cities << "#{city_name} #{subdivisions_inverted[subdivision_1_name]}"
45
+ end
46
+
47
+ cities.uniq!
48
+ cities.sort!
49
+ cities.reject! {|d| d.empty? }
50
+
51
+ dawg = Dawg.new
52
+ cities.each do |city|
53
+ dawg.insert(city)
54
+ end
55
+
56
+ dawg.finish
57
+
58
+ dawg.save('russia.bin')
59
+ end
60
+
data/city_search.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'city_search'
3
- s.version = '0.0.2'
4
- s.date = '2019-04-30'
3
+ s.version = '0.0.3'
4
+ s.date = '2019-05-04'
5
5
  s.summary = "City Search"
6
6
  s.description = "Finds cities"
7
7
  s.authors = ["Maksatbek Mansurov"]
data/data/russia.bin CHANGED
Binary file
data/lib/city_search.rb CHANGED
@@ -1,33 +1,36 @@
1
1
  require 'dawg'
2
2
 
3
3
  class CitySearch
4
- def initialize(country = :all)
5
- raise 'country not found' unless country_exists?(country)
6
-
7
- @country = country
8
- end
9
-
10
4
  def search(q)
11
- results = db.query(q).reject {|r| r.empty? }
5
+ results = db.query(q.downcase).reject {|r| r.empty? }
12
6
  results.map do |r|
13
7
  result = r.split(' ')
14
8
  code = result.last
15
9
  result.pop
16
- result += states[code.to_i]
17
- end
18
- end
19
10
 
20
- def country_exists?(country)
21
- File.exists?(File.dirname(__FILE__) + "/../data/#{country}.bin")
11
+ city_name = result.join(' ')
12
+ .split
13
+ .map(&:capitalize)
14
+ .join(' ')
15
+ state = states[code.to_i]
16
+
17
+ [city_name, state]
18
+ end
22
19
  end
23
20
 
24
21
  def db
25
- @all ||= Dawg.load(File.dirname(__FILE__) + "/../data/#{@country}.bin")
22
+ @all ||= Dawg.load(File.join(data_path, 'russia.bin'))
26
23
  end
27
24
 
28
25
  def states
29
26
  @states ||= Marshal.load(
30
- File.read(File.dirname(__FILE__) + "/../data/#{@country}_states.bin")
27
+ File.read(File.join(data_path, 'subdivisions.bin'))
31
28
  )
32
29
  end
30
+
31
+ private
32
+
33
+ def data_path
34
+ @data_path ||= File.join(File.dirname(__FILE__), '/../data')
35
+ end
33
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: city_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maksatbek Mansurov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dawg
@@ -37,11 +37,10 @@ extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
39
  - ".gitignore"
40
+ - README.md
41
+ - Rakefile
40
42
  - city_search.gemspec
41
- - data/all.bin
42
- - data/all_states.bin
43
43
  - data/russia.bin
44
- - data/russia_states.bin
45
44
  - lib/city_search.rb
46
45
  homepage: http://rubygems.org/gems/city_search
47
46
  licenses:
data/data/all.bin DELETED
Binary file
data/data/all_states.bin DELETED
Binary file
Binary file