Module: Inkcite::Util

Defined in:
lib/inkcite/util.rb

Class Method Summary collapse

Class Method Details

.add_query_param(href, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
# 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) ⇒ Object



64
65
66
# File 'lib/inkcite/util.rb', line 64

def self.contrasting_text_color color
  brightness_value(color) > 382.5 ? darken(color) : lighten(color)
end

.darken(color, amount = 0.4) ⇒ Object



26
27
28
29
30
31
32
33
# 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) ⇒ Object

Iterates through the list of possible options and returns the first non-blank value.



37
38
39
# File 'lib/inkcite/util.rb', line 37

def self.detect *opts
  opts.detect { |o| !o.blank? }
end

.each_line(path, fail_if_not_exists, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/inkcite/util.rb', line 68

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) ⇒ Object

Centralizing the URL/CGI encoding for all HREF processing because URI.escape/encode is obsolete.



43
44
45
46
47
# File 'lib/inkcite/util.rb', line 43

def self.encode *arg
  silence_warnings do
    URI.escape(*arg)
  end
end

.escape(*arg) ⇒ Object



49
50
51
52
53
# File 'lib/inkcite/util.rb', line 49

def self.escape *arg
  silence_warnings do
    URI.escape(*arg)
  end
end

.is_fully_qualified?(href) ⇒ Boolean



78
79
80
# File 'lib/inkcite/util.rb', line 78

def self.is_fully_qualified? href
  href.include?('//')
end

.last_modified(file) ⇒ Object



82
83
84
# File 'lib/inkcite/util.rb', line 82

def self.last_modified file
  file && File.exist?(file) ? File.mtime(file).to_i : 0
end

.lighten(color, amount = 0.6) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/inkcite/util.rb', line 55

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

.read(*argv) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/inkcite/util.rb', line 86

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 = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/inkcite/util.rb', line 96

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