]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/local_chapter.rb
Rubocop autofixes
[rails.git] / lib / osm_community_index / local_chapter.rb
1 module OsmCommunityIndex
2   class LocalChapter
3     attr_reader :id, :url
4
5     def initialize(id, url)
6       @id = id
7       @url = url
8     end
9
10     def self.local_chapters
11       @chapters = init_local_chapters
12     end
13
14     def self.init_local_chapters
15       raw_local_chapters = load_raw_local_chapters
16       local_chapters = []
17       raw_local_chapters.each do |chapter|
18         id = chapter[:id]
19         url = chapter[:resource]["strings"]["url"]
20         local_chapters.push(LocalChapter.new(id, url))
21       end
22       local_chapters
23     end
24
25     def self.load_raw_local_chapters
26       community_index = OsmCommunityIndex.community_index
27       raw_local_chapters = []
28       community_index["resources"].each do |id, resource|
29         resource.each do |key, value|
30           next unless key == "type" && value == "osm-lc" && id != "OSMF"
31
32           raw_local_chapters.push({ :id => id, :resource => resource })
33         end
34       end
35       raw_local_chapters
36     end
37
38     def self.add_to_i18n
39       raw_local_chapters = load_raw_local_chapters
40       files = Dir.glob(Rails.root.join("node_modules/osm-community-index/i18n/*"))
41       files.each do |file|
42         locale = File.basename(file, ".yaml")
43         community_index_yaml = YAML.safe_load(File.read(file))[locale]
44         # rails wants en-GB but osm-community-index has en_GB
45         locale_rails = locale.split("_").join("-")
46         data = {}
47
48         raw_local_chapters.each do |chapter|
49           id = chapter[:id]
50           resource = chapter[:resource]
51
52           strings = community_index_yaml[id] || {}
53           # if the name isn't defined then fall back on community,
54           # as per discussion here: https://github.com/osmlab/osm-community-index/issues/483
55           strings["name"] = strings["name"] || resource["strings"]["name"] || resource["strings"]["community"]
56
57           data.deep_merge!({ "osm_community_index" => { "local_chapter" => { id => strings } } })
58         end
59
60         I18n.backend.store_translations locale_rails, data
61       end
62     end
63   end
64 end