mbta_wrapper 0.0.1 → 0.0.2
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/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +1 -0
- data/README.md +16 -2
- data/lib/mbta_wrapper/bus_line.rb +5 -0
- data/lib/mbta_wrapper/commuterrail_line.rb +5 -0
- data/lib/mbta_wrapper/subway.rb +19 -30
- data/lib/mbta_wrapper/subway_line.rb +69 -0
- data/lib/mbta_wrapper/version.rb +1 -1
- data/lib/mbta_wrapper.rb +5 -2
- data/spec/bus_spec.rb +1 -0
- data/spec/mbta_wrapper_spec.rb +1 -0
- data/spec/sample_data/red.json +60 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/subway_line_spec.rb +19 -0
- data/spec/subway_spec.rb +26 -0
- metadata +19 -2
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# MbtaWrapper
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://travis-ci.org/sicentendu/mbta_wrapper)
|
|
4
|
+
|
|
5
|
+
Ruby MBTA API wrapper, nothing is really built yet but maybe it will be soon
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -18,7 +20,19 @@ Or install it yourself as:
|
|
|
18
20
|
|
|
19
21
|
## Usage
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
This gem provides super easy access to the MBTA's API.
|
|
24
|
+
|
|
25
|
+
MbtaWrapper::Subway
|
|
26
|
+
MbtaWrapper::CommuterRail
|
|
27
|
+
MbtaWrapper::Bus
|
|
28
|
+
|
|
29
|
+
To view the current number of red line trains running:
|
|
30
|
+
|
|
31
|
+
MbtaWrapper::SubwayLine.new('red').active_trains
|
|
32
|
+
|
|
33
|
+
To create an array of current trains on the red line:
|
|
34
|
+
|
|
35
|
+
MbtaWrapper::SubwayLine.new('red').current_trains
|
|
22
36
|
|
|
23
37
|
## Contributing
|
|
24
38
|
|
data/lib/mbta_wrapper/subway.rb
CHANGED
|
@@ -1,45 +1,34 @@
|
|
|
1
1
|
module MbtaWrapper
|
|
2
2
|
class Subway
|
|
3
|
-
def initialize(
|
|
4
|
-
@
|
|
3
|
+
def initialize(train_number, destination, stops, position)
|
|
4
|
+
@train_number = train_number
|
|
5
|
+
@destination = destination
|
|
6
|
+
@stops = stops
|
|
7
|
+
@position = position
|
|
5
8
|
end
|
|
6
9
|
|
|
7
|
-
def
|
|
8
|
-
|
|
9
|
-
data = JSON.parse(resp.body)
|
|
10
|
-
ready = data['TripList']
|
|
10
|
+
def train_number
|
|
11
|
+
@train_number
|
|
11
12
|
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
data = get_and_parse_json
|
|
16
|
-
data['Trips'].length
|
|
14
|
+
def destination
|
|
15
|
+
@destination
|
|
17
16
|
end
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
data = get_and_parse_json
|
|
22
|
-
time = 0
|
|
23
|
-
data['Trips'].each do |trip|
|
|
24
|
-
end
|
|
18
|
+
def stops
|
|
19
|
+
@stops
|
|
25
20
|
end
|
|
26
21
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
data = get_and_parse_json
|
|
30
|
-
data['Trips'].each do |trip|
|
|
31
|
-
first_prediction = trip['Predictions'].first
|
|
32
|
-
stop = first_prediction['Stop']
|
|
33
|
-
time = (sec = first_prediction['Seconds'].to_i) < 60 ? sec : (sec / 60).round
|
|
34
|
-
if sec < 60
|
|
35
|
-
puts "A #{@line} line train will arrive at #{stop} in #{time} seconds"
|
|
36
|
-
else
|
|
37
|
-
puts "A #{@line} line train will arrive at #{stop} in #{time} minutes"
|
|
38
|
-
end
|
|
39
|
-
end
|
|
22
|
+
def position
|
|
23
|
+
@position
|
|
40
24
|
end
|
|
41
25
|
|
|
42
|
-
def
|
|
26
|
+
def next_stop
|
|
27
|
+
stops.first
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def time_to_stop_at(station)
|
|
31
|
+
stops.select {|stop| stop['Stop'] == station}.first['Seconds']
|
|
43
32
|
end
|
|
44
33
|
end
|
|
45
34
|
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module MbtaWrapper
|
|
2
|
+
class SubwayLine
|
|
3
|
+
def initialize(line)
|
|
4
|
+
@line = line
|
|
5
|
+
@default_json_url = "http://developer.mbta.com/lib/rthr/" + @line.to_s.downcase + ".json"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def current_trains
|
|
9
|
+
data = parse_json
|
|
10
|
+
current_trains = []
|
|
11
|
+
data['Trips'].each do |train|
|
|
12
|
+
if !train['Position'].nil?
|
|
13
|
+
train_number = train['Position']['Train'] ||= nil
|
|
14
|
+
position = {'Latitude' => train['Position']['Lat'], 'Longitude' => train['Position']['Long']}
|
|
15
|
+
end
|
|
16
|
+
destination = train['Destination']
|
|
17
|
+
stops = []
|
|
18
|
+
train['Predictions'].each do |stop|
|
|
19
|
+
stops << {'Stop' => stop['Stop'], 'Seconds' => stop['Seconds']}
|
|
20
|
+
end
|
|
21
|
+
current_trains << MbtaWrapper::Subway.new(train_number, destination, stops, position)
|
|
22
|
+
end
|
|
23
|
+
current_trains
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_json(url = @default_json_url)
|
|
28
|
+
Net::HTTP.get_response(URI.parse(url)).body
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def parse_json(url = @default_json_url)
|
|
32
|
+
resp = get_json(url)
|
|
33
|
+
data = JSON.parse(resp)
|
|
34
|
+
ready = data['TripList']
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Number of current active trains
|
|
38
|
+
def active_trains
|
|
39
|
+
data = parse_json
|
|
40
|
+
data['Trips'].length
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Time until next train at station
|
|
44
|
+
def time_until(station)
|
|
45
|
+
data = parse_json
|
|
46
|
+
time = 0
|
|
47
|
+
data['Trips'].each do |trip|
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
#
|
|
52
|
+
def upcoming(number = 10)
|
|
53
|
+
data = parse_json
|
|
54
|
+
data['Trips'].each do |trip|
|
|
55
|
+
first_prediction = trip['Predictions'].first
|
|
56
|
+
stop = first_prediction['Stop']
|
|
57
|
+
time = (sec = first_prediction['Seconds'].to_i) < 60 ? sec : (sec / 60).round
|
|
58
|
+
if sec < 60
|
|
59
|
+
puts "A #{@line} line train will arrive at #{stop} in #{time} seconds"
|
|
60
|
+
else
|
|
61
|
+
puts "A #{@line} line train will arrive at #{stop} in #{time} minutes"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def next_stops
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
data/lib/mbta_wrapper/version.rb
CHANGED
data/lib/mbta_wrapper.rb
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
require "mbta_wrapper/version"
|
|
2
|
+
require "mbta_wrapper/subway_line"
|
|
2
3
|
require "mbta_wrapper/bus"
|
|
3
4
|
require "mbta_wrapper/commuterrail"
|
|
4
5
|
require "mbta_wrapper/subway"
|
|
6
|
+
require "mbta_wrapper/bus_line"
|
|
7
|
+
require "mbta_wrapper/commuterrail_line"
|
|
5
8
|
require 'json'
|
|
6
9
|
require 'net/http'
|
|
7
10
|
require 'rexml/document'
|
|
8
11
|
|
|
9
|
-
module
|
|
10
|
-
|
|
12
|
+
module MbtaWrapper
|
|
13
|
+
|
|
11
14
|
end
|
data/spec/bus_spec.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{ "TripList": {"CurrentTime": 1360520290,"Line": "Red", "Trips": [{"TripID": "R98308BF6", "Destination": "Ashmont", "Predictions": [
|
|
2
|
+
{"StopID": "70061", "Stop": "Alewife", "Seconds": 985},
|
|
3
|
+
{"StopID": "70063", "Stop": "Davis", "Seconds": 1069},
|
|
4
|
+
{"StopID": "70065", "Stop": "Porter Square", "Seconds": 1192}]},
|
|
5
|
+
{"TripID": "R98308C5D", "Destination": "Alewife", "Position": {"Timestamp": 1360518799, "Train": "1510", "Lat": 42.20749, "Long": -71.00173,"Heading": 190}, "Predictions": [
|
|
6
|
+
{"StopID": "70104", "Stop": "Quincy Adams", "Seconds": 237},
|
|
7
|
+
{"StopID": "70102", "Stop": "Quincy Center", "Seconds": 416},
|
|
8
|
+
{"StopID": "70100", "Stop": "Wollaston", "Seconds": 689},
|
|
9
|
+
{"StopID": "70098", "Stop": "North Quincy", "Seconds": 1195}]},
|
|
10
|
+
{"TripID": "R98308C74", "Destination": "Alewife", "Position": {"Timestamp": 1360519343, "Train": "1639", "Lat": 42.20749, "Long": -71.00156,"Heading": 190}, "Predictions": [
|
|
11
|
+
{"StopID": "70105", "Stop": "Braintree", "Seconds": 710},
|
|
12
|
+
{"StopID": "70104", "Stop": "Quincy Adams", "Seconds": 985},
|
|
13
|
+
{"StopID": "70102", "Stop": "Quincy Center", "Seconds": 1159}]},
|
|
14
|
+
{"TripID": "R98308C2A", "Destination": "Alewife", "Position": {"Timestamp": 1360519774, "Train": "1813", "Lat": 42.28458, "Long": -71.06377,"Heading": 330}, "Predictions": [
|
|
15
|
+
{"StopID": "70094", "Stop": "Ashmont", "Seconds": -10},
|
|
16
|
+
{"StopID": "70092", "Stop": "Shawmut", "Seconds": 64},
|
|
17
|
+
{"StopID": "70090", "Stop": "Fields Corner", "Seconds": 133},
|
|
18
|
+
{"StopID": "70088", "Stop": "Savin Hill", "Seconds": 295},
|
|
19
|
+
{"StopID": "70086", "Stop": "JFK/UMass", "Seconds": 424}]},
|
|
20
|
+
{"TripID": "R98308C9F", "Destination": "Braintree", "Position": {"Timestamp": 1360520257, "Train": "1623", "Lat": 42.38826, "Long": -71.11895,"Heading": 180}, "Predictions": [
|
|
21
|
+
{"StopID": "70067", "Stop": "Harvard Square", "Seconds": 117},
|
|
22
|
+
{"StopID": "70069", "Stop": "Central Square", "Seconds": 310},
|
|
23
|
+
{"StopID": "70071", "Stop": "Kendall/MIT", "Seconds": 432},
|
|
24
|
+
{"StopID": "70073", "Stop": "Charles/MGH", "Seconds": 571},
|
|
25
|
+
{"StopID": "70075", "Stop": "Park Street", "Seconds": 679},
|
|
26
|
+
{"StopID": "70077", "Stop": "Downtown Crossing", "Seconds": 744},
|
|
27
|
+
{"StopID": "70079", "Stop": "South Station", "Seconds": 788},
|
|
28
|
+
{"StopID": "70081", "Stop": "Broadway", "Seconds": 904},
|
|
29
|
+
{"StopID": "70083", "Stop": "Andrew", "Seconds": 1028}]},
|
|
30
|
+
{"TripID": "R98308BF5", "Destination": "Alewife", "Position": {"Timestamp": 1360520075, "Train": "1846", "Lat": 42.36598, "Long": -71.10479,"Heading": 310}, "Predictions": [
|
|
31
|
+
{"StopID": "70066", "Stop": "Porter Square", "Seconds": 370},
|
|
32
|
+
{"StopID": "70064", "Stop": "Davis", "Seconds": 549},
|
|
33
|
+
{"StopID": "70061", "Stop": "Alewife", "Seconds": 746}]},
|
|
34
|
+
{"TripID": "R98308BDC", "Destination": "Ashmont", "Note": "Delayed", "Position": {"Timestamp": 1360519520, "Train": "1840", "Lat": 42.39621, "Long": -71.14054,"Heading": 265}, "Predictions": [
|
|
35
|
+
{"StopID": "70063", "Stop": "Davis", "Seconds": 46},
|
|
36
|
+
{"StopID": "70065", "Stop": "Porter Square", "Seconds": 169},
|
|
37
|
+
{"StopID": "70067", "Stop": "Harvard Square", "Seconds": 319},
|
|
38
|
+
{"StopID": "70069", "Stop": "Central Square", "Seconds": 512},
|
|
39
|
+
{"StopID": "70071", "Stop": "Kendall/MIT", "Seconds": 634}]},
|
|
40
|
+
{"TripID": "R98308CE1", "Destination": "Braintree", "Position": {"Timestamp": 1360519936, "Train": "1705", "Lat": 42.3185, "Long": -71.05212,"Heading": 170}, "Predictions": [
|
|
41
|
+
{"StopID": "70097", "Stop": "North Quincy", "Seconds": 168},
|
|
42
|
+
{"StopID": "70099", "Stop": "Wollaston", "Seconds": 277},
|
|
43
|
+
{"StopID": "70101", "Stop": "Quincy Center", "Seconds": 581},
|
|
44
|
+
{"StopID": "70103", "Stop": "Quincy Adams", "Seconds": 770},
|
|
45
|
+
{"StopID": "70105", "Stop": "Braintree", "Seconds": 1052}]},
|
|
46
|
+
{"TripID": "R98308CE2", "Destination": "Alewife", "Position": {"Timestamp": 1360520260, "Train": "1520", "Lat": 42.27582, "Long": -71.03012,"Heading": 315}, "Predictions": [
|
|
47
|
+
{"StopID": "70096", "Stop": "JFK/UMass", "Seconds": 595},
|
|
48
|
+
{"StopID": "70084", "Stop": "Andrew", "Seconds": 809},
|
|
49
|
+
{"StopID": "70082", "Stop": "Broadway", "Seconds": 933},
|
|
50
|
+
{"StopID": "70080", "Stop": "South Station", "Seconds": 1083},
|
|
51
|
+
{"StopID": "70078", "Stop": "Downtown Crossing", "Seconds": 1154}]},
|
|
52
|
+
{"TripID": "R98308C0F", "Destination": "Ashmont", "Position": {"Timestamp": 1360520261, "Train": "1883", "Lat": 42.35524, "Long": -71.06018,"Heading": 135}, "Predictions": [
|
|
53
|
+
{"StopID": "70079", "Stop": "South Station", "Seconds": 15},
|
|
54
|
+
{"StopID": "70081", "Stop": "Broadway", "Seconds": 131},
|
|
55
|
+
{"StopID": "70083", "Stop": "Andrew", "Seconds": 255},
|
|
56
|
+
{"StopID": "70085", "Stop": "JFK/UMass", "Seconds": 408},
|
|
57
|
+
{"StopID": "70087", "Stop": "Savin Hill", "Seconds": 546},
|
|
58
|
+
{"StopID": "70089", "Stop": "Fields Corner", "Seconds": 714},
|
|
59
|
+
{"StopID": "70091", "Stop": "Shawmut", "Seconds": 831},
|
|
60
|
+
{"StopID": "70093", "Stop": "Ashmont", "Seconds": 981}]}]}}
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
|
4
|
+
# loaded once.
|
|
5
|
+
#
|
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
7
|
+
require 'mbta_wrapper'
|
|
8
|
+
require 'rspec'
|
|
9
|
+
|
|
10
|
+
RSpec.configure do |config|
|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
|
12
|
+
config.run_all_when_everything_filtered = true
|
|
13
|
+
config.filter_run :focus
|
|
14
|
+
|
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
|
17
|
+
# the seed, which is printed after each run.
|
|
18
|
+
# --seed 1234
|
|
19
|
+
config.order = 'random'
|
|
20
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe MbtaWrapper::SubwayLine do
|
|
4
|
+
it 'should return correct number of current trains' do
|
|
5
|
+
red_line = create_red_line
|
|
6
|
+
red_line.active_trains.should == 10
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it 'should create an array of Subways' do
|
|
10
|
+
red_line = create_red_line
|
|
11
|
+
red_line.current_trains.first.class.should == MbtaWrapper::Subway
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def create_red_line
|
|
15
|
+
red_line = MbtaWrapper::SubwayLine.new('red')
|
|
16
|
+
red_line.stub!(:get_json).and_return(File.read('spec/sample_data/red.json'))
|
|
17
|
+
red_line
|
|
18
|
+
end
|
|
19
|
+
end
|
data/spec/subway_spec.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe MbtaWrapper::Subway do
|
|
4
|
+
it 'should have working accessors' do
|
|
5
|
+
t = create_red_line_train
|
|
6
|
+
t.destination.should == 'Alewife'
|
|
7
|
+
t.train_number.should == '1510'
|
|
8
|
+
t.stops.class.should == Array
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'should give the correct next stop' do
|
|
12
|
+
t = create_red_line_train
|
|
13
|
+
t.next_stop.should == {'Stop' => 'Quincy Adams', 'Seconds' => 237}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'should give the correct time to specified stop' do
|
|
17
|
+
t = create_red_line_train
|
|
18
|
+
t.time_to_stop_at('Quincy Adams').should == 237
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def create_red_line_train
|
|
22
|
+
red_line = MbtaWrapper::SubwayLine.new('red')
|
|
23
|
+
red_line.stub!(:get_json).and_return(File.read('spec/sample_data/red.json'))
|
|
24
|
+
red_line.current_trains[1]
|
|
25
|
+
end
|
|
26
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mbta_wrapper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -19,16 +19,27 @@ extensions: []
|
|
|
19
19
|
extra_rdoc_files: []
|
|
20
20
|
files:
|
|
21
21
|
- .gitignore
|
|
22
|
+
- .rspec
|
|
23
|
+
- .travis.yml
|
|
22
24
|
- Gemfile
|
|
23
25
|
- LICENSE.txt
|
|
24
26
|
- README.md
|
|
25
27
|
- Rakefile
|
|
26
28
|
- lib/mbta_wrapper.rb
|
|
27
29
|
- lib/mbta_wrapper/bus.rb
|
|
30
|
+
- lib/mbta_wrapper/bus_line.rb
|
|
28
31
|
- lib/mbta_wrapper/commuterrail.rb
|
|
32
|
+
- lib/mbta_wrapper/commuterrail_line.rb
|
|
29
33
|
- lib/mbta_wrapper/subway.rb
|
|
34
|
+
- lib/mbta_wrapper/subway_line.rb
|
|
30
35
|
- lib/mbta_wrapper/version.rb
|
|
31
36
|
- mbta_wrapper.gemspec
|
|
37
|
+
- spec/bus_spec.rb
|
|
38
|
+
- spec/mbta_wrapper_spec.rb
|
|
39
|
+
- spec/sample_data/red.json
|
|
40
|
+
- spec/spec_helper.rb
|
|
41
|
+
- spec/subway_line_spec.rb
|
|
42
|
+
- spec/subway_spec.rb
|
|
32
43
|
homepage: ''
|
|
33
44
|
licenses: []
|
|
34
45
|
post_install_message:
|
|
@@ -53,4 +64,10 @@ rubygems_version: 1.8.25
|
|
|
53
64
|
signing_key:
|
|
54
65
|
specification_version: 3
|
|
55
66
|
summary: Simple MBTA Ruby wrapper
|
|
56
|
-
test_files:
|
|
67
|
+
test_files:
|
|
68
|
+
- spec/bus_spec.rb
|
|
69
|
+
- spec/mbta_wrapper_spec.rb
|
|
70
|
+
- spec/sample_data/red.json
|
|
71
|
+
- spec/spec_helper.rb
|
|
72
|
+
- spec/subway_line_spec.rb
|
|
73
|
+
- spec/subway_spec.rb
|