geolist 0.1.0
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 +7 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +3 -0
- data/bin/console +8 -0
- data/geolist.gemspec +26 -0
- data/lib/data.csv +118505 -0
- data/lib/location.rb +68 -0
- metadata +112 -0
data/lib/location.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require 'iso_country_codes'
|
5
|
+
require 'open-uri'
|
6
|
+
require 'zip'
|
7
|
+
require 'pry'
|
8
|
+
|
9
|
+
module GeoList
|
10
|
+
class << self
|
11
|
+
MAXMIND_URL = 'https://download.maxmind.com/app/geoip_download'
|
12
|
+
MAXMIND_PARAMS = '?edition_id=GeoLite2-City-CSV&suffix=zip&license_key=PC4n5BvdsNQK3w6m'
|
13
|
+
MAXMIND_LINK = MAXMIND_URL + MAXMIND_PARAMS
|
14
|
+
|
15
|
+
DATA_FILE = File.join(__dir__, 'data.csv')
|
16
|
+
|
17
|
+
# position in CSV file
|
18
|
+
COUNTRY_NAME = 5
|
19
|
+
CITY = 10
|
20
|
+
|
21
|
+
def update_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
22
|
+
downloaded_file = open(MAXMIND_LINK) # rubocop:disable Security/Open
|
23
|
+
csv_filename = 'GeoLite2-City-Locations-en.csv'
|
24
|
+
|
25
|
+
Zip::File.open(downloaded_file) do |zip_file|
|
26
|
+
zip_file.each do |file|
|
27
|
+
next unless file.name.match?(/#{csv_filename}/)
|
28
|
+
|
29
|
+
path = File.join(__dir__, csv_filename)
|
30
|
+
zip_file.extract(file, path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
FileUtils.rm(DATA_FILE) if File.exist?(DATA_FILE)
|
35
|
+
File.rename File.join(__dir__, csv_filename), File.join(__dir__, 'data.csv')
|
36
|
+
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def countries
|
41
|
+
::IsoCountryCodes.all.map { |country| { name: country.name, alpha2: country.alpha2, alpha3: country.alpha3 } }
|
42
|
+
end
|
43
|
+
|
44
|
+
def cities(country_name)
|
45
|
+
return [] if country_name&.empty?
|
46
|
+
return ['Hong Kong'] if country_name == 'Hong Kong'
|
47
|
+
|
48
|
+
data.select { |row| row[COUNTRY_NAME] == country_name }.map { |row| row[CITY] }.uniq.compact.sort
|
49
|
+
end
|
50
|
+
|
51
|
+
def suburbs(city)
|
52
|
+
return [] unless city == 'Hong Kong'
|
53
|
+
|
54
|
+
data.select { |row| row[COUNTRY_NAME] == city }.map { |row| row[CITY] }.uniq.compact.sort
|
55
|
+
end
|
56
|
+
|
57
|
+
def get(country = nil, city = nil, _suburb = nil)
|
58
|
+
return countries if country.nil?
|
59
|
+
return cities(country) if city.nil?
|
60
|
+
|
61
|
+
suburbs(city)
|
62
|
+
end
|
63
|
+
|
64
|
+
def data
|
65
|
+
@data ||= CSV.read(DATA_FILE)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geolist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- relaxcore
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: iso_country_codes
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubyzip
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.1'
|
69
|
+
description: Useful to make forms and validations. It uses MaxMind database.
|
70
|
+
email:
|
71
|
+
- yuriirogulya@gmail.com
|
72
|
+
executables:
|
73
|
+
- console
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- geolist.gemspec
|
86
|
+
- lib/data.csv
|
87
|
+
- lib/location.rb
|
88
|
+
homepage: https://github.com/relaxcoredev/location
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.7.6
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Simple list of countries, cities and suburbs of the world
|
112
|
+
test_files: []
|