Module: UncleKryon::Util
- Defined in:
- lib/unclekryon/util.rb
Constant Summary collapse
- DATE_FORMAT =
'%F'
- DATETIME_FORMAT =
'%F %T'
Class Method Summary collapse
- .add_trail_slash(url) ⇒ Object
- .clean_charset(str) ⇒ Object
- .clean_data(str) ⇒ Object
- .clean_link(url, link) ⇒ Object
- .empty_s?(str) ⇒ Boolean
- .fix_link(url) ⇒ Object
- .fix_shortwith_text(text) ⇒ Object
- .format_date(date) ⇒ Object
- .format_datetime(datetime) ⇒ Object
- .get_top_link(url) ⇒ Object
- .get_url_header_data(url) ⇒ Object
- .hash_def(hash, keys, value) ⇒ Object
- .hash_def_all(hash, keys, value) ⇒ Object
- .mk_dirs_from_filepath(filepath) ⇒ Object
- .parse_date_s(str) ⇒ Object
- .parse_datetime_s(str) ⇒ Object
- .parse_url_filename(url) ⇒ Object
- .safe_max(a, b) ⇒ Object
Class Method Details
.add_trail_slash(url) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/unclekryon/util.rb', line 26 def self.add_trail_slash(url) #url = url + '/' if url !~ /\/\z/ #return url return File.join(url,'') end |
.clean_charset(str) ⇒ Object
33 34 35 |
# File 'lib/unclekryon/util.rb', line 33 def self.clean_charset(str) return str.encode('utf-8','MacRoman',universal_newline: true) # X-MAC-ROMAN end |
.clean_data(str) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/unclekryon/util.rb', line 37 def self.clean_data(str) # Have to use "[[:space:]]" for " " and "<br/>" # This is necessary for "<br />\s+" (see 2015 "KRYON IN LIMA, PERU (2)") str = str.clone str.gsub!(/[[:space:]]+/,' ') # Replace all spaces with one space str.strip! return clean_charset(str) end |
.clean_link(url, link) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/unclekryon/util.rb', line 46 def self.clean_link(url,link) if url !~ %r{/\z} # Don't know if the end is a filename or a dirname, so just assume it is a filename and chop it off url = File.dirname(url) url = add_trail_slash(url) end # 1st, handle "/" (because you won't have "/../filename", which is invalid) slash_regex = %r{\A(/+\.*/*)+} if link =~ slash_regex link = link.gsub(slash_regex,'') link = get_top_link(url) + link # get_top_link(...) adds a slash return link # Already handles "../" or "./" in the regex end # 2nd, handle "../" (and potentially "../././/" or "..//") # - Ignores "./" if has it dotdot_regex = %r{\A(\.\./)((\./)*(/)*)*} # \A (../) ( (./)* (/)* )* num_dirs = 0 # Could be a boolean; left as int because of legacy code while link =~ dotdot_regex num_dirs += 1 link = link.gsub(dotdot_regex,'') url = File.dirname(url) end if num_dirs > 0 link = add_trail_slash(url) + link return link # Already handled "./" in the regex end # 3rd, handle "./" dot_regex = %r{\A(\./+)+} if link =~ dot_regex link = link.gsub(dot_regex,'') link = url + link # Slash already added at top of method return link end # 4th, handle no path #if link !~ /#{get_top_link(url)}/i if link !~ /\Ahttps?:/i link = url + link else link = link.sub(/\Ahttp:/i,'https:') end return link end |
.empty_s?(str) ⇒ Boolean
101 102 103 |
# File 'lib/unclekryon/util.rb', line 101 def self.empty_s?(str) return str.nil? || str.gsub(/[[:space:]]+/,'').empty? end |
.fix_link(url) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/unclekryon/util.rb', line 105 def self.fix_link(url) # If we do URI.escape(), then if it's already "%20", # then it will convert it to "%2520" url = url.gsub(/[[:space:]]/,'%20') return url end |
.fix_shortwith_text(text) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/unclekryon/util.rb', line 114 def self.fix_shortwith_text(text) if text =~ %r{w/[[:alnum:]]}i # I think it looks better with a space, personally. # Some grammar guides say no space, but the Chicago style guide says there should be a space when it # is a word by itself. text = text.gsub(%r{w/}i,'w/ ') end return text end |
.format_date(date) ⇒ Object
125 126 127 |
# File 'lib/unclekryon/util.rb', line 125 def self.format_date(date) return date.nil? ? nil : date.strftime(DATE_FORMAT) end |
.format_datetime(datetime) ⇒ Object
129 130 131 |
# File 'lib/unclekryon/util.rb', line 129 def self.format_datetime(datetime) return datetime.nil? ? nil : datetime.strftime(DATETIME_FORMAT) end |
.get_top_link(url) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/unclekryon/util.rb', line 133 def self.get_top_link(url) raise "No top link: #{url}" if DevOpts.instance.dev? && url !~ /\Ahttps?\:/i http_regex = /\Ahttps?\:|\A\./i # Check '.' to prevent infinite loop while File.basename(File.dirname(url)) !~ http_regex url = File.dirname(url).strip break if url == '.' || url.empty? end return add_trail_slash(url) end |
.get_url_header_data(url) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/unclekryon/util.rb', line 147 def self.get_url_header_data(url) uri = URI(url) r = {} Net::HTTP.start(uri.host,uri.port) do |http| resp = http.request_head(uri) r = resp.to_hash end return r end |
.hash_def(hash, keys, value) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/unclekryon/util.rb', line 159 def self.hash_def(hash,keys,value) v = hash (0..keys.length - 2).each do |i| v = v[keys[i]] end v[keys[keys.length - 1]] = value if v[keys[keys.length - 1]].nil? return v[keys[keys.length - 1]] end |
.hash_def_all(hash, keys, value) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/unclekryon/util.rb', line 170 def self.hash_def_all(hash,keys,value) v = hash (0..keys.length - 2).each do |i| if v[keys[i]].nil? v[keys[i]] = {} v = v[keys[i]] end end v[keys[keys.length - 1]] = value if v[keys[keys.length - 1]].nil? return v[keys[keys.length - 1]] end |
.mk_dirs_from_filepath(filepath) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/unclekryon/util.rb', line 184 def self.mk_dirs_from_filepath(filepath) dirname = File.dirname(filepath) if !dirname.nil? raise "Spaces around dirname: '#{dirname}'" if dirname != dirname.strip if !Dir.exist?(dirname) Log.instance.info("Making dirs: '#{dirname}'...") FileUtils.mkdir_p(dirname) end end end |
.parse_date_s(str) ⇒ Object
197 198 199 |
# File 'lib/unclekryon/util.rb', line 197 def self.parse_date_s(str) return empty_s?(str) ? nil : Date.strptime(str,DATE_FORMAT) end |
.parse_datetime_s(str) ⇒ Object
201 202 203 |
# File 'lib/unclekryon/util.rb', line 201 def self.parse_datetime_s(str) return empty_s?(str) ? nil : DateTime.strptime(str,DATETIME_FORMAT) end |
.parse_url_filename(url) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/unclekryon/util.rb', line 205 def self.parse_url_filename(url) uri = URI.parse(url) r = File.basename(uri.path) r = CGI.unescape(r) return r.strip end |
.safe_max(a, b) ⇒ Object
212 213 214 |
# File 'lib/unclekryon/util.rb', line 212 def self.safe_max(a,b) return a.nil? ? b : (b.nil? ? a : ((a > b) ? a : b)) end |