Sha256: 6b25f448cd3dcd38395a672069885a7b1deab8373e08214366bd8c1d7643d0b2

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

# encoding: UTF-8

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

require 'twitter_cldr/resources/download'
require 'fileutils'
require 'json'

module TwitterCldr
  module Resources
    module Uli
      class SegmentExceptionsImporter

        URL = "http://unicode.org/uli/trac/export/58/trunk/abbrs/json/%{locale}.json"

        attr_reader :input_path, :output_path

        def initialize(input_path, output_path)
          @input_path  = input_path
          @output_path = output_path
        end

        def import(locales)
          FileUtils.mkdir_p(input_path)
          FileUtils.mkdir_p(output_path)
          locales.each { |locale| import_locale(locale) }
        end

        private

        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_path_for(locale)
          TwitterCldr::Resources.download_if_necessary(
            input_file, URL.gsub("%{locale}", locale.to_s)
          )
          input_file
        end

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

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter_cldr-4.0.0 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.6.0 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.5.0 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.4.0 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.3.0 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb