Class: WMATA_Alerts

Inherits:
Geeklet show all
Defined in:
lib/WMATA_Alerts/wmata_alerts.rb

Instance Method Summary collapse

Methods inherited from Geeklet

#show_help

Methods included from Configurable

#add_overrides, #command_parser, #configurableValue, #configurations, included, #isHelp?, #registerConfiguration

Instance Method Details

#descriptionObject



17
18
19
# File 'lib/WMATA_Alerts/wmata_alerts.rb', line 17

def description
  "Returns WMATA feed information for metro lines."
end

#nameObject



13
14
15
# File 'lib/WMATA_Alerts/wmata_alerts.rb', line 13

def name
  "WMATA_Alerts"
end

#run(params) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/WMATA_Alerts/wmata_alerts.rb', line 29

def run(params)
  super(:WMATA_Alerts, params)

  rss_content = ""

  open(configurableValue(:WMATA_Alerts, :URL)) do |f|
    rss_content = f.read
  end

  rss = RSS::Parser.parse(rss_content, false)

  item_hash = {}

  # filter down to the items we want.
  rss.items.each do |item|
    item_hash[item.pubDate] = item unless item.pubDate < Chronic.parse(configurableValue(:WMATA_Alerts, :history))
  end

  if item_hash.empty?
    puts "No data from WMATA feed."
  else
    coder = HTMLEntities.new
    item_hash.keys.sort.reverse.each do |key|
      item = item_hash[key]

      title = "#{item.pubDate.strftime('%x')} - #{item.title}"
      puts title
      puts ("-" * title.length)

      message = coder.decode(item.description)
      message = translate(message)
      subject, *body = message.split(".")
      
      width = configurableValue(:WMATA_Alerts, :width)
      puts Utility.wrap_text(subject + "...", width)
      body.each do |sentence|
        puts Utility.wrap_text(sentence + ".", width, 3, :all)
      end

      puts
    end
  end

end

#translate(message) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/WMATA_Alerts/wmata_alerts.rb', line 21

def translate(message)
  [
    ["p.m.", "pm"], 
    ["a.m.", "am"]
  ].each { |r| message.gsub!(r[0], r[1]) }
  message
end