]> git.openstreetmap.org Git - rails.git/blobdiff - lib/osm_community_index/local_chapter.rb
Improve encapsulation / location of osm-community-index files
[rails.git] / lib / osm_community_index / local_chapter.rb
diff --git a/lib/osm_community_index/local_chapter.rb b/lib/osm_community_index/local_chapter.rb
new file mode 100644 (file)
index 0000000..6b203af
--- /dev/null
@@ -0,0 +1,39 @@
+module OsmCommunityIndex
+  class LocalChapter
+
+    attr_reader :id, :name, :url
+
+    @localised_chapters = {}
+
+    def initialize(id, name, url)
+      @id = id
+      @name = name
+      @url = url
+    end
+
+    def self.local_chapters_with_locale(locale)
+      @localised_chapters[locale] ||= load_local_chapters(locale)
+    end
+
+    protected
+
+    def self.load_local_chapters(locale)
+      community_index = OsmCommunityIndex.community_index
+      localised_strings = OsmCommunityIndex.localised_strings(locale)
+      local_chapters = []
+      community_index["resources"].each do |id, resource|
+        resource.each do |key, value|
+          next unless key == "type" && value == "osm-lc" && id != "OSMF"
+
+          strings = resource["strings"]
+          name = localised_strings.dig(id, "name") || strings["name"] || strings["community"]
+          url = strings["url"]
+          local_chapters.push(LocalChapter.new(id, name, url))
+        end
+      end
+      local_chapters
+    end
+
+  end
+
+end
\ No newline at end of file