Class: SiriProxy::Plugin::NextBus

Inherits:
SiriProxy::Plugin
  • Object
show all
Defined in:
lib/siriproxy-nextbus.rb

Overview

Queries Chapel Hill’s next bus service

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NextBus

Returns a new instance of NextBus.



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
# File 'lib/siriproxy-nextbus.rb', line 67

def initialize(config)
  location = get_app_config("next_bus","location")
  if location == nil  || !location.is_a?(String)
    return
  end
  buses = get_app_config("next_bus","routes")
  if buses != nil \
    && buses.respond_to?('each')
    buses.each do |bus_data|
      if bus_data == nil \
        || !bus_data.respond_to?('has_key?') 
        next
      end
      route = bus_data["route"]
      response = bus_data["response"]
      stop = bus_data["stop"]
      direction = bus_data["direction"]
      listeners = bus_data["listeners"]
      if route == nil || stop == nil || direction == nil || listeners == nil 
        next
      end
      add_listeners(listeners) {show_next_bus(route,direction,stop,response)}
    end
  end
end

Instance Method Details

#add_listeners(regexps, options = {}, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/siriproxy-nextbus.rb', line 50

def add_listeners(regexps,options= {},&block) 
  if regexps == nil 
    return
  end
  if !regexps.respond_to?('each') 
    #may be a scalar value
    regexps = [regexps]
  end
  regexps.each do |regexp|
    if (regexp = ensure_regexp(regexp)) == nil \
      || !regexp.is_a?(Regexp)
      next
    end
    listeners[regexp] = {:block => block,   within_state: ([options[:within_state]].flatten)}
  end
end

#ensure_regexp(regexp) ⇒ Object

redundant: this is defined in my custom plungin manage siriproxypm-clientcachestate



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/siriproxy-nextbus.rb', line 34

def ensure_regexp(regexp) 
  if regexp != nil
    regexp.is_a?(String) 
    if regexp[0] == '/'
      #try to make it into a regexp
      regexp = eval regexp
    elsif  
      regexp = Regexp.new("^\s*#{regexp}",true);
    end
  end
  if regexp == nil || !regexp.is_a?(Regexp)
    regexp = nil
  end
  return regexp
end

#get_app_config(*args) ⇒ Object

redundant: this is defined in my custom plungin manage siriproxypm-clientcachestae



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/siriproxy-nextbus.rb', line 14

def get_app_config(*args)
  result = $APP_CONFIG
  if args != nil \
    && (first_arg = args.shift) != nil 
    eval "result = result.#{first_arg}"
    args.each do |arg|
      if arg == nil \
        || result  == nil \
        || !result.respond_to?('has_key?')\
        || !result.has_key?(arg)
        result = nil
        break
      end          
      result = result[arg]
    end
  end
  return result
end

#get_next_bus(route, direction, stop) ⇒ Object



110
111
112
113
114
# File 'lib/siriproxy-nextbus.rb', line 110

def get_next_bus(route,direction,stop) 
  url = "http://www.nextbus.com/predictor/fancyBookmarkablePredictionLayer.shtml?a=chapel-hill&r=#{route}&d=#{direction}&s=#{stop}"
  path = "//td[@class='predictionNumberForFirstPred']"
  return scrape_url(url,path)
end

#scrape_url(url, path) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/siriproxy-nextbus.rb', line 116

def scrape_url(url,path)
  log "Scraping #{url}"
  result = false
  doc = Nokogiri::HTML(open(url))
  doc.xpath(path).each do |node|
    result = node.content
    break
  end
  log "Scraped #{result}"
  return result
end

#show_next_bus(route, direction, stop, response) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/siriproxy-nextbus.rb', line 95

def show_next_bus(route,direction,stop,response)
  say "Let me check" 
  minutes = get_next_bus(route,direction,stop)
  if minutes  == nil || !minutes.is_a?(String)
    say "Sorry. I could not find the next bus information"
  else
    if response == nil || !response.is_a?(String)
      reponse = "The next #{route} bus is in %s minutes"
    end
    response = sprintf(response,minutes.strip)
    say response
  end
  request_completed
end