module NHKore::Util

@author Jonathan Bradley Whited @since 0.2.0

Constants

CORE_DIR
HIRAGANA_REGEX
JPN_SPACE
JST_OFFSET
JST_OFFSET_HOUR
JST_OFFSET_MIN
JST_YEAR
KANA_REGEX
KANJI_REGEX
KATAKANA_REGEX
MAX_SANE_YEAR
MIN_SANE_YEAR

NHK was founded in 1924/25.

However, when was the website first created?

NORMALIZE_STR_REGEX
STRIP_WEB_STR_REGEX
WEB_DIR
WEB_SPACES_REGEX

Public Class Methods

dir_str?(str) click to toggle source
# File lib/nhkore/util.rb, line 53
def self.dir_str?(str)
  return str.match?(%r{[/\\]\s*\z/})
end
domain(host,clean: true) click to toggle source
# File lib/nhkore/util.rb, line 57
def self.domain(host,clean: true)
  require 'public_suffix'

  domain = PublicSuffix.domain(host)
  domain = unspace_web_str(domain).downcase if !domain.nil? && clean

  return domain
end
dump_yaml(obj,flow_level: 8,stylers: nil) click to toggle source
# File lib/nhkore/util.rb, line 66
def self.dump_yaml(obj,flow_level: 8,stylers: nil)
  require 'psychgus'

  stylers = Array(stylers)

  return Psychgus.dump(obj,
    deref_aliases: true, # Dereference aliases for load_yaml()
    header: true, # %YAML [version]
    line_width: 10_000, # Try not to wrap; ichiman!
    stylers: [
      Psychgus::FlowStyler.new(flow_level), # Put extra details on one line (flow/inline style)
      Psychgus::NoSymStyler.new(cap: false), # Remove symbols, don't capitalize
      Psychgus::NoTagStyler.new, # Remove class names (tags)
    ].concat(stylers),
  )
end
empty_web_str?(str) click to toggle source
# File lib/nhkore/util.rb, line 83
def self.empty_web_str?(str)
  return str.nil? || strip_web_str(str).empty?
end
escape_html(str) click to toggle source
# File lib/nhkore/util.rb, line 87
def self.escape_html(str)
  str = CGI.escapeHTML(str)
  str = str.gsub("\n",'<br>')

  return str
end
filename_str?(str) click to toggle source
# File lib/nhkore/util.rb, line 94
def self.filename_str?(str)
  # Do not use "!dir_str?()"! It's not the same meaning!
  return !str.match?(%r{[/\\]})
end
hiragana?(str) click to toggle source
# File lib/nhkore/util.rb, line 99
def self.hiragana?(str)
  return HIRAGANA_REGEX =~ str
end
jst_now() click to toggle source
# File lib/nhkore/util.rb, line 40
def self.jst_now
  return Time.now.getlocal(JST_OFFSET)
end
jst_time(time) click to toggle source

This doesn't modify the hour/minute according to {JST_OFFSET}, but instead, it just drops {JST_OFFSET} into it without adjusting it.

# File lib/nhkore/util.rb, line 105
def self.jst_time(time)
  return Time.new(time.year,time.month,time.day,time.hour,time.min,time.sec,JST_OFFSET)
end
kana?(str) click to toggle source
# File lib/nhkore/util.rb, line 109
def self.kana?(str)
  return KANA_REGEX =~ str
end
kanji?(str) click to toggle source
# File lib/nhkore/util.rb, line 113
def self.kanji?(str)
  return KANJI_REGEX =~ str
end
katakana?(str) click to toggle source
# File lib/nhkore/util.rb, line 117
def self.katakana?(str)
  return KATAKANA_REGEX =~ str
end
load_yaml(data,file: nil,**kargs) click to toggle source
# File lib/nhkore/util.rb, line 121
def self.load_yaml(data,file: nil,**kargs)
  require 'psychgus'

  return Psych.safe_load(data,
    aliases: false,
    filename: file,
    #freeze: true, # Not in this current version of Psych
    permitted_classes: [Symbol],
    symbolize_names: true,
    **kargs,
  )
end
normalize_str(str) click to toggle source
# File lib/nhkore/util.rb, line 134
def self.normalize_str(str)
  return str.gsub(NORMALIZE_STR_REGEX,'')
end
reduce_jpn_space(str) click to toggle source
# File lib/nhkore/util.rb, line 138
def self.reduce_jpn_space(str)
  # Do not strip; use a Japanese space
  return str.gsub(WEB_SPACES_REGEX,JPN_SPACE)
end
reduce_space(str) click to toggle source
# File lib/nhkore/util.rb, line 143
def self.reduce_space(str)
  return str.gsub(WEB_SPACES_REGEX,' ')
end
replace_uri_query!(uri,**new_query) click to toggle source
# File lib/nhkore/util.rb, line 147
def self.replace_uri_query!(uri,**new_query)
  return uri if new_query.empty?

  query = uri.query
  query = query.nil? ? [] : URI.decode_www_form(query)

  # First, remove the old ones.
  if !query.empty?
    new_query_keys = Set.new(new_query.keys.map do |key|
      unspace_web_str(key.to_s).downcase
    end)

    query.filter! do |q|
      if q.nil? || q.empty?
        false
      else
        key = unspace_web_str(q[0].to_s).downcase

        !new_query_keys.include?(key)
      end
    end
  end

  # Next, add the new ones.
  new_query.each do |key,value|
    query << [key,value.nil? ? '' : value]
  end

  uri.query = URI.encode_www_form(query)

  return uri
end
sane_year?(year) click to toggle source
# File lib/nhkore/util.rb, line 180
def self.sane_year?(year)
  return year >= MIN_SANE_YEAR && year <= MAX_SANE_YEAR
end
strip_web_str(str) click to toggle source

String's normal strip() method doesn't work with special Unicode/HTML white space.

# File lib/nhkore/util.rb, line 185
def self.strip_web_str(str)
  # After testing with Benchmark, this is slower than one regex.
  #str = str.gsub(/\A[[:space:]]+/,'')
  #str = str.gsub(/[[:space:]]+\z/,'')

  str = str.gsub(STRIP_WEB_STR_REGEX,'')

  return str
end
unspace_web_str(str) click to toggle source
# File lib/nhkore/util.rb, line 195
def self.unspace_web_str(str)
  return str.gsub(WEB_SPACES_REGEX,'')
end
warn(msg,uplevel: 1) click to toggle source
# File lib/nhkore/util.rb, line 199
def self.warn(msg,uplevel: 1)
  Kernel.warn(msg,uplevel: uplevel)
end