onebusaway 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.1)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.1)
19
+ rcov
20
+ shoulda
21
+
22
+ METADATA
23
+ version: 1.0.6
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jeff Ching
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.
@@ -0,0 +1,37 @@
1
+ OneBusAway API
2
+ --------------
3
+
4
+ Unofficial Ruby library for interacting with the [OneBusAway API](http://code.google.com/p/onebusaway/wiki/OneBusAwayApiReference)
5
+
6
+ ### Documentation & Requirements
7
+ * REXML
8
+
9
+ ### Examples
10
+
11
+ Basic usage:
12
+
13
+ require 'rubygems'
14
+ require 'onebusaway'
15
+
16
+ Onebusaway.api_key = 'YOUR_API_KEY'
17
+
18
+ # find a stop by id - returns a Onebusaway::Stop instance
19
+ stop = Onebusaway.stop_by_id(:id => "1_10020")
20
+
21
+ # find a route by id - returns a Onebusaway::Route instance
22
+ route = Onebusaway.route_by_id(:id => "1_30")
23
+
24
+ # find stops nearby a given latitude and longitude
25
+ # - returns an array of Onebusaway::Stop instances
26
+ stops = Onebusaway.stops_for_location(:lat => "47.66", :lon => "-122.29")
27
+
28
+ # find routes nearby a given latitude and longitude
29
+ # - returns an array of Onebusaway::Route instances
30
+ routes = Onebusaway.routes_for_location(:lat => "47.66", :lon => "-122.29")
31
+
32
+ # find stops for a route - returns an array of Onebusaway::Stop instances
33
+ stops = Onebusaway.stops_for_route(:id => "1_30")
34
+
35
+ # find arrivals/departures for a given stop
36
+ # - returns an array of Onebusaway::ArrivalAndDeparture instances
37
+ arrivals = Onebusaway.arrivals_and_departures_for_stop(:id => "1_570")
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "onebusaway"
16
+ gem.homepage = "http://github.com/chingor13/onebusaway"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Unofficial Ruby library for interacting with the OneBusAway API}
19
+ gem.description = %Q{Unofficial Ruby library for interacting with the OneBusAway API. See http://code.google.com/p/onebusaway/wiki/OneBusAwayRestApi}
20
+ gem.email = "ching.jeff@gmail.com"
21
+ gem.authors = ["Jeff Ching"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "onebusaway #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,181 @@
1
+ module Onebusaway
2
+
3
+ API_BASE = "http://api.onebusaway.org/api/where/"
4
+
5
+ class << self
6
+ def api_key=(key)
7
+ @api_key = key
8
+ end
9
+
10
+ def stop_by_id(params)
11
+ raise if params[:id].nil?
12
+
13
+ xml = request('stop', params)
14
+ stop = Stop.from_xml(xml)
15
+ end
16
+
17
+ def route_by_id(params)
18
+ raise if params[:id].nil?
19
+
20
+ xml = request('route', params)
21
+ route = Route.from_xml(xml)
22
+ end
23
+
24
+ def stops_for_location(params)
25
+ raise if params[:lat].nil? || params[:lon].nil?
26
+
27
+ doc = request('stops-for-location', params)
28
+ stops = []
29
+ doc.elements.each("stops/stop") do |stop_el|
30
+ stops << Stop.from_xml(stop_el)
31
+ end
32
+ stops
33
+ end
34
+
35
+ def routes_for_location(params)
36
+ raise if params[:lat].nil? || params[:lon].nil?
37
+
38
+ doc = request('routes-for-location', params)
39
+ routes = []
40
+ doc.elements.each("routes/route") do |route_el|
41
+ routes << Route.from_xml(route_el)
42
+ end
43
+ routes
44
+ end
45
+
46
+ def stops_for_route(params)
47
+ raise if params[:id].nil?
48
+
49
+ doc = request('stops-for-route', params)
50
+ stops = []
51
+ doc.elements.each("stops/stop") do |stop_el|
52
+ stops << Stop.from_xml(stop_el)
53
+ end
54
+ stops
55
+ end
56
+
57
+ def arrivals_and_departures_for_stop(params)
58
+ raise if params[:id].nil?
59
+
60
+ doc = request('arrivals-and-departures-for-stop', params)
61
+ arrivals = []
62
+ doc.elements.each("arrivalsAndDepartures/arrivalAndDeparture") do |arrival_el|
63
+ arrivals << ArrivalAndDeparture.from_xml(arrival_el)
64
+ end
65
+ arrivals
66
+ end
67
+
68
+ private
69
+
70
+ def api_url(call, options = {})
71
+ url = API_BASE + call
72
+ id = options.delete(:id)
73
+ if id
74
+ url += "/" + id
75
+ end
76
+ url += ".xml?"
77
+ options[:key] = @api_key
78
+ url += options.to_a.map{|pair| "#{pair[0]}=#{pair[1]}"}.join("&")
79
+ url
80
+ end
81
+
82
+ def request(call, url_options)
83
+ url = api_url(call, url_options)
84
+
85
+ doc = REXML::Document.new(open(url))
86
+ root = doc.root
87
+ status_code = root.elements["code"].text
88
+ status_text = root.elements["text"].text
89
+
90
+ # failed status
91
+ unless /2\d{2}/.match(status_code)
92
+ raise "Request failed (#{status_code}): #{status_text}"
93
+ end
94
+
95
+ return root.elements["data"]
96
+ end
97
+
98
+ end
99
+
100
+ class Base
101
+ def self.from_xml(xml_or_data)
102
+ xml_or_data = REXML::Document.new(xml_or_data).root if xml_or_data.is_a?(String)
103
+ self.parse(xml_or_data)
104
+ end
105
+
106
+ def self.parse(data)
107
+ raise "not implemented"
108
+ end
109
+ end
110
+
111
+ class Agency < Base
112
+ attr_accessor :id, :name, :url, :timezone, :lang, :phone
113
+ def self.parse(data)
114
+ agency = self.new
115
+ [:id, :name, :url, :timezone, :lang, :phone].each do |attr|
116
+ value = data.elements[attr.to_s]
117
+ agency.send("#{attr}=", value.text) if value
118
+ end
119
+ agency
120
+ end
121
+ end
122
+
123
+ class ArrivalAndDeparture < Base
124
+ attr_accessor :routeId, :routeShortName, :tripId, :tripHeadsign, :stopId, :predictedArrivalTime, :scheduledArrivalTime, :predictedDepartureTime, :scheduledDepartureTime, :status
125
+ def self.parse(data)
126
+ arrival = self.new
127
+ [:routeId, :routeShortName, :tripId, :tripHeadsign, :stopId, :predictedArrivalTime, :scheduledArrivalTime, :predictedDepartureTime, :scheduledDepartureTime, :status].each do |attr|
128
+ value = data.elements[attr.to_s]
129
+ arrival.send("#{attr}=", value.text) if value
130
+ end
131
+ arrival
132
+ end
133
+
134
+ def minutes_from_now
135
+ @minutes_from_now ||= begin
136
+ at = predictedArrivalTime.to_i
137
+ if at == 0
138
+ # no predicted time, use scheduled
139
+ (scheduledArrivalTime.to_i/1000 - Time.now.to_i) / 60
140
+ else
141
+ (predictedArrivalTime.to_i/1000 - Time.now.to_i) / 60
142
+ end
143
+ end
144
+ @minutes_from_now
145
+ end
146
+ end
147
+
148
+ class EncodedPolyline < Base
149
+ attr_accessor :points, :length, :levels
150
+ end
151
+
152
+ class Route < Base
153
+ attr_accessor :id, :shortName, :longName, :description, :type, :url, :agency
154
+ def self.parse(data)
155
+ route = self.new
156
+ [:id, :shortName, :longName, :description, :type, :url].each do |attr|
157
+ value = data.elements[attr.to_s]
158
+ route.send("#{attr}=", value.text) if value
159
+ end
160
+ route.agency = Agency.parse(data.elements["agency"])
161
+ route
162
+ end
163
+ end
164
+
165
+ class Stop < Base
166
+ attr_accessor :id, :lat, :lon, :direction, :name, :code, :locationType, :routes
167
+ def self.parse(data)
168
+ stop = self.new
169
+ [:id, :lat, :lon, :direction, :name, :code, :locationType].each do |attr|
170
+ value = data.elements[attr.to_s]
171
+ stop.send("#{attr}=", value.text) if value
172
+ end
173
+ stop.routes ||= []
174
+ data.elements.each("routes/route") do |route_el|
175
+ stop.routes << Route.parse(route_el)
176
+ end
177
+ stop
178
+ end
179
+ end
180
+ end
181
+
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'onebusaway'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,76 @@
1
+ module Onebusaway
2
+ module SampleData
3
+ STOP_DATA = <<EOS
4
+ <response>
5
+ <version>1.0</version>
6
+ <code>200</code>
7
+ <text>OK</text>
8
+ <data class="stop">
9
+ <id>1_10020</id>
10
+ <lat>47.6685829</lat>
11
+ <lon>-122.2883</lon>
12
+ <direction>W</direction>
13
+ <name>NE 55th St &amp; 37th Ave NE</name>
14
+ <code>10020</code>
15
+ <locationType>0</locationType>
16
+ <routes>
17
+ <route>
18
+ <id>1_30</id>
19
+ <shortName>30</shortName>
20
+ <description>Sandpoint/U-Dist/Seattle Center</description>
21
+ <type>3</type>
22
+ <url>http://metro.kingcounty.gov/tops/bus/schedules/s030_0_.html</url>
23
+ <agency>
24
+ <id>1</id>
25
+ <name>Metro Transit</name>
26
+ <url>http://metro.kingcounty.gov</url>
27
+ <timezone>America/Los_Angeles</timezone>
28
+ <lang>en</lang>
29
+ <phone>206-553-3000</phone>
30
+ </agency>
31
+ </route>
32
+ <route>
33
+ <id>1_74</id>
34
+ <shortName>74</shortName>
35
+ <description>Sandpoint/U-Dist/CPS</description>
36
+ <type>3</type>
37
+ <url>http://metro.kingcounty.gov/tops/bus/schedules/s074_0_.html</url>
38
+ <agency>
39
+ <id>1</id>
40
+ <name>Metro Transit</name>
41
+ <url>http://metro.kingcounty.gov</url>
42
+ <timezone>America/Los_Angeles</timezone>
43
+ <lang>en</lang>
44
+ <phone>206-553-3000</phone>
45
+ </agency>
46
+ </route>
47
+ </routes>
48
+ </data>
49
+ </response>
50
+ EOS
51
+ ROUTE_DATA = <<EOS
52
+ <response>
53
+ <version>1.0</version>
54
+ <code>200</code>
55
+ <text>OK</text>
56
+ <data class="route">
57
+ <id>1_30</id>
58
+ <shortName>30</shortName>
59
+
60
+ <description>Sandpoint/U-Dist/Seattle Center</description>
61
+ <type>3</type>
62
+ <url>http://metro.kingcounty.gov/tops/bus/schedules/s030_0_.html</url>
63
+ <agency>
64
+ <id>1</id>
65
+ <name>Metro Transit</name>
66
+
67
+ <url>http://metro.kingcounty.gov</url>
68
+ <timezone>America/Los_Angeles</timezone>
69
+ <lang>en</lang>
70
+ <phone>206-553-3000</phone>
71
+ </agency>
72
+ </data>
73
+ </response>
74
+ EOS
75
+ end
76
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestOnebusaway < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ #flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onebusaway
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
+ - Jeff Ching
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-17 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: shoulda
24
+ version_requirements: &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
+ requirement: *id001
34
+ type: :development
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ name: bundler
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ requirement: *id002
50
+ type: :development
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ name: jeweler
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 1
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 1
64
+ version: 1.5.1
65
+ requirement: *id003
66
+ type: :development
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ name: rcov
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirement: *id004
80
+ type: :development
81
+ description: Unofficial Ruby library for interacting with the OneBusAway API. See http://code.google.com/p/onebusaway/wiki/OneBusAwayRestApi
82
+ email: ching.jeff@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.markdown
90
+ files:
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.markdown
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/onebusaway.rb
98
+ - test/helper.rb
99
+ - test/sample_data.rb
100
+ - test/unit/test_onebusaway.rb
101
+ has_rdoc: true
102
+ homepage: http://github.com/chingor13/onebusaway
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.7
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Unofficial Ruby library for interacting with the OneBusAway API
135
+ test_files:
136
+ - test/helper.rb
137
+ - test/sample_data.rb
138
+ - test/unit/test_onebusaway.rb