wmata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -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 Jeremy McAnally
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,17 @@
1
+ = wmata
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 Jeremy McAnally. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "wmata"
8
+ gem.summary = %Q{A gem for the WMATA API}
9
+ gem.description = %Q{A gem for accessing the WMATA API}
10
+ gem.email = "jeremymcanally@gmail.com"
11
+ gem.homepage = "http://github.com/jm/wmata"
12
+ gem.authors = ["Jeremy McAnally"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "wmata #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,37 @@
1
+ module WMATA
2
+ class Resource
3
+ class <<self
4
+ def get_all(params={})
5
+ url = WMATA.base_url % [service, endpoint, to_query_string(params)]
6
+ HTTParty.get(url).first.last.map {|values| new(values) }
7
+ end
8
+
9
+ alias_method :find_all, :get_all
10
+
11
+ def service(value=nil)
12
+ @service = value if value
13
+ @service || "#{self.name.capitalize}s"
14
+ end
15
+
16
+ def endpoint(value=nil)
17
+ @endpoint = value if value
18
+ @endpoint || "#{self.name.capitalize}s"
19
+ end
20
+
21
+ def to_query_string(params)
22
+ "&" + params.map {|k, v| "#{k.to_s}=#{v}"}.join("&")
23
+ end
24
+ end
25
+
26
+ attr_reader :attrs
27
+
28
+ def initialize(attrs={})
29
+ @attrs = attrs
30
+ end
31
+
32
+ def method_missing(m, *args)
33
+ camel_cased = m.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
34
+ @attrs[m.to_s] or @attrs[camel_cased] or super
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ module WMATA
2
+ class ElevatorIncident < Resource
3
+ service "Incidents"
4
+ endpoint "ElevatorIncidents"
5
+
6
+ def self.get_by_station(affected_station)
7
+ @incidents ||= get_all
8
+ @incidents.select {|i| i.station_code == affected_station.to_s }.pop
9
+ end
10
+
11
+ def affected_station
12
+ Station.get(@attrs['StationCode'])
13
+ end
14
+
15
+ alias_method :station, :affected_station
16
+
17
+ def date_out_of_service
18
+ Time.parse(@attrs['DateOutOfServ'])
19
+ end
20
+
21
+ def date_updated
22
+ Time.parse(@attrs['DateUpdated'])
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ module WMATA
2
+ class Line < Resource
3
+ service "Rail"
4
+ endpoint "JLines"
5
+
6
+ class <<self
7
+ alias_method :get_all_without_memoize, :get_all
8
+
9
+ # Memoize this since (a) there's no way to ask for just one line and
10
+ # (b) they're unlikely to change while we're doing a request.
11
+ def get_all(params)
12
+ @lines ||= get_all_without_memoize(params)
13
+ end
14
+ end
15
+
16
+ def start_station
17
+ @start_station ||= Station.get(@attrs['StartStationCode'])
18
+ end
19
+
20
+ def end_station
21
+ @end_station ||= Station.get(@attrs['EndStationCode'])
22
+ end
23
+
24
+ def internal_destinations
25
+ [@attrs['InternalDestination1'], @attrs['InternalDestination2']].compact.map do |s|
26
+ Station.get(s)
27
+ end
28
+ end
29
+
30
+ def rail_incidents
31
+ @incidents ||= RailIncident.get_by_line(self)
32
+ end
33
+
34
+ alias_method :incidents, :rail_incidents
35
+
36
+ def route
37
+ Station.get_on_line(code)
38
+ end
39
+
40
+ alias_method :stations, :route
41
+
42
+ def get(code)
43
+ get_all.select {|l| l.code == code}.pop
44
+ end
45
+
46
+ def code
47
+ @attrs['LineCode']
48
+ end
49
+
50
+ def to_s
51
+ code
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,18 @@
1
+ module WMATA
2
+ class PathSegment < Resource
3
+ service "Rail"
4
+ endpoint "JPath"
5
+
6
+ def station
7
+ @station ||= Station.get(@attrs['StationCode'])
8
+ end
9
+
10
+ def line
11
+ @line ||= Line.get(@attrs['LineCode'])
12
+ end
13
+
14
+ def index
15
+ @attrs['SeqNum']
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ module WMATA
2
+ class Prediction < Resource
3
+ service "StationPrediction"
4
+
5
+ def self.predict_for(station_code)
6
+ url = WMATA.base_url % [service, "GetPrediction/#{station_code}", ""]
7
+ HTTParty.get(url).first.last.map {|values| new(values) }
8
+ end
9
+
10
+ def location
11
+ @location ||= Station.get(@attrs['LocationCode'])
12
+ end
13
+
14
+ alias_method :station, :location
15
+
16
+ def destination
17
+ @destination ||= Station.get(@attrs['DestinationCode'])
18
+ end
19
+
20
+ def line_code
21
+ @attrs['Line']
22
+ end
23
+
24
+ def line
25
+ @line ||= Line.get(@attrs['Line'])
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ module WMATA
2
+ class RailIncident < Resource
3
+ service "Incidents"
4
+ endpoint "Incidents"
5
+
6
+ def self.get_by_line(line)
7
+ @incidents ||= get_all
8
+ @incidents.select {|i| i.line_codes_affected.include?(line.to_s)}
9
+ end
10
+
11
+ def date_updated
12
+ Time.parse(@attrs['DateUpdated'])
13
+ end
14
+
15
+ def line_codes_affected
16
+ @attrs['LinesAffected'].split(";").reject {|s| s.empty? || s.nil?}
17
+ end
18
+
19
+ def lines_affected
20
+ @lines_affected = line_codes_affected.map {|l| Line.get(l)}
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,48 @@
1
+ module WMATA
2
+ class Station < Resource
3
+ service "Rail"
4
+ endpoint "JStations"
5
+
6
+ SYMBOL_TO_LINES_MAP = {
7
+ :red => "RD",
8
+ :blue => "BL",
9
+ :orange => "OR",
10
+ :green => "GR",
11
+ :yellow => "YE"
12
+ }
13
+
14
+ def self.get_on_line(line)
15
+ line = SYMBOL_TO_LINES_MAP[line] if line.is_a?(Symbol)
16
+ get_all("LineCode" => line.to_s)
17
+ end
18
+
19
+ def self.get(code)
20
+ url = WMATA.base_url % [service, "JStationInfo", to_query_string("StationCode" => code)]
21
+ new(HTTParty.get(url))
22
+ end
23
+
24
+ def codes
25
+ [@attrs['Code'], @attrs['StationTogether1'], @attrs['StationTogether2']].compact
26
+ end
27
+
28
+ def line_codes
29
+ [@attrs['LineCode1'], @attrs['LineCode2'], @attrs['LineCode3'], @attrs['LineCode4']].compact
30
+ end
31
+
32
+ def lines
33
+ @lines ||= line_codes.map {|l| Line.get(l)}
34
+ end
35
+
36
+ def predictions
37
+ @predictions ||= Prediction.predict_for(self)
38
+ end
39
+
40
+ def elevator_incidents
41
+ @elevator_incidents ||= ElevatorIncident.get_by_station(self)
42
+ end
43
+
44
+ def to_s
45
+ @attrs['Code']
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,14 @@
1
+ module WMATA
2
+ class StationEntrance < Resource
3
+ service "Rail"
4
+ endpoint "JStationEntrances"
5
+
6
+ def station_codes
7
+ [@attrs['StationCode1'], @attrs['StationCode2']].compact
8
+ end
9
+
10
+ def station
11
+ @station ||= Station.get(station_codes.first)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,68 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'httparty'
5
+ require 'forwardable'
6
+
7
+ require 'resource'
8
+ require 'resources/line'
9
+ require 'resources/station'
10
+ require 'resources/rail_incident'
11
+ require 'resources/elevator_incident'
12
+ require 'resources/prediction'
13
+ require 'resources/path_segment'
14
+ require 'resources/station_entrance'
15
+
16
+ module WMATA
17
+ BASE_URL = "http://api.wmata.com/%s.svc/json/%s?api_key=%s%s"
18
+
19
+ class <<self
20
+ attr_accessor :api_key
21
+
22
+ def base_url
23
+ BASE_URL.dup % ["%s", "%s", @api_key, "%s"]
24
+ end
25
+
26
+ def lines
27
+ Line.get_all
28
+ end
29
+
30
+ def stations
31
+ Station.get_all
32
+ end
33
+
34
+ def station(code)
35
+ Station.get(code)
36
+ end
37
+
38
+ def stations_on_line(code)
39
+ Station.get_on_line(code)
40
+ end
41
+
42
+ def predict_for(station="All")
43
+ Prediction.predict_for(station)
44
+ end
45
+
46
+ def rail_incidents
47
+ RailIncident.get_all
48
+ end
49
+
50
+ alias_method :incidents, :rail_incidents
51
+
52
+ def elevator_incidents
53
+ ElevatorIncident.get_all
54
+ end
55
+
56
+ def build_path(from, to)
57
+ PathSegment.get_all("FromStationCode" => from, "ToStationCode" => to)
58
+ end
59
+
60
+ def entrances(from={})
61
+ params = {:lat => 0, :lon => 0, :radius => 500}.merge(from)
62
+ StationEntrance.get_all(params)
63
+ end
64
+
65
+ alias_method :station_entrances, :entrances
66
+ alias_method :entrances_near, :entrances
67
+ end
68
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'flexmock'
4
+ require 'flexmock/test_unit'
5
+ require 'ostruct'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+
10
+ require 'wmata'
11
+
12
+ class Test::Unit::TestCase
13
+
14
+ end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ class TestElevatorIncident < Test::Unit::TestCase
4
+ def test_get_by_station
5
+ fake_incidents = []
6
+ 5.times {|i| fake_incidents << OpenStruct.new(:station_code => "A#{i}")}
7
+ flexmock(WMATA::ElevatorIncident).should_receive(:get_all).and_return(fake_incidents)
8
+
9
+ assert_equal "A2", WMATA::ElevatorIncident.get_by_station("A2").station_code
10
+ end
11
+
12
+ def test_affected_station
13
+ flexmock(WMATA::Station).should_receive(:get).and_return(OpenStruct.new(:name => "Winner"))
14
+ incident = WMATA::ElevatorIncident.new("StationCode" => "A4")
15
+
16
+ assert_equal "Winner", incident.affected_station.name
17
+ end
18
+
19
+ def test_date_out_of_service
20
+ incident = WMATA::ElevatorIncident.new("DateOutOfServ" => "2010-07-27T00:00:00")
21
+ assert_equal Time.parse("07/27/2010"), incident.date_out_of_service
22
+ end
23
+
24
+ def test_date_updated
25
+ incident = WMATA::ElevatorIncident.new("DateUpdated" => "2010-07-27T00:00:00")
26
+ assert_equal Time.parse("07/27/2010"), incident.date_updated
27
+ end
28
+ end
@@ -0,0 +1,61 @@
1
+ class TestLine < Test::Unit::TestCase
2
+ def test_start_station
3
+ flexmock(WMATA::Station).should_receive(:get).and_return(OpenStruct.new(:name => "Winner"))
4
+ line = WMATA::Line.new("StartStationCode" => "A4")
5
+
6
+ assert_equal "Winner", line.start_station.name
7
+ end
8
+
9
+ def test_end_station
10
+ flexmock(WMATA::Station).should_receive(:get).and_return(OpenStruct.new(:name => "Winner"))
11
+ line = WMATA::Line.new("EndStationCode" => "A4")
12
+
13
+ assert_equal "Winner", line.end_station.name
14
+ end
15
+
16
+ def test_internal_destinations
17
+ flexmock(WMATA::Station).should_receive(:get).with("A1").and_return(OpenStruct.new(:name => "Winner"))
18
+ flexmock(WMATA::Station).should_receive(:get).with("A2").and_return(OpenStruct.new(:name => "Failure"))
19
+ line = WMATA::Line.new("InternalDestination1" => "A1", "InternalDestination2" => "A2")
20
+
21
+ assert_equal ["Failure", "Winner"], line.internal_destinations.map {|d| d.name}.sort
22
+ end
23
+
24
+ def test_internal_destinations_with_partial_set
25
+ flexmock(WMATA::Station).should_receive(:get).with("A1").and_return(OpenStruct.new(:name => "Winner"))
26
+ line = WMATA::Line.new("InternalDestination1" => "A1")
27
+
28
+ assert_equal ["Winner"], line.internal_destinations.map {|d| d.name}.sort
29
+ end
30
+
31
+ def test_rail_incidents
32
+ flexmock(WMATA::RailIncident).should_receive(:get_by_line).and_return([OpenStruct.new(:name => "Winner")])
33
+ line = WMATA::Line.new("LineCode" => "RD")
34
+
35
+ assert_equal ["Winner"], line.rail_incidents.map(&:name)
36
+ end
37
+
38
+ def test_route
39
+ flexmock(WMATA::Station).should_receive(:get_on_line).and_return([OpenStruct.new(:name => "Winner")])
40
+ line = WMATA::Line.new("LineCode" => "RD")
41
+
42
+ assert_equal ["Winner"], line.route.map(&:name)
43
+ end
44
+
45
+ def test_get
46
+ flexmock(WMATA::Station).should_receive(:get_on_line).and_return([OpenStruct.new(:name => "Winner")])
47
+ line = WMATA::Line.new("LineCode" => "RD")
48
+
49
+ assert_equal ["Winner"], line.route.map(&:name)
50
+ end
51
+
52
+ def test_code
53
+ line = WMATA::Line.new("LineCode" => "RD")
54
+ assert_equal "RD", line.code
55
+ end
56
+
57
+ def test_to_s
58
+ line = WMATA::Line.new("LineCode" => "RD")
59
+ assert_equal "RD", line.to_s
60
+ end
61
+ end
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ class TestPathSegment < Test::Unit::TestCase
4
+ def test_station
5
+ flexmock(WMATA::Station).should_receive(:get).and_return(OpenStruct.new(:name => "Winner"))
6
+ segment = WMATA::PathSegment.new("StationCode" => "A4")
7
+
8
+ assert_equal "Winner", segment.station.name
9
+ end
10
+
11
+ def test_line
12
+ flexmock(WMATA::Line).should_receive(:get).and_return(OpenStruct.new(:name => "Winner"))
13
+ segment = WMATA::PathSegment.new("LineCode" => "A4")
14
+
15
+ assert_equal "Winner", segment.line.name
16
+ end
17
+
18
+ def test_index
19
+ segment = WMATA::PathSegment.new("SeqNum" => "1")
20
+ assert_equal "1", segment.index
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ require 'helper'
2
+
3
+ class TestPrediction< Test::Unit::TestCase
4
+ def setup
5
+ WMATA.api_key = "1234"
6
+ @fake = OpenStruct.new(:name => "Winner")
7
+ @prediction = WMATA::Prediction.new("LocationCode" => "A4", "DestinationCode" => "A3", "Line" => "RD")
8
+ end
9
+
10
+ def test_predict_for
11
+ flexmock(HTTParty).should_receive(:get).with("http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A4?api_key=1234").and_return([[{"Things" => 1234}]])
12
+ prediction = WMATA::Prediction.predict_for("A4")
13
+ end
14
+
15
+ def test_location
16
+ flexmock(WMATA::Station).should_receive(:get).and_return(@fake)
17
+
18
+ assert_equal @fake, @prediction.location
19
+ end
20
+
21
+ def test_destination
22
+ flexmock(WMATA::Station).should_receive(:get).and_return(@fake)
23
+
24
+ assert_equal @fake, @prediction.destination
25
+ end
26
+
27
+ def test_line_code
28
+ assert_equal "RD", @prediction.line_code
29
+ end
30
+
31
+ def test_line
32
+ flexmock(WMATA::Line).should_receive(:get).and_return(@fake)
33
+
34
+ assert_equal @fake, @prediction.line
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ class TestRailIncident < Test::Unit::TestCase
4
+ def test_get_by_station
5
+ fake_incidents = []
6
+ 5.times {|i| fake_incidents << OpenStruct.new(:line_codes_affected => ["RD", "BL#{i}"])}
7
+ flexmock(WMATA::RailIncident).should_receive(:get_all).and_return(fake_incidents)
8
+
9
+ assert_equal ["BL2", "RD"], WMATA::RailIncident.get_by_line("RD")[2].line_codes_affected.sort
10
+ end
11
+
12
+ def test_line_codes_affected
13
+ incident = WMATA::RailIncident.new("LinesAffected" => "RD;BL;OR")
14
+ assert_equal ["BL", "OR", "RD"], incident.line_codes_affected.sort
15
+ end
16
+
17
+ def test_lines_affected
18
+ flexmock(WMATA::Line).should_receive(:get).and_return(WMATA::Line.new("Code" => "RD"))
19
+ incident = WMATA::RailIncident.new("LinesAffected" => "RD;BL;OR")
20
+
21
+ assert_equal 3, incident.lines_affected.length
22
+ end
23
+
24
+ def test_date_updated
25
+ incident = WMATA::RailIncident.new("DateUpdated" => "2010-07-27T00:00:00")
26
+ assert_equal Time.parse("07/27/2010"), incident.date_updated
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ require 'helper'
2
+
3
+ class TestResource < Test::Unit::TestCase
4
+ class Faker < WMATA::Resource
5
+ end
6
+
7
+ def setup
8
+ @fake = Faker.new("method1" => "win", "MethodTwo" => "epic")
9
+ end
10
+
11
+ def test_method_mapping
12
+ assert_nothing_raised do
13
+ assert_equal "win", @fake.method1
14
+ end
15
+ end
16
+
17
+ def test_method_mapping_with_camel_case
18
+ assert_nothing_raised do
19
+ assert_equal "epic", @fake.method_two
20
+ end
21
+ end
22
+
23
+ def test_method_missing_fails_if_missing
24
+ assert_raises(NoMethodError) do
25
+ @fake.whateva
26
+ end
27
+ end
28
+
29
+ def test_service_set
30
+ Faker.service "Rail"
31
+ assert_equal "Rail", Faker.service
32
+ end
33
+
34
+ def test_endpoint_set
35
+ Faker.endpoint "Rail"
36
+ assert_equal "Rail", Faker.endpoint
37
+ end
38
+
39
+ def test_to_query_string
40
+ assert_equal "&things=yes&whatever=no", Faker.to_query_string(:things => "yes", :whatever => "no")
41
+ end
42
+ end
@@ -0,0 +1,66 @@
1
+ require 'helper'
2
+
3
+ class TestStation < Test::Unit::TestCase
4
+ def test_get_on_line
5
+ flexmock(WMATA::Station).should_receive(:get_all).with("LineCode" => "RD")
6
+ WMATA::Station.get_on_line("RD")
7
+ end
8
+
9
+ def test_get_on_line_with_symbol
10
+ flexmock(WMATA::Station).should_receive(:get_all).with("LineCode" => "RD")
11
+ WMATA::Station.get_on_line(:red)
12
+ end
13
+
14
+ def test_get
15
+ flexmock(HTTParty).should_receive(:get).with("http://api.wmata.com/Rail.svc/json/JStationInfo?api_key=1234&StationCode=A4").and_return({"Things" => "1234"})
16
+ station = WMATA::Station.get("A4")
17
+
18
+ assert_equal "1234", station.things
19
+ end
20
+
21
+ def test_codes
22
+ station = WMATA::Station.new("Code" => "A1", "StationTogether1" => "C2", "StationTogether2" => "D1")
23
+ assert_equal ["A1", "C2", "D1"], station.codes.sort
24
+ end
25
+
26
+ def test_codes_with_partial_set
27
+ station = WMATA::Station.new("Code" => "A1", "StationTogether2" => "D1")
28
+ assert_equal ["A1", "D1"], station.codes.sort
29
+ end
30
+
31
+ def test_line_codes
32
+ station = WMATA::Station.new("LineCode1" => "RD", "LineCode2" => "BL", "LineCode3" => "GR", "LineCode4" => "OR")
33
+ assert_equal ["BL", "GR", "OR", "RD"], station.line_codes.sort
34
+ end
35
+
36
+ def test_line_codes_with_partial_set
37
+ station = WMATA::Station.new("LineCode1" => "RD", "LineCode3" => "GR", "LineCode4" => "OR")
38
+ assert_equal ["GR", "OR", "RD"], station.line_codes.sort
39
+ end
40
+
41
+ def test_lines
42
+ flexmock(WMATA::Line).should_receive(:get).with("RD").and_return(OpenStruct.new("code" => "RD"))
43
+ station = WMATA::Station.new("LineCode1" => "RD")
44
+
45
+ assert_equal "RD", station.lines.first.code
46
+ end
47
+
48
+ def test_predictions
49
+ flexmock(WMATA::Prediction).should_receive(:predict_for).and_return([])
50
+ station = WMATA::Station.new("Code" => "A1")
51
+
52
+ assert_equal [], station.predictions
53
+ end
54
+
55
+ def test_elevator_incidents
56
+ flexmock(WMATA::ElevatorIncident).should_receive(:get_by_station).and_return([])
57
+ station = WMATA::Station.new("Code" => "A1")
58
+
59
+ assert_equal [], station.elevator_incidents
60
+ end
61
+
62
+ def test_to_s
63
+ station = WMATA::Station.new("Code" => "A1")
64
+ assert_equal "A1", station.to_s
65
+ end
66
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class TestStationEntrance < Test::Unit::TestCase
4
+ def test_station_codes
5
+ station = WMATA::StationEntrance.new("StationCode1" => "A1", "StationCode2" => "A2")
6
+ assert_equal ["A1","A2"], station.station_codes.sort
7
+ end
8
+
9
+ def test_station_codes_with_partial_set
10
+ station = WMATA::StationEntrance.new("StationCode2" => "A2")
11
+ assert_equal ["A2"], station.station_codes.sort
12
+ end
13
+ end
@@ -0,0 +1,66 @@
1
+ require 'helper'
2
+
3
+ class TestWmata < Test::Unit::TestCase
4
+ def setup
5
+ flexmock(WMATA).should_receive(:api_key).and_return("1234")
6
+ end
7
+
8
+ def test_lines
9
+ mock_resource(WMATA::Line)
10
+ WMATA.lines
11
+ end
12
+
13
+ def test_stations
14
+ mock_resource(WMATA::Station)
15
+ WMATA.stations
16
+ end
17
+
18
+ def test_station
19
+ flexmock(WMATA::Station).should_receive(:get).with("A2")
20
+ WMATA.station("A2")
21
+ end
22
+
23
+ def test_stations_on_line
24
+ flexmock(WMATA::Station).should_receive(:get_on_line).with(:red)
25
+ WMATA.stations_on_line(:red)
26
+ end
27
+
28
+ def test_predict_for
29
+ flexmock(WMATA::Prediction).should_receive(:predict_for).with("A7")
30
+ WMATA.predict_for("A7")
31
+ end
32
+
33
+ def test_predict_for_with_default
34
+ flexmock(WMATA::Prediction).should_receive(:predict_for).with("All")
35
+ WMATA.predict_for
36
+ end
37
+
38
+ def test_rail_incidents
39
+ mock_resource(WMATA::RailIncident)
40
+ WMATA.rail_incidents
41
+ end
42
+
43
+ def test_elevator_incidents
44
+ mock_resource(WMATA::ElevatorIncident)
45
+ WMATA.elevator_incidents
46
+ end
47
+
48
+ def test_build_path
49
+ flexmock(WMATA::PathSegment).should_receive(:get_all).with("FromStationCode" => "37.80", "ToStationCode" => "88.7")
50
+ WMATA.build_path("37.80", "88.7")
51
+ end
52
+
53
+ def test_entrances
54
+ flexmock(WMATA::StationEntrance).should_receive(:get_all).with(:lat => 0, :lon => 0, :radius => 500)
55
+ WMATA.entrances
56
+ end
57
+
58
+ def test_entrances_with_specifics
59
+ flexmock(WMATA::StationEntrance).should_receive(:get_all).with(:lat => 39.0, :lon => 0, :radius => 2000)
60
+ WMATA.entrances(:lat => 39.0, :radius => 2000)
61
+ end
62
+
63
+ def mock_resource(resource_class)
64
+ flexmock(resource_class).should_receive(:get_all)
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wmata
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jeremy McAnally
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-16 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A gem for accessing the WMATA API
22
+ email: jeremymcanally@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/resource.rb
38
+ - lib/resources/elevator_incident.rb
39
+ - lib/resources/line.rb
40
+ - lib/resources/path_segment.rb
41
+ - lib/resources/prediction.rb
42
+ - lib/resources/rail_incident.rb
43
+ - lib/resources/station.rb
44
+ - lib/resources/station_entrance.rb
45
+ - lib/wmata.rb
46
+ - test/helper.rb
47
+ - test/test_elevator_incident.rb
48
+ - test/test_line.rb
49
+ - test/test_path_segment.rb
50
+ - test/test_prediction.rb
51
+ - test/test_rail_incident.rb
52
+ - test/test_resource.rb
53
+ - test/test_station.rb
54
+ - test/test_station_entrance.rb
55
+ - test/test_wmata.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/jm/wmata
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.6
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A gem for the WMATA API
86
+ test_files:
87
+ - test/helper.rb
88
+ - test/test_elevator_incident.rb
89
+ - test/test_line.rb
90
+ - test/test_path_segment.rb
91
+ - test/test_prediction.rb
92
+ - test/test_rail_incident.rb
93
+ - test/test_resource.rb
94
+ - test/test_station.rb
95
+ - test/test_station_entrance.rb
96
+ - test/test_wmata.rb