class TwitterCldr::Resources::TimezonesImporter::TimezoneData

Attributes

cldr_req[R]
locale[R]

Public Class Methods

new(locale, cldr_req) click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 88
def initialize(locale, cldr_req)
  @locale = locale
  @cldr_req = cldr_req
end

Public Instance Methods

to_h() click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 93
def to_h
  {
    formats: formats,
    timezones: timezones,
    metazones: metazones
  }
end

Private Instance Methods

cldr_main_path() click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 175
def cldr_main_path
  @cldr_main_path ||= File.join(cldr_req.common_path, 'main')
end
doc() click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 168
def doc
  @doc ||= begin
    locale_fs = locale.to_s.gsub('-', '_')
    Nokogiri.XML(File.read(File.join(cldr_main_path, "#{locale_fs}.xml")))
  end
end
formats() click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 103
def formats
  @formats ||= doc.xpath('ldml/dates/timeZoneNames/*').inject({}) do |result, format|
    if format.name.end_with?('Format')
      next if unconfirmed_draft?(format)

      underscored_name = format.name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase + 's'
      result[underscored_name] ||= {}

      type = if (type_attr = format.attribute('type'))
        type_attr.value
      else
        :generic
      end

      result[underscored_name][type] = format.text
    end

    result
  end
end
metazones() click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 140
def metazones
  @metazones ||= doc.xpath('ldml/dates/timeZoneNames/metazone').inject({}) do |result, zone|
    type = zone.attr('type').to_sym
    result[type] = {}
    long = nodes_to_hash(zone.xpath('long/*'))
    result[type][:long] = long unless long.empty?
    short = nodes_to_hash(zone.xpath('short/*'))
    result[type][:short] = short unless short.empty?
    result
  end
end
nodes_to_hash(nodes) click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 152
def nodes_to_hash(nodes)
  nodes.inject({}) do |result, node|
    unless cldr_req.draft?(node)
      result[node.name.to_sym] = node.content
    end

    result
  end
end
timezones() click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 124
def timezones
  @timezones ||= doc.xpath('ldml/dates/timeZoneNames/zone').inject({}) do |result, zone|
    type = zone.attr('type').to_sym
    result[type] = {}
    long = nodes_to_hash(zone.xpath('long/*'))
    result[type][:long] = long unless long.empty?
    short = nodes_to_hash(zone.xpath('short/*'))
    result[type][:short] = short unless short.empty?
    city = zone.xpath('exemplarCity').first
    if city && !unconfirmed_draft?(city)
      result[type][:city] = city.content
    end
    result
  end
end
unconfirmed_draft?(node) click to toggle source
# File lib/twitter_cldr/resources/timezones_importer.rb, line 162
def unconfirmed_draft?(node)
  node &&
    node.attributes['draft'] &&
    node.attributes['draft'].value == 'unconfirmed'
end