Sha256: 2c45d727fae917407eca1329409f1fc1ce14f87c418ed59a84755a450a7c2e8f

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'fileutils'
require 'open-uri'
require 'json'

module TwitterCldr
  module Resources
    module Uli
      class SegmentExceptionsImporter < Resources::Importer

        URL = "https://unicode.org/uli/trac/export/58/trunk/abbrs/json/%{locale}.json"
        LOCALES = [:de, :en, :es, :fr, :it, :pt, :ru]   # these are the only locales ULI supports at the moment

        output_path 'uli/segments'
        ruby_engine :mri

        def execute
          FileUtils.mkdir_p(input_path)
          FileUtils.mkdir_p(output_path)
          LOCALES.each { |locale| import_locale(locale) }
        end

        private

        def output_path
          params.fetch(:output_path)
        end

        def import_locale(locale)
          if input_file = download_resource_for(locale)
            output_file = File.join(output_path, "#{locale}.yml")
            exceptions = JSON.parse(File.read(input_file))

            File.open(output_file, 'w+') do |f|
              YAML.dump({
                locale => {
                  exceptions: exceptions['data']['abbrs']
                }
              }, f)
            end
          end
        end

        def download_resource_for(locale)
          input_file = input_file_for(locale)
          url = URL % { locale: locale }

          unless File.file?(input_file)
            STDOUT.write("Downloading #{url}... ")
            open(input_file, 'wb') { |file| file << open(url).read }
            puts 'done'
          end

          input_file
        end

        def input_path
          @input_path ||= File.join(
            TwitterCldr::VENDOR_DIR, 'uli', 'segments'
          )
        end

        def input_file_for(locale)
          File.join(input_path, "#{locale}.json")
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-4.4.5 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb