Module: Gwtf

Defined in:
lib/gwtf.rb,
lib/gwtf/item.rb,
lib/gwtf/items.rb,
lib/gwtf/version.rb,
lib/gwtf/notifier/base.rb,
lib/gwtf/notifier/email.rb,
lib/gwtf/notifier/boxcar.rb,
lib/gwtf/notifier/notifo.rb,
lib/gwtf/notifier/pushover.rb

Defined Under Namespace

Modules: Notifier Classes: Item, Items

Constant Summary collapse

VERSION =
'0.10.0'
@@color =
true

Class Method Summary collapse

Class Method Details

.ask(prompt = "") ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/gwtf.rb', line 83

def self.ask(prompt="")
  stty_save = `stty -g`.chomp

  begin
    return Readline.readline("#{prompt} ", true)
  rescue Interrupt => e
    system('stty', stty_save)
    raise
  end
end

.color=(value) ⇒ Object



23
24
25
# File 'lib/gwtf.rb', line 23

def self.color=(value)
  @@color = value
end

.each_commandObject



27
28
29
30
31
32
# File 'lib/gwtf.rb', line 27

def self.each_command
  commands_dir = File.join(File.dirname(__FILE__), "gwtf", "commands")
  Dir.entries(commands_dir).grep(/_command.rb$/).sort.each do |command|
    yield File.join(commands_dir, command)
  end
end

.green(msg) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/gwtf.rb', line 94

def self.green(msg)
  if STDOUT.tty? && @@color
    "%s" % [ msg ]
  else
    msg
  end
end

.notifier_for_address(address) ⇒ Object



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
# File 'lib/gwtf.rb', line 54

def self.notifier_for_address(address)
  protocol = protocol_for_address(address)

  case protocol_for_address(address)
    when "email"
      return Notifier::Email
    when "boxcar"
      gem 'boxcar_api', '>= 1.2.0'
      require 'boxcar_api'
      require 'gwtf/notifier/boxcar'

      return Notifier::Boxcar
    when "notifo"
      gem 'notifo'
      require 'notifo'
      require 'gwtf/notifier/notifo'

      return Notifier::Notifo
    when "pushover"
      gem 'pushover'
      require 'pushover'
      require 'gwtf/notifier/pushover'

      return Notifier::Pushover
    else
      raise "Do not know how to handle addresses of type #{protocol} for #{address}"
  end
end

.parse_time(timespec) ⇒ Object



19
20
21
# File 'lib/gwtf.rb', line 19

def self.parse_time(timespec)
  Chronic.parse(timespec, :ambiguous_time_range => 8)
end

.projects(root_dir) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/gwtf.rb', line 34

def self.projects(root_dir)
  Dir.entries(root_dir).map do |entry|
    next if entry =~ /^\./
    next unless File.directory?(File.join(root_dir, entry))

    entry
  end.compact.sort
end

.protocol_for_address(address) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/gwtf.rb', line 43

def self.protocol_for_address(address)
  uri = URI.parse(address)

  case uri.scheme
    when nil, "email"
      return "email"
    else
      return uri.scheme
  end
end

.red(msg) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/gwtf.rb', line 110

def self.red(msg)
  if STDOUT.tty? && @@color
    "%s" % [ msg ]
  else
    msg
  end
end

.seconds_to_human(seconds) ⇒ Object

borrowed from ohai, thanks Adam.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gwtf.rb', line 119

def self.seconds_to_human(seconds)
  days = seconds.to_i / 86400
  seconds -= 86400 * days

  hours = seconds.to_i / 3600
  seconds -= 3600 * hours

  minutes = seconds.to_i / 60
  seconds -= 60 * minutes

  if days > 1
    return sprintf("%d days %d hours %d minutes %d seconds", days, hours, minutes, seconds)
  elsif days == 1
    return sprintf("%d day %d hours %d minutes %d seconds", days, hours, minutes, seconds)
  elsif hours > 0
    return sprintf("%d hours %d minutes %d seconds", hours, minutes, seconds)
  elsif minutes > 0
    return sprintf("%d minutes %d seconds", minutes, seconds)
  else
    return sprintf("%d seconds", seconds)
  end
end

.yellow(msg) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/gwtf.rb', line 102

def self.yellow(msg)
  if STDOUT.tty? && @@color
    "%s" % [ msg ]
  else
    msg
  end
end