Sha256: 197c9d7c284ef7f3abb7c1e1d176fa4f0c148e94b7da32783e07544f8498725a

Contents?: true

Size: 1.53 KB

Versions: 11

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/38/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

11 entries across 11 versions & 1 rubygems

Version Path
twitter_cldr-3.0.10 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.9 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.8 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.7 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.6 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.5 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.4 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.3 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.2 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.1 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb
twitter_cldr-3.0.0 lib/twitter_cldr/resources/uli/segment_exceptions_importer.rb