Module: NHKore::Util
- Defined in:
- lib/nhkore/util.rb
Constant Summary collapse
- CORE_DIR =
'core'
- WEB_DIR =
'web'
- JST_OFFSET =
Japan Standard Time (JST) time zone offset from UTC
'+09:00'
- JST_OFFSET_HOUR =
9
- JST_OFFSET_MIN =
0
- HIRAGANA_REGEX =
/\p{Hiragana}/.freeze
- JPN_SPACE =
Must be double-quoted for escape chars
"\u3000"
- KANA_REGEX =
/\p{Hiragana}|\p{Katakana}/.freeze
- KANJI_REGEX =
Han probably stands for Hanzi?
/\p{Han}/.freeze
- KATAKANA_REGEX =
/\p{Katakana}/.freeze
- NORMALIZE_STR_REGEX =
/[^[[:alpha:]]]+/.freeze
- STRIP_WEB_STR_REGEX =
/(\A[[:space:]]+)|([[:space:]]+\z)/.freeze
- WEB_SPACES_REGEX =
/[[:space:]]+/.freeze
- JST_YEAR =
jst_now.year
- MAX_SANE_YEAR =
+1 Justin Case for time zone differences at the end of the year
JST_YEAR + 1
- MIN_SANE_YEAR =
NHK was founded in 1924/25.
However, when was the website first created?
1924
Class Method Summary collapse
- .dir_str?(str) ⇒ Boolean
- .domain(host, clean: true) ⇒ Object
- .dump_yaml(obj, flow_level: 8, stylers: nil) ⇒ Object
- .empty_web_str?(str) ⇒ Boolean
- .escape_html(str) ⇒ Object
- .filename_str?(str) ⇒ Boolean
- .hiragana?(str) ⇒ Boolean
- .jst_now ⇒ Object
-
.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.
- .kana?(str) ⇒ Boolean
- .kanji?(str) ⇒ Boolean
- .katakana?(str) ⇒ Boolean
- .load_yaml(data, file: nil, **kargs) ⇒ Object
- .normalize_str(str) ⇒ Object
- .reduce_jpn_space(str) ⇒ Object
- .reduce_space(str) ⇒ Object
- .replace_uri_query!(uri, **new_query) ⇒ Object
- .sane_year?(year) ⇒ Boolean
-
.strip_web_str(str) ⇒ Object
String’s normal strip() method doesn’t work with special Unicode/HTML white space.
- .unspace_web_str(str) ⇒ Object
- .warn(msg, uplevel: 1) ⇒ Object
Class Method Details
.dir_str?(str) ⇒ Boolean
49 50 51 |
# File 'lib/nhkore/util.rb', line 49 def self.dir_str?(str) return str.match?(%r{[/\\]\s*\z/}) end |
.domain(host, clean: true) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/nhkore/util.rb', line 53 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nhkore/util.rb', line 62 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
79 80 81 |
# File 'lib/nhkore/util.rb', line 79 def self.empty_web_str?(str) return str.nil? || strip_web_str(str).empty? end |
.escape_html(str) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/nhkore/util.rb', line 83 def self.escape_html(str) str = CGI.escapeHTML(str) str = str.gsub("\n",'<br>') return str end |
.filename_str?(str) ⇒ Boolean
90 91 92 93 |
# File 'lib/nhkore/util.rb', line 90 def self.filename_str?(str) # Do not use "!dir_str?()"! It's not the same meaning! return !str.match?(%r{[/\\]}) end |
.hiragana?(str) ⇒ Boolean
95 96 97 |
# File 'lib/nhkore/util.rb', line 95 def self.hiragana?(str) return HIRAGANA_REGEX =~ str end |
.jst_now ⇒ Object
36 37 38 |
# File 'lib/nhkore/util.rb', line 36 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.
101 102 103 |
# File 'lib/nhkore/util.rb', line 101 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
105 106 107 |
# File 'lib/nhkore/util.rb', line 105 def self.kana?(str) return KANA_REGEX =~ str end |
.kanji?(str) ⇒ Boolean
109 110 111 |
# File 'lib/nhkore/util.rb', line 109 def self.kanji?(str) return KANJI_REGEX =~ str end |
.katakana?(str) ⇒ Boolean
113 114 115 |
# File 'lib/nhkore/util.rb', line 113 def self.katakana?(str) return KATAKANA_REGEX =~ str end |
.load_yaml(data, file: nil, **kargs) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/nhkore/util.rb', line 117 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
130 131 132 |
# File 'lib/nhkore/util.rb', line 130 def self.normalize_str(str) return str.gsub(NORMALIZE_STR_REGEX,'') end |
.reduce_jpn_space(str) ⇒ Object
134 135 136 137 |
# File 'lib/nhkore/util.rb', line 134 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
139 140 141 |
# File 'lib/nhkore/util.rb', line 139 def self.reduce_space(str) return str.gsub(WEB_SPACES_REGEX,' ') end |
.replace_uri_query!(uri, **new_query) ⇒ Object
143 144 145 146 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 |
# File 'lib/nhkore/util.rb', line 143 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
176 177 178 |
# File 'lib/nhkore/util.rb', line 176 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.
181 182 183 184 185 186 187 188 189 |
# File 'lib/nhkore/util.rb', line 181 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
191 192 193 |
# File 'lib/nhkore/util.rb', line 191 def self.unspace_web_str(str) return str.gsub(WEB_SPACES_REGEX,'') end |
.warn(msg, uplevel: 1) ⇒ Object
195 196 197 |
# File 'lib/nhkore/util.rb', line 195 def self.warn(msg,uplevel: 1) Kernel.warn(msg,uplevel: uplevel) end |