Sha256: e63d915d587376d7b84b1e4f19fd2a694109421e1eed8fc46b15f67a37173a61

Contents?: true

Size: 1023 Bytes

Versions: 2

Compression:

Stored size: 1023 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

      if code.nil?
        raise StandardError.new("Iso639Code not found for '#{@code}'!")
      end

      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

2 entries across 2 versions & 1 rubygems

Version Path
iev-0.3.4 lib/iev/iso_639_code.rb
iev-0.3.3 lib/iev/iso_639_code.rb