]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/local_chapter.rb
Merge branch 'master' into feature/add-communities-page
[rails.git] / lib / osm_community_index / local_chapter.rb
1 module OsmCommunityIndex
2   class LocalChapter
3
4     attr_reader :id, :name, :url
5
6     @localised_chapters = {}
7
8     def initialize(id, name, url)
9       @id = id
10       @name = name
11       @url = url
12     end
13
14     def self.local_chapters_with_locale(locale)
15       @localised_chapters[locale] ||= load_local_chapters(locale)
16     end
17
18     protected
19
20     def self.load_local_chapters(locale)
21       community_index = OsmCommunityIndex.community_index
22       localised_strings = OsmCommunityIndex.localised_strings(locale)
23       local_chapters = []
24       community_index["resources"].each do |id, resource|
25         resource.each do |key, value|
26           next unless key == "type" && value == "osm-lc" && id != "OSMF"
27
28           strings = resource["strings"]
29           name = localised_strings.dig(id, "name") || strings["name"] || strings["community"]
30           url = strings["url"]
31           local_chapters.push(LocalChapter.new(id, name, url))
32         end
33       end
34       local_chapters
35     end
36
37   end
38
39 end