siriproxy-nextbus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in siriproxy-hello.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ port: 443
2
+ log_level: 1
3
+ plugins:
4
+ # NOTE: run bundle after changing plugin configurations to update required gems
5
+
6
+ - name: 'NextBus'
7
+ path: './plugins/siriproxy-nextbus'
8
+
9
+
10
+ next_bus:
11
+ location: 'chapel-hill'
12
+
13
+ routes:
14
+ - route: 'NS'
15
+ stop: 'mannunch'
16
+ direction: 'N'
17
+ listeners: ["bus","/when.*next.*northbound.*(tennis|n s|ns|and S).*bus/i","/when.*next.*northboud.*(and|10).*sbus/i", "/when.*next.*(n s|ns).*bus.*(return|home)/i" ]
18
+ response: "The next NS bus returns in %s minutes"
19
+
20
+
21
+ - route: 'NS'
22
+ stop: 'airpwest_s'
23
+ direction: 'S'
24
+ listeners: ["/when.*next.*(tennis|n s|ns|and S).*bus/i", "/when.*next.*(10|and).*sbus/i"]
25
+ response: "The next NS bus leaves in %s minutes"
@@ -0,0 +1,131 @@
1
+ require 'cora'
2
+ require 'siri_objects'
3
+ require 'nokogiri'
4
+ require 'open-uri'
5
+
6
+ #######
7
+ # Queries Chapel Hill's next bus service
8
+ ######
9
+
10
+ class SiriProxy::Plugin::NextBus < SiriProxy::Plugin
11
+
12
+
13
+ #redundant: this is defined in my custom plungin manage siriproxypm-clientcachestae
14
+ def get_app_config(*args)
15
+ result = $APP_CONFIG
16
+ if args != nil \
17
+ && (first_arg = args.shift) != nil
18
+ eval "result = result.#{first_arg}"
19
+ args.each do |arg|
20
+ if arg == nil \
21
+ || result == nil \
22
+ || !result.respond_to?('has_key?')\
23
+ || !result.has_key?(arg)
24
+ result = nil
25
+ break
26
+ end
27
+ result = result[arg]
28
+ end
29
+ end
30
+ return result
31
+ end
32
+
33
+ #redundant: this is defined in my custom plungin manage siriproxypm-clientcachestate
34
+ def ensure_regexp(regexp)
35
+ if regexp != nil
36
+ regexp.is_a?(String)
37
+ if regexp[0] == '/'
38
+ #try to make it into a regexp
39
+ regexp = eval regexp
40
+ elsif
41
+ regexp = Regexp.new("^\s*#{regexp}",true);
42
+ end
43
+ end
44
+ if regexp == nil || !regexp.is_a?(Regexp)
45
+ regexp = nil
46
+ end
47
+ return regexp
48
+ end
49
+
50
+ def add_listeners(regexps,options= {},&block)
51
+ if regexps == nil
52
+ return
53
+ end
54
+ if !regexps.respond_to?('each')
55
+ #may be a scalar value
56
+ regexps = [regexps]
57
+ end
58
+ regexps.each do |regexp|
59
+ if (regexp = ensure_regexp(regexp)) == nil \
60
+ || !regexp.is_a?(Regexp)
61
+ next
62
+ end
63
+ listeners[regexp] = {:block => block, within_state: ([options[:within_state]].flatten)}
64
+ end
65
+ end
66
+
67
+ def initialize(config)
68
+ location = get_app_config("next_bus","location")
69
+ if location == nil || !location.is_a?(String)
70
+ return
71
+ end
72
+ buses = get_app_config("next_bus","routes")
73
+ if buses != nil \
74
+ && buses.respond_to?('each')
75
+ buses.each do |bus_data|
76
+ if bus_data == nil \
77
+ || !bus_data.respond_to?('has_key?')
78
+ next
79
+ end
80
+ route = bus_data["route"]
81
+ response = bus_data["response"]
82
+ stop = bus_data["stop"]
83
+ direction = bus_data["direction"]
84
+ listeners = bus_data["listeners"]
85
+ if route == nil || stop == nil || direction == nil || listeners == nil
86
+ next
87
+ end
88
+ add_listeners(listeners) {show_next_bus(route,direction,stop,response)}
89
+ end
90
+ end
91
+ end
92
+
93
+
94
+
95
+ def show_next_bus(route,direction,stop,response)
96
+ say "Let me check"
97
+ minutes = get_next_bus(route,direction,stop)
98
+ if minutes == nil || !minutes.is_a?(String)
99
+ say "Sorry. I could not find the next bus information"
100
+ else
101
+ if response == nil || !response.is_a?(String)
102
+ reponse = "The next #{route} bus is in %s minutes"
103
+ end
104
+ response = sprintf(response,minutes.strip)
105
+ say response
106
+ end
107
+ request_completed
108
+ end
109
+
110
+ def get_next_bus(route,direction,stop)
111
+ url = "http://www.nextbus.com/predictor/fancyBookmarkablePredictionLayer.shtml?a=chapel-hill&r=#{route}&d=#{direction}&s=#{stop}"
112
+ path = "//td[@class='predictionNumberForFirstPred']"
113
+ return scrape_url(url,path)
114
+ end
115
+
116
+ def scrape_url(url,path)
117
+ log "Scraping #{url}"
118
+ result = false
119
+ doc = Nokogiri::HTML(open(url))
120
+ doc.xpath(path).each do |node|
121
+ result = node.content
122
+ break
123
+ end
124
+ log "Scraped #{result}"
125
+ return result
126
+ end
127
+
128
+
129
+
130
+
131
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "siriproxy-nextbus"
6
+ s.version = "0.0.1"
7
+ s.authors = ["litlfred"]
8
+ s.email = [""]
9
+ s.homepage = ""
10
+ s.summary = %q{Get Next Bus Information for Chapel Hill}
11
+ s.description = %q{Queries against http://www.nextbus.com to get the next bus}
12
+
13
+ s.rubyforge_project = "siriproxy-nextbus"
14
+
15
+ s.files = `git ls-files 2> /dev/null`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/* 2> /dev/null`.split("\n")
17
+ s.executables = `git ls-files -- bin/* 2> /dev/null`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for hello:
21
+ # s.add_development_dependency "rspec"
22
+ s.add_dependency "nokogiri"
23
+ end
24
+
25
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siriproxy-nextbus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - litlfred
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &77413630 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *77413630
25
+ description: Queries against http://www.nextbus.com to get the next bus
26
+ email:
27
+ - ''
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - config.example.nextbus.yml
36
+ - lib/siriproxy-nextbus.rb
37
+ - siriproxy-nextbus.gemspec
38
+ homepage: ''
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project: siriproxy-nextbus
58
+ rubygems_version: 1.8.10
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Get Next Bus Information for Chapel Hill
62
+ test_files: []