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 "&nbsp;" 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
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_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_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