Class: OPM_Alerts

Inherits:
Object
  • Object
show all
Defined in:
lib/OPM_Alerts/opm_alerts.rb

Instance Method Summary collapse

Instance Method Details

#dieObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/OPM_Alerts/opm_alerts.rb', line 10

def die
  puts <<-EOS
opm-status

USAGE:

\topm-status [wrapping-width]

Returns U.S. Office of Personnel Management status Alerts.

[wrapping-width] is an integer to limit the width of the descriptions
Defaults to: 40

EOS

  exit
end

#run(params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/OPM_Alerts/opm_alerts.rb', line 28

def run(params)
  die if params[0] == "-h"

  width = (params[1] and params[1].to_i) || 40

  doc = Nokogiri::HTML(open(URL))

  date_str = doc.css('#_ctl0__ctl0_DisplayDateSpan').text.strip
  begin
    date = Date.parse(date_str)
    date_str = date.strftime("%x")
  rescue
    date_str = Date.today.strftime("%x") if date_str.length == 0
  end

  title = doc.css('h3').text.strip
  title = "VRE Status" if title.length == 0

  status = doc.css('.statusbox').text.strip

  # Deal with the flaky case where OPM decides to
  # display an image instead of actual text content
  images = doc.xpath("//img")
  status = images[0].attributes["alt"].text.strip if ((status.length == 0) && (images.count == 1))

  status = "Not found" if status.length == 0

  # display results
  puts Utility.wrap_text("#{date_str} - #{title}", width, date_str.length + 3, :outdent)
  puts "-" * width
  puts Utility.wrap_text(status, width)
end