Module: NHKore::Util

Defined in:
lib/nhkore/util.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.0

Constant Summary collapse

CORE_DIR =

Since:

  • 0.2.0

'core'
WEB_DIR =

Since:

  • 0.2.0

'web'
JST_OFFSET =

Japan Standard Time (JST) time zone offset from UTC

Since:

  • 0.2.0

'+09:00'
JST_OFFSET_HOUR =

Since:

  • 0.2.0

9
JST_OFFSET_MIN =

Since:

  • 0.2.0

0
HIRAGANA_REGEX =

Since:

  • 0.2.0

/\p{Hiragana}/.freeze
JPN_SPACE =

Must be double-quoted for escape chars

Since:

  • 0.2.0

"\u3000"
KANA_REGEX =

Since:

  • 0.2.0

/\p{Hiragana}|\p{Katakana}/.freeze
KANJI_REGEX =

Han probably stands for Hanzi?

Since:

  • 0.2.0

/\p{Han}/.freeze
KATAKANA_REGEX =

Since:

  • 0.2.0

/\p{Katakana}/.freeze
NORMALIZE_STR_REGEX =

Since:

  • 0.2.0

/[^[[:alpha:]]]+/.freeze
STRIP_WEB_STR_REGEX =

Since:

  • 0.2.0

/(\A[[:space:]]+)|([[:space:]]+\z)/.freeze
WEB_SPACES_REGEX =

Since:

  • 0.2.0

/[[:space:]]+/.freeze
JST_YEAR =

Since:

  • 0.2.0

jst_now.year
MAX_SANE_YEAR =

+1 Justin Case for time zone differences at the end of the year

Since:

  • 0.2.0

JST_YEAR + 1
MIN_SANE_YEAR =

NHK was founded in 1924/25.

However, when was the website first created?

Since:

  • 0.2.0

1924

Class Method Summary collapse

Class Method Details

.dir_str?(str) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



53
54
55
# File 'lib/nhkore/util.rb', line 53

def self.dir_str?(str)
  return str.match?(%r{[/\\]\s*\z/})
end

.domain(host, clean: true) ⇒ Object

Since:

  • 0.2.0



57
58
59
60
61
62
63
64
# 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) ⇒ Object

Since:

  • 0.2.0



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



83
84
85
# 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) ⇒ Object

Since:

  • 0.2.0



87
88
89
90
91
92
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



94
95
96
97
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



99
100
101
# File 'lib/nhkore/util.rb', line 99

def self.hiragana?(str)
  return HIRAGANA_REGEX =~ str
end

.jst_nowObject

Since:

  • 0.2.0



40
41
42
# File 'lib/nhkore/util.rb', line 40

def self.jst_now
  return Time.now.getlocal(JST_OFFSET)
end

.jst_time(time) ⇒ Object

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

Since:

  • 0.2.0



105
106
107
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



109
110
111
# File 'lib/nhkore/util.rb', line 109

def self.kana?(str)
  return KANA_REGEX =~ str
end

.kanji?(str) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



113
114
115
# File 'lib/nhkore/util.rb', line 113

def self.kanji?(str)
  return KANJI_REGEX =~ str
end

.katakana?(str) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



117
118
119
# File 'lib/nhkore/util.rb', line 117

def self.katakana?(str)
  return KATAKANA_REGEX =~ str
end

.load_yaml(data, file: nil, **kargs) ⇒ Object

Since:

  • 0.2.0



121
122
123
124
125
126
127
128
129
130
131
132
# 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) ⇒ Object

Since:

  • 0.2.0



134
135
136
# File 'lib/nhkore/util.rb', line 134

def self.normalize_str(str)
  return str.gsub(NORMALIZE_STR_REGEX,'')
end

.reduce_jpn_space(str) ⇒ Object

Since:

  • 0.2.0



138
139
140
141
# 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) ⇒ Object

Since:

  • 0.2.0



143
144
145
# 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) ⇒ Object

Since:

  • 0.2.0



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.0



180
181
182
# 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) ⇒ Object

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

Since:

  • 0.2.0



185
186
187
188
189
190
191
192
193
# 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) ⇒ Object

Since:

  • 0.2.0



195
196
197
# File 'lib/nhkore/util.rb', line 195

def self.unspace_web_str(str)
  return str.gsub(WEB_SPACES_REGEX,'')
end

.warn(msg, uplevel: 1) ⇒ Object

Since:

  • 0.2.0



199
200
201
# File 'lib/nhkore/util.rb', line 199

def self.warn(msg,uplevel: 1)
  Kernel.warn(msg,uplevel: uplevel)
end