module UncleKryon::Util
Constants
- DATETIME_FORMAT
- DATE_FORMAT
Public Class Methods
add_trail_slash(url)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/unclekryon/util.rb, line 101 def self.empty_s?(str) return str.nil? || str.gsub(/[[:space:]]+/,'').empty? end
fix_link(url)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/unclekryon/util.rb, line 125 def self.format_date(date) return date.nil? ? nil : date.strftime(DATE_FORMAT) end
format_datetime(datetime)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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