pumi 0.13.0 → 0.13.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
  SHA256:
3
- metadata.gz: 4b0cfc831082d67eb960f4bef137ff0f6ae67ea60eee36aeaae1edeb7f8fc945
4
- data.tar.gz: 7b3fb1bc47eb838c8aa75c0467bbc518e83f09a2473e75d2a70c53c76fdce492
3
+ metadata.gz: c7df0e8620fb33e07db08efc84310967c1ce09c545de619b98c7b8294baab835
4
+ data.tar.gz: d752627f28b601a273508e7868de577dc2498b61278955152a88a7890552c992
5
5
  SHA512:
6
- metadata.gz: bb6d74bd6d8b7192c8da709c30e2be05591cfe80f06f703cd765a35058458dca553f31a3cd5ffe870f2a3875054162ad9b71fa10aac47ac38a3755b2d0f42912
7
- data.tar.gz: aa3d20e100720654a696d03b82470954fe1fb2691e01e17cd93961a8a39eaff99dd0475931fc0ccae8de2f4ac26e02bb96265ec923039128922240facc0fee74
6
+ metadata.gz: abebda567ad01aceb9ab68279f37456870dedb498c89e2b8465b215fb453da437a1963924fd1342a5f55a904a4ae76943ad76fd523f6af1f29d2f722509a3d20
7
+ data.tar.gz: 420fd418789d76c2eef6ce332c75d92c08fead8d99b902b8ccf43816e26f1bfa9ebc4d75cf3d58579906e0a1ca6202d85a5d6d4869bf9c0bf194a3a1a134c4e8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.13.1](https://github.com/dwilkie/pumi/compare/v0.13.0...v0.13.1) (2023-03-02)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * Improve data parser ([#35](https://github.com/dwilkie/pumi/issues/35)) ([f17ebf5](https://github.com/dwilkie/pumi/commit/f17ebf5eba26d88f4fff4fb5bb2461d1817a0829))
9
+
3
10
  ## [0.13.0](https://github.com/dwilkie/pumi/compare/v0.12.2...v0.13.0) (2023-02-06)
4
11
 
5
12
 
data/README.md CHANGED
@@ -8,13 +8,13 @@ Pumi (ភូមិ pronounced Poom, which means Village in Khmer) is an Open Sou
8
8
 
9
9
  ## Demo and API
10
10
 
11
- A [JSON API](https://pumiapp.herokuapp.com) is available to if you're not using Ruby or if you just don't want to install a local copy of the data. The [API Start Page](https://pumiapp.herokuapp.com) also shows a Demo of a UI for for entering any location in Cambodia.
11
+ A [JSON API](https://pumi.onrender.com/) is available to if you're not using Ruby or if you just don't want to install a local copy of the data. The [API Start Page](https://pumi.onrender.com/) also shows a Demo of a UI for for entering any location in Cambodia.
12
12
 
13
13
  ## Usage
14
14
 
15
15
  ### Rails
16
16
 
17
- Using Pumi with Rails gives you some javascript helpers as well as an API to filter and select Provinces (ខេត្ត), Districts (ស្រុក / ខណ្ឌ), Communes (ឃុំ / សង្កាត់) and Villages (ភូមិ) in both latin and Khmer as seen below and in the [Pumi API Start Page](https://pumiapp.herokuapp.com)
17
+ Using Pumi with Rails gives you some javascript helpers as well as an API to filter and select Provinces (ខេត្ត), Districts (ស្រុក / ខណ្ឌ), Communes (ឃុំ / សង្កាត់) and Villages (ភូមិ) in both latin and Khmer as seen below and in the [Pumi API Start Page](https://pumi.onrender.com/)
18
18
 
19
19
  ![Pumi UI Latin](https://raw.githubusercontent.com/dwilkie/pumi/master/pumi_ui_en.png)
20
20
  ![Pumi UI Khmer](https://raw.githubusercontent.com/dwilkie/pumi/master/pumi_ui_km.png)
data/bin/parse_data CHANGED
@@ -4,63 +4,9 @@
4
4
 
5
5
  # $ parse_data
6
6
 
7
- require "pathname"
8
- require "csv"
9
- require "yaml"
7
+ require "bundler/setup"
8
+ require "pumi"
10
9
 
11
- DEFAULT_DATA_LOCATION = "tmp/*.csv".freeze
12
- HEADERS = %w[type code name_km name_latin reference note1 note2].freeze
13
- CODE_LENGTHS = {
14
- 4 => "districts",
15
- 6 => "communes",
16
- 8 => "villages"
17
- }.freeze
18
-
19
- # https://en.wikipedia.org/wiki/Administrative_divisions_of_Cambodia
20
- # https://en.wikipedia.org/wiki/Romanization_of_Khmer
21
- # https://en.wikipedia.org/wiki/United_Nations_Group_of_Experts_on_Geographical_Names
22
-
23
- AdministrativeUnit = Struct.new(:en, :km, :latin, keyword_init: true)
24
- ADMINISTRATIVE_UNITS = {
25
- "ស្រុក" => AdministrativeUnit.new(en: "District", km: "ស្រុក", latin: "Srok"),
26
- "ខណ្ឌ" => AdministrativeUnit.new(en: "Section", km: "ខណ្ឌ", latin: "Khan"),
27
- "ឃុំ" => AdministrativeUnit.new(en: "Commune", km: "ឃុំ", latin: "Khum"),
28
- "សង្កាត់" => AdministrativeUnit.new(en: "Quarter", km: "សង្កាត់", latin: "Sangkat"),
29
- "ភូមិ" => AdministrativeUnit.new(en: "Village", km: "ភូមិ", latin: "Phum"),
30
- "ក្រុង" => AdministrativeUnit.new(en: "Municipality", km: "ក្រុង", latin: "Krong")
31
- }.freeze
32
-
33
- data = {}
34
-
35
- Pathname.glob(DEFAULT_DATA_LOCATION).select(&:file?).each do |file|
36
- CSV.read(file, headers: HEADERS).each do |row|
37
- code = row.fetch("code")
38
- next if code.to_s.gsub(/\D/, "").empty?
39
-
40
- code = code.rjust(code.length + 1, "0") if code.length.odd?
41
- location_type = CODE_LENGTHS.fetch(code.length)
42
- administrative_unit = ADMINISTRATIVE_UNITS.fetch(row.fetch("type"))
43
-
44
- data[location_type] ||= {}
45
- data[location_type][code] = {
46
- "name" => {
47
- "km" => row.fetch("name_km"),
48
- "latin" => row.fetch("name_latin")
49
- },
50
- "administrative_unit" => {
51
- "km" => administrative_unit.km,
52
- "latin" => administrative_unit.latin,
53
- "en" => administrative_unit.en
54
- }
55
- }
56
- end
57
- end
58
-
59
- if data.any?
60
- CODE_LENGTHS.each do |_, type|
61
- File.write(
62
- "data/#{type}.yml",
63
- { type => data.fetch(type).sort.to_h }.to_yaml
64
- )
65
- end
66
- end
10
+ data_parser = Pumi::DataParser.new
11
+ data = data_parser.load_data!
12
+ data_parser.write_data!(data)
@@ -0,0 +1,75 @@
1
+ require "pathname"
2
+ require "csv"
3
+ require "yaml"
4
+
5
+ # https://en.wikipedia.org/wiki/Administrative_divisions_of_Cambodia
6
+ # https://en.wikipedia.org/wiki/Romanization_of_Khmer
7
+ # https://en.wikipedia.org/wiki/United_Nations_Group_of_Experts_on_Geographical_Names
8
+
9
+ module Pumi
10
+ class DataParser
11
+ CSV_HEADERS = %w[type code name_km name_latin reference note1 note2].freeze
12
+
13
+ AdministrativeUnit = Struct.new(:en, :km, :latin, :code_length, :group, keyword_init: true)
14
+ ADMINISTRATIVE_UNITS = {
15
+ "ស្រុក" => AdministrativeUnit.new(en: "District", km: "ស្រុក", latin: "Srok", code_length: 4, group: "districts"),
16
+ "ខណ្ឌ" => AdministrativeUnit.new(en: "Section", km: "ខណ្ឌ", latin: "Khan", code_length: 4, group: "districts"),
17
+ "ក្រុង" => AdministrativeUnit.new(en: "Municipality", km: "ក្រុង", latin: "Krong", code_length: 4, group: "districts"),
18
+ "ឃុំ" => AdministrativeUnit.new(en: "Commune", km: "ឃុំ", latin: "Khum", code_length: 6, group: "communes"),
19
+ "សង្កាត់" => AdministrativeUnit.new(en: "Quarter", km: "សង្កាត់", latin: "Sangkat", code_length: 6, group: "communes"),
20
+ "ភូមិ" => AdministrativeUnit.new(en: "Village", km: "ភូមិ", latin: "Phum", code_length: 8, group: "villages")
21
+ }.freeze
22
+
23
+ def load_data!(source_dir: "tmp")
24
+ data = {}
25
+
26
+ source_files(source_dir).each do |file|
27
+ CSV.read(file, headers: CSV_HEADERS).each do |row|
28
+ code = row.fetch("code")
29
+ next if code.to_s.gsub(/\D/, "").empty?
30
+
31
+ code = code.rjust(code.length + 1, "0") if code.length.odd?
32
+ administrative_unit = ADMINISTRATIVE_UNITS.fetch(row.fetch("type"))
33
+
34
+ next if administrative_unit.code_length != code.length
35
+
36
+ data[administrative_unit.group] ||= {}
37
+ data[administrative_unit.group][code] = {
38
+ "name" => {
39
+ "km" => row.fetch("name_km"),
40
+ "latin" => row.fetch("name_latin")
41
+ },
42
+ "administrative_unit" => {
43
+ "km" => administrative_unit.km,
44
+ "latin" => administrative_unit.latin,
45
+ "en" => administrative_unit.en
46
+ }
47
+ }
48
+ end
49
+ end
50
+
51
+ data
52
+ end
53
+
54
+ def write_data!(data, destination_dir: "data")
55
+ return if data.empty?
56
+
57
+ data_groups.each do |group|
58
+ File.write(
59
+ "#{destination_dir}/#{group}.yml",
60
+ { group => data.fetch(group).sort.to_h }.to_yaml
61
+ )
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def source_files(source_dir)
68
+ Pathname.glob("#{source_dir}/*.csv").select(&:file?)
69
+ end
70
+
71
+ def data_groups
72
+ ADMINISTRATIVE_UNITS.values.map(&:group).uniq
73
+ end
74
+ end
75
+ end
data/lib/pumi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pumi
2
- VERSION = "0.13.0".freeze
2
+ VERSION = "0.13.1".freeze
3
3
  end
data/lib/pumi.rb CHANGED
@@ -23,3 +23,4 @@ require_relative "pumi/district"
23
23
  require_relative "pumi/commune"
24
24
  require_relative "pumi/village"
25
25
  require_relative "pumi/parser"
26
+ require_relative "pumi/data_parser"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pumi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Wilkie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-06 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -274,6 +274,7 @@ files:
274
274
  - lib/pumi.rb
275
275
  - lib/pumi/administrative_unit.rb
276
276
  - lib/pumi/commune.rb
277
+ - lib/pumi/data_parser.rb
277
278
  - lib/pumi/data_store.rb
278
279
  - lib/pumi/district.rb
279
280
  - lib/pumi/location.rb