module Inkcite::Util
Constants
- BLACK
- DATE_FORMAT
- KB
- WHITE
Public Class Methods
add_query_param(href, value)
click to toggle source
# File lib/inkcite/util.rb, line 6 def self.add_query_param href, value # Start with either a question mark or an ampersand depending on # whether or not there is already a question mark in the URI. param = href.include?('?') ? '&' : '?' param << value.to_s if hash_position = href.index('#') href[hash_position..0] = param else href << param end href end
brightness_value(color)
click to toggle source
# File lib/inkcite/util.rb, line 22 def self.brightness_value color color.nil? ? 0 : (color.gsub('#', '').scan(/../).map { |c| c.hex }).inject { |sum, c| sum + c } end
contrasting_text_color(color)
click to toggle source
# File lib/inkcite/util.rb, line 167 def self.contrasting_text_color color brightness_value(color) > 382.5 ? darken(color) : lighten(color) end
darken(color, amount=0.4)
click to toggle source
# File lib/inkcite/util.rb, line 26 def self.darken color, amount=0.4 return BLACK if color.nil? rgb = color.gsub('#', '').scan(/../).map { |c| c.hex } rgb[0] = (rgb[0].to_i * amount).round rgb[1] = (rgb[1].to_i * amount).round rgb[2] = (rgb[2].to_i * amount).round "#%02x%02x%02x" % rgb end
detect(*opts)
click to toggle source
Iterates through the list of possible options and returns the first non-blank value.
# File lib/inkcite/util.rb, line 37 def self.detect *opts opts.detect { |o| !o.blank? } end
dir_size(path)
click to toggle source
# File lib/inkcite/util.rb, line 41 def self.dir_size path Dir.glob(File.join(path, '*.*')).inject(0) { |size, file| size + File.size(file) } end
each_line(path, fail_if_not_exists) { |strip| ... }
click to toggle source
# File lib/inkcite/util.rb, line 171 def self.each_line path, fail_if_not_exists, &block if File.exist?(path) File.open(path).each { |line| yield line.strip } elsif fail_if_not_exists raise "File not found: #{path}" end end
encode(*arg)
click to toggle source
Centralizing the URL/CGI encoding for all HREF processing because URI.escape/encode is obsolete.
# File lib/inkcite/util.rb, line 47 def self.encode *arg silence_warnings do URI.escape(*arg) end end
escape(*arg)
click to toggle source
# File lib/inkcite/util.rb, line 53 def self.escape *arg silence_warnings do URI.escape(*arg) end end
exec(command)
click to toggle source
# File lib/inkcite/util.rb, line 59 def self.exec command command = command.join(' ') if command.is_a?(Array) `#{command} 2>&1` end
file_extension(path_to_file)
click to toggle source
# File lib/inkcite/util.rb, line 64 def self.file_extension path_to_file File.extname(path_to_file).delete('.').downcase end
hsl_to_color(h, s, l)
click to toggle source
Conversion of HSL to RGB color courtesy of axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
# File lib/inkcite/util.rb, line 70 def self.hsl_to_color h, s, l # The algorithm expects h, s and l to be values between 0-1. h = h / 360.0 s = s / 100.0 l = l / 100.0 # Wrap the color wheel if the hue provided is less than or # greater than 1 h += 1.0 while h < 0 h -= 1.0 while h > 1 s = 0.0 if s < 0 s = 1.0 if s > 1 l = 0.0 if l < 0 l = 1.0 if l > 1 r = g = b = 0 if s == 0 r = g = b = l else q = l < 0.5 ? l * (1 + s) : l + s - l * s p = 2 * l - q r = hue_to_rgb(p, q, h + 1/3.0) g = hue_to_rgb(p, q, h) b = hue_to_rgb(p, q, h - 1/3.0) end r = (r * 255).round(0) g = (g * 255).round(0) b = (b * 255).round(0) "##{rgb_to_hex(r)}#{rgb_to_hex(g)}#{rgb_to_hex(b)}" end
hue_to_rgb(p, q, t)
click to toggle source
# File lib/inkcite/util.rb, line 109 def self.hue_to_rgb p, q, t t += 1 if t < 0 t -= 1 if t > 1 return (p + (q - p) * 6.0 * t) if (t < 1.0/6.0) return q if (t < 0.5) return (p + (q - p) * (2/3.0 - t) * 6) if (t < 2/3.0) p end
is_fully_qualified?(href)
click to toggle source
# File lib/inkcite/util.rb, line 181 def self.is_fully_qualified? href href.include?('//') end
last_modified(file)
click to toggle source
# File lib/inkcite/util.rb, line 185 def self.last_modified file file && File.exist?(file) ? File.mtime(file).to_i : 0 end
lighten(color, amount=0.6)
click to toggle source
# File lib/inkcite/util.rb, line 158 def self.lighten color, amount=0.6 return WHITE if color.nil? rgb = color.gsub('#', '').scan(/../).map { |c| c.hex } rgb[0] = [(rgb[0].to_i + 255 * amount).round, 255].min rgb[1] = [(rgb[1].to_i + 255 * amount).round, 255].min rgb[2] = [(rgb[2].to_i + 255 * amount).round, 255].min "#%02x%02x%02x" % rgb end
log(message, meta=nil)
click to toggle source
# File lib/inkcite/util.rb, line 118 def self.log message, meta=nil msg = "#{Time.now.strftime(DATE_FORMAT)} - INFO - #{message}" unless meta.blank? msg << '[' msg << Renderer.join_hash(meta, '=', ', ') msg << ']' end puts msg end
pretty_file_size(size)
click to toggle source
# File lib/inkcite/util.rb, line 130 def self.pretty_file_size size size = size.to_f decimals = 2 ext = 'b' if size > KB size = size / KB if size > KB size = size / KB ext = 'MB' else ext = 'Kb' end else round = 0 "#{size}b" end "#{size.round(decimals)}#{ext}" end
read(*argv)
click to toggle source
# File lib/inkcite/util.rb, line 189 def self.read *argv path = File.join(File.expand_path('../..', File.dirname(__FILE__)), argv) if File.exist?(path) line = File.open(path).read line.gsub!(/[\r\f\n]+/, "\n") line.gsub!(/ {2,}/, ' ') line end end
read_yml(file, opts={})
click to toggle source
# File lib/inkcite/util.rb, line 199 def self.read_yml file, opts={} if File.exist?(file) yml = YAML.load_file(file) symbolize_keys(yml) unless opts[:symbolize_keys] == false yml elsif opts[:fail_if_not_exists] raise "File not found: #{file}" else {} end end
rgb_to_hex(val)
click to toggle source
RGB to hex courtesy of blog.lebrijo.com/converting-rgb-colors-to-hexadecimal-with-ruby/
# File lib/inkcite/util.rb, line 154 def self.rgb_to_hex val val.to_s(16).rjust(2, '0').downcase end
Private Class Methods
symbolize_keys(hash)
click to toggle source
Recursive key symbolization for the provided Hash.
# File lib/inkcite/util.rb, line 221 def self.symbolize_keys hash unless hash.nil? hash.symbolize_keys! hash.each do |k, v| symbolize_keys(v) if v.is_a?(Hash) end end hash end