Sha256: e6cf2c8cff87ad2e0e25be118b5476e236277318409f003dda24af0c51572a82

Contents?: true

Size: 1011 Bytes

Versions: 1

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true

# (c) Copyright 2020 Ribose Inc.
#

module Iev
  # @todo This needs to be rewritten.
  class Iso639Code
    COUNTRY_CODES = YAML.load(IO.read(File.join(__dir__, "iso_639_2.yaml")))
    THREE_CHAR_MEMO = {}

    def initialize(two_char_code)
      @code = case two_char_code.length
        when 2
          two_char_code
        else
          # This is to handle code "nl BE" in the Iev sheet
          two_char_code.split(" ").first
        end
    end

    def find(code_type)
      code = country_codes.detect do |key, value|
        key if value["iso_639_1"] == @code.to_s && value[code_type]
      end

      raise StandardError, "Iso639Code not found for '#{@code}'!" if code.nil?

      code
    end

    def self.three_char_code(two_char_code, code_type = "terminology")
      memo_index = [two_char_code, code_type]
      THREE_CHAR_MEMO[memo_index] ||= new(two_char_code).find(code_type)
    end

    private

    def country_codes
      COUNTRY_CODES
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iev-0.3.5 lib/iev/iso_639_code.rb