module Stringprep::Rfc3454

Constants

DATA_LINE
END_TABLE
READ_RETRIES
RFC_DIR
RFC_FILE
SKIP_LINE
START_TABLE
TABLES_DATA_DIR
TABLES_FILE

Public Class Methods

tables() click to toggle source

Loads code point tables from data/rfc3454.json, creates it if it does not exist

# File lib/stringprep/rfc3454.rb, line 8
def self.tables
  tries = READ_RETRIES
  @tables ||= begin
    raw_read_tables
  rescue Errno::ENOENT
    write_tables(parse_rfc_txt)
    retry if (tries -= 1) > 0
    raise
  end
end

Private Class Methods

parse_rfc_txt() click to toggle source
# File lib/stringprep/rfc3454.rb, line 37
def self.parse_rfc_txt
  tables = {}
  table_name = nil
  started = false
  File.readlines(RFC_FILE).each do |line|
    if line =~ START_TABLE
      started = true
      table_name = $1.gsub('.', '').downcase
      tables[table_name] = []
    elsif line =~ END_TABLE
      started = false
    elsif started && line =~ DATA_LINE && line !~ SKIP_LINE
      tables[table_name] << line.strip
    end
  end
  tables
end
raw_read_tables() click to toggle source
# File lib/stringprep/rfc3454.rb, line 33
def self.raw_read_tables
  JSON.parse(File.read(TABLES_FILE))
end
write_tables(tables) click to toggle source
# File lib/stringprep/rfc3454.rb, line 55
def self.write_tables(tables)
  FileUtils.mkdir_p TABLES_DATA_DIR
  File.open(TABLES_FILE, 'w') { |f|
    f.write(JSON.generate(tables))
  }
end