ttc-gps 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 shyndman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = ttc-gps
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 shyndman. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ttc-gps"
8
+ gem.summary = %Q{A service for consuming TTC streetcar GPS data.}
9
+ gem.description = %Q{Uses NextBus data.}
10
+ gem.email = "scotty.hyndman@gmail.com"
11
+ gem.homepage = "http://github.com/shyndman/ttc-gps"
12
+ gem.authors = ["shyndman"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_development_dependency "geokit", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "ttc-gps #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,8 @@
1
+ module TTC
2
+ module UrlTemplates
3
+ STREET_CAR_LOCATIONS = 'http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=ttc&t=0&r=%s'
4
+ ROUTE_CONFIG = 'http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=ttc&r=%s'
5
+ end
6
+
7
+ ROUTES = ('501'...'506').to_a + ('508'...'512').to_a
8
+ end
@@ -0,0 +1,7 @@
1
+ module Kernel
2
+ def Boolean(string)
3
+ return true if string == true || string =~ /^true$/i
4
+ return false if string == false || string.nil? || string =~ /^false$/i
5
+ raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
6
+ end
7
+ end
@@ -0,0 +1,136 @@
1
+ require 'rubygems'
2
+ require 'geokit'
3
+
4
+ # Extensions to Geokit.
5
+ module Geokit
6
+ class Bounds
7
+ def expand_by_radius units
8
+ ret = Bounds.new LatLng.new(@sw.lat,@sw.lng), LatLng.new(@ne.lat, @ne.lng)
9
+ ret.expand_by_radius! units
10
+ ret
11
+ end
12
+
13
+ def expand_by_radius! units
14
+ sw.lat = sw.lat - units
15
+ sw.lng = sw.lng - units
16
+ ne.lon = ne.lon + units
17
+ ne.lng = ne.lng + units
18
+ end
19
+ end
20
+ end
21
+
22
+ module TTC
23
+ # <vehicle lon='-79.3582' secsSinceReport='7' predictable='true' speedKmHr='0.0' dirTag='504_westbound' id='4142' heading='54' lat='43.677082' routeTag='504'/>
24
+ class Vehicle
25
+ attr_accessor :id, :route, :position, :heading, :dir, :secs_since_report, :predictable
26
+
27
+ def to_s
28
+ inspect
29
+ end
30
+
31
+ # Parses an XML element into a Vehicle instance
32
+ def Vehicle.parse_element element
33
+ attrs = element.attributes
34
+
35
+ v = Vehicle.new
36
+ v.id = attrs["id"]
37
+ v.route = attrs["routeTag"]
38
+
39
+ lon = Float(attrs["lon"])
40
+ lat = Float(attrs["lat"])
41
+ v.position = Geokit::LatLng.new(lat, lon)
42
+
43
+ v.heading = Float(attrs["heading"])
44
+ v.dir = attrs["dirTag"]
45
+ v.secs_since_report = Integer(attrs["secsSinceReport"])
46
+ v.predictable = Boolean(attrs["predictable"])
47
+ v
48
+ end
49
+ end
50
+
51
+ # <route lonMin='-79.5444325' title='501- Queen' tag='501' color='e92127' latMax='43.6739102' lonMax='-79.2817125' oppositeColor='ffffff' latMin='43.591831'>
52
+ class Route
53
+ attr_accessor :tag, :title, :bounds, :stop_map, :directions
54
+
55
+ def initialize
56
+ @stop_map = {}
57
+ @directions = {}
58
+ end
59
+
60
+ def add_stop stop
61
+ @stop_map[stop.tag] = stop
62
+ end
63
+
64
+ def add_stop_to_direction direction, stop_tag
65
+ if !@stop_map[stop_tag]
66
+ puts "No stop found with tag, tag='#{stop_tag}'"
67
+ return
68
+ end
69
+
70
+ dir_arr = @directions[direction] || []
71
+ dir_arr << @stop_map[stop_tag]
72
+ @directions[direction] = dir_arr
73
+ end
74
+
75
+ def contains? latlon, radius=0
76
+ @bounds.contains? latlon
77
+ end
78
+
79
+ def get_closest_stops latlon, direction
80
+ @directions[direction].sort do |a, b|
81
+ latlon.distance_to(a.position) <=> latlon.distance_to(b.position)
82
+ end
83
+ end
84
+
85
+ def get_closest_stop latlon, direction
86
+ get_closest_stops[0]
87
+ end
88
+
89
+ def to_s
90
+ inspect
91
+ end
92
+
93
+ def Route.parse_element element
94
+ attrs = element.attributes
95
+
96
+ r = Route.new
97
+ r.tag = attrs["tag"]
98
+ r.title = attrs["title"]
99
+
100
+ lat_min = Float(attrs["latMin"])
101
+ lat_max = Float(attrs["latMax"])
102
+ lon_min = Float(attrs["lonMin"])
103
+ lon_max = Float(attrs["lonMax"])
104
+
105
+ r.bounds = Geokit::Bounds.new(
106
+ Geokit::LatLng.new(lat_min, lon_min),
107
+ Geokit::LatLng.new(lat_max, lon_max))
108
+
109
+ r
110
+ end
111
+ end
112
+
113
+ # <stop lon='-79.5407149' title='Lake Shore Blvd W At 39th' tag='lake39th_e' dirTag='501_eastbound' stopId='6503' lat='43.5928211'/>
114
+ class Stop
115
+ attr_accessor :id, :title, :position, :dir, :tag
116
+
117
+ def to_s
118
+ inspect
119
+ end
120
+
121
+ # Parses an XML element into a Stop instance
122
+ def Stop.parse_element element
123
+ attrs = element.attributes
124
+
125
+ s = Stop.new
126
+ s.id = attrs["stopId"]
127
+ s.title = attrs["title"]
128
+ lat = Float(attrs["lat"])
129
+ lon = Float(attrs["lon"])
130
+ s.position = Geokit::LatLng.new(lat, lon)
131
+ s.dir = attrs["dirTag"]
132
+ s.tag = attrs["tag"]
133
+ s
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,44 @@
1
+ require 'rexml/document'
2
+ require 'open-uri'
3
+
4
+ module TTC
5
+ class TTCService
6
+ def get_route route_tag
7
+ route = nil
8
+
9
+ open TTC::UrlTemplates::ROUTE_CONFIG % route_tag do |file|
10
+ config_xml = REXML::Document.new file
11
+
12
+ route = Route.parse_element config_xml.root.elements['route']
13
+
14
+ config_xml.root.elements.each 'route/stop' do |element|
15
+ route.add_stop TTC::Stop::parse_element element
16
+ end
17
+
18
+ config_xml.root.elements.each 'route/direction' do |element|
19
+ dir = element.attributes["title"].downcase!
20
+ element.elements.each 'stop' do |element|
21
+ route.add_stop_to_direction dir, element.attributes["tag"]
22
+ end
23
+ end
24
+ end
25
+
26
+ route
27
+ end
28
+
29
+ def get_vehicle_locations route_tag
30
+ vehicles = []
31
+
32
+ open TTC::UrlTemplates::STREET_CAR_LOCATIONS % route_tag do |file|
33
+ route_xml = REXML::Document.new file
34
+ root = route_xml.root
35
+
36
+ root.elements.each 'vehicle' do |element|
37
+ vehicles << TTC::Vehicle::parse_element(element)
38
+ end
39
+ end
40
+
41
+ vehicles
42
+ end
43
+ end
44
+ end
data/lib/ttc-gps.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'ttc-gps/extensions'
2
+ require 'ttc-gps/constants'
3
+ require 'ttc-gps/models'
4
+ require 'ttc-gps/service'
5
+ require 'rubygems'
6
+ require 'geokit'
7
+ require 'yaml'
8
+
9
+ include TTC
10
+
11
+ service = TTCService.new
12
+ route = service.get_route(ROUTES[0])
13
+
14
+ me = Geokit::LatLng.new 43.6038156, -79.4930397
15
+ puts route.contains? me
16
+ puts (route.get_closest_stops me, "westbound").to_yaml
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'ttc-gps'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class TestTtcGps < Test::Unit::TestCase
4
+ end
data/ttc-gps.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ttc-gps}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["shyndman"]
12
+ s.date = %q{2010-10-22}
13
+ s.description = %q{Uses NextBus data.}
14
+ s.email = %q{scotty.hyndman@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/ttc-gps.rb",
27
+ "lib/ttc-gps/constants.rb",
28
+ "lib/ttc-gps/extensions.rb",
29
+ "lib/ttc-gps/models.rb",
30
+ "lib/ttc-gps/service.rb",
31
+ "test/helper.rb",
32
+ "test/test_ttc-gps.rb",
33
+ "ttc-gps.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/shyndman/ttc-gps}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{A service for consuming TTC streetcar GPS data.}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_ttc-gps.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ s.add_development_dependency(%q<geokit>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ s.add_dependency(%q<geokit>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ s.add_dependency(%q<geokit>, [">= 0"])
59
+ end
60
+ end
61
+
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttc-gps
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - shyndman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-22 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: geokit
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Uses NextBus data.
50
+ email: scotty.hyndman@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ files:
59
+ - .document
60
+ - .gitignore
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - VERSION
65
+ - lib/ttc-gps.rb
66
+ - lib/ttc-gps/constants.rb
67
+ - lib/ttc-gps/extensions.rb
68
+ - lib/ttc-gps/models.rb
69
+ - lib/ttc-gps/service.rb
70
+ - test/helper.rb
71
+ - test/test_ttc-gps.rb
72
+ - ttc-gps.gemspec
73
+ has_rdoc: true
74
+ homepage: http://github.com/shyndman/ttc-gps
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A service for consuming TTC streetcar GPS data.
107
+ test_files:
108
+ - test/helper.rb
109
+ - test/test_ttc-gps.rb