Class: WMATA_Alerts

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

Constant Summary collapse

REPLACEMENTS =
[
  ["p.m.", "pm"], 
  ["a.m.", "am"]
]
FEED_URL =
"http://www.wmata.com/rider_tools/metro_service_status/feeds/rail.xml"

Instance Method Summary collapse

Instance Method Details

#dieObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/WMATA_Alerts/wmata_alerts.rb', line 18

def die
  puts <<-EOS
metro-feed

  USAGE:

  \tmetro-feed [history] [wrapping-width]

  Returns WMATA feed information for metro lines.

  [history] is how many days of feed history to display.
  \tdefaults to 7 days. Can be anything parseable by Chronic gem

  [wrapping-width] is the limit to how wide the text can be
  \tdefaults to 40 characters. should be a positive integer value.

  Requires gems: chronic, htmlentities


EOS

  exit
end

#run(params) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/WMATA_Alerts/wmata_alerts.rb', line 42

def run(params)

  die if params[0] == "-h"

  @cutoff_string = params[0] || "7 days ago"
  @width = params[1] || "40"
  @width = @width.to_s.to_i

  @cutoff = Chronic.parse(@cutoff_string)

  @rss_content = ""

  open(FEED_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 < @cutoff
  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)
      REPLACEMENTS.each do |r|
        message = message.gsub(r[0], r[1])
      end
      subject, *body = message.split(".")
      
      puts Utility.wrap_text(subject+"...", @width)
      body.each do |sentence|
        puts Utility.wrap_text(sentence+".", @width, 3, :all)
      end  

      puts
    end
  end

end