mbta 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .DS_Store
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
@@ -1,191 +1,13 @@
1
- require 'httparty'
2
- require 'json'
3
1
  require 'csv'
4
- require 'pp'
5
2
 
6
- require "mbta/version"
3
+ require './mbta/version'
7
4
 
8
- module MBTA
9
- module HeavyRail
10
- module Announcements
11
- class Base
12
- attr_reader :trip, :platform, :time
13
-
14
- def initialize(trip, platform, time)
15
- @trip = trip
16
- @platform = platform
17
- @time = time
18
- end
19
-
20
- def type
21
- self.class.name.split('::').last.downcase.to_sym
22
- end
23
- end
24
-
25
- class Prediction < Base
26
- end
27
-
28
- class Arrival < Base
29
- end
30
- end
31
-
32
- class Line
33
- attr_reader :name, :color
34
- attr_writer :stations
35
- attr_accessor :trips
36
-
37
- def initialize(name, stations = {})
38
- @name = name
39
- @color = name.downcase.to_sym
40
- @stations = stations
41
- @trips = {}
42
- end
43
-
44
- def stations(name_or_direction = nil)
45
- case name_or_direction
46
- when String then self[name_or_direction]
47
- when Symbol then nil
48
- else @stations
49
- end
50
- end
51
-
52
- def [](name_or_key)
53
- case name_or_key
54
- when String then @stations.find { |station| station.name == name_or_key }
55
- when Symbol then platforms.find { |platform| platform.key == name_or_key }
56
- end
57
- end
58
-
59
- def platforms
60
- stations.inject(Array.new) { |memo, station| memo << station.platforms }.flatten
61
- end
62
-
63
- def parse_announcements(json)
64
- data = JSON.parse(json)
65
- _trips = data.inject(Array.new) { |memo, announcement| memo << announcement['Trip'].to_i }.uniq
66
- _trips.each do |trip|
67
- self.trips[trip] = MBTA::HeavyRail::Trip.new(self, trip)
68
- end
69
-
70
- data.each do |a|
71
- trip = self.trips[a['Trip'].to_i]
72
- time = Time.strptime(a['Time'], '%m/%d/%Y %H:%M:%S %p')
73
- platform = self[a['PlatformKey'].downcase.to_sym]
74
-
75
- announcement_type = case a['InformationType']
76
- when 'Arrived' then MBTA::HeavyRail::Announcements::Arrival
77
- when 'Predicted' then MBTA::HeavyRail::Announcements::Prediction
78
- end
79
-
80
- trip.announcements << announcement_type.new(trip, platform, time) if announcement_type
81
- end
82
- end
83
- end
84
-
85
- class Trip
86
- attr_reader :line, :id
87
- attr_accessor :announcements
88
-
89
- def initialize(line, id)
90
- @line = line
91
- @id = id
92
- @announcements = []
93
- end
94
- end
95
-
96
- class Station
97
- attr_reader :line, :name
98
- attr_writer :platforms
5
+ require './mbta/commuter_rail'
6
+ require './mbta/heavy_rail'
7
+ require './mbta/bus'
99
8
 
100
- def initialize(line, name, platforms = [])
101
- @line = line
102
- @name = name
103
- @platforms = platforms
104
- end
105
-
106
- def platforms(direction = nil)
107
- if (direction)
108
- self[direction]
109
- else
110
- @platforms
111
- end
112
- end
113
-
114
- def [](direction)
115
- @platforms.find { |platform| platform.direction == direction }
116
- end
117
- end
118
-
119
- class Platform
120
- attr_reader :station, :direction, :key
121
-
122
- def initialize(station, direction, order, key)
123
- @station = station
124
- @direction = direction
125
- @order = order
126
- @key = key
127
- end
128
-
129
- def predictions
130
- station.line.trips.inject(Array.new) do |memo, trip|
131
- memo << trip[1].announcements.inject(Array.new) do |m, a|
132
- m << a if (self.key == a.platform.key && a.type == :prediction)
133
- m
134
- end
135
- end.flatten.compact.sort {|x, y| x.time <=> y.time }
136
- end
137
- end
138
-
139
- module System
140
- class Map
141
- attr_reader :lines
142
-
143
- def initialize(lines = {})
144
- @lines = lines
145
- end
146
- end
147
-
148
- class Builder
149
- def initialize
150
- @data = CSV.read("../data/RealTimeHeavyRailKeys.csv", { headers: true })
151
- end
152
-
153
- def map
154
- data = @data
155
- line_names = data.inject(Array.new) {|memo, platform| memo.push(platform['Line']) }.uniq
156
- lines = line_names.map{|name| MBTA::HeavyRail::Line.new(name) }
157
-
158
- lines.each do |line|
159
- platforms = data.select {|row| row['Line'] == line.name }
160
- station_names = platforms.inject(Array.new) {|memo, platform| memo.push(platform['StationName']) }.uniq
161
- line.stations = station_names.map {|name| MBTA::HeavyRail::Station.new(line, name.capitalize) }
162
-
163
- line.stations.each do |station|
164
- data.select{|row| row['StationName'].capitalize == station.name }.each do |row|
165
- direction = case row['Direction']
166
- when 'NB' then :northbound
167
- when 'SB' then :southbound
168
- when 'EB' then :eastbound
169
- when 'WB' then :westbound
170
- end
171
- key = row['PlatformKey'].downcase.to_sym
172
- station.platforms.push(MBTA::HeavyRail::Platform.new(station, direction, row['PlatformOrder'], key))
173
- end
174
- end
175
- end
176
-
177
- _lines = lines.inject(Hash.new){|lines, line| lines[line.name] = line; lines }
178
-
179
- map = MBTA::HeavyRail::System::Map.new(_lines)
180
- return map
181
- end
182
- end
183
-
184
- MAP = Builder.new.map
185
- end
186
- end
187
-
188
- BlueLine = MBTA::HeavyRail::System::MAP.lines['Blue']
189
- GreenLine = MBTA::HeavyRail::System::MAP.lines['Green']
190
- RedLine = MBTA::HeavyRail::System::MAP.lines['Red']
9
+ module MBTA
10
+ include MBTA::CommuterRail
11
+ include MBTA::HeavyRail
12
+ include MBTA::Bus
191
13
  end
@@ -0,0 +1,4 @@
1
+ module MBTA
2
+ module Bus
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MBTA
2
+ module CommuterRail
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ require './mbta/heavy_rail/system'
2
+
3
+ module MBTA
4
+ module HeavyRail
5
+ include MBTA::HeavyRail::System
6
+
7
+ BlueLine = MBTA::HeavyRail::System::MAP.lines['Blue']
8
+ OrangeLine = MBTA::HeavyRail::System::MAP.lines['Orange']
9
+ RedLine = MBTA::HeavyRail::System::MAP.lines['Red']
10
+ end
11
+ end
File without changes
@@ -0,0 +1,8 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ module Announcements
4
+ class Arrival < Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ module Announcements
4
+ class Base
5
+ attr_reader :trip, :platform, :time
6
+
7
+ def initialize(trip, platform, time)
8
+ @trip = trip
9
+ @platform = platform
10
+ @time = time
11
+ end
12
+
13
+ def type
14
+ self.class.name.split('::').last.downcase.to_sym
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ module Announcements
4
+ class Prediction < Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,56 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ class Line
4
+ attr_reader :name, :color
5
+ attr_writer :stations
6
+ attr_accessor :trips
7
+
8
+ def initialize(name, stations = {})
9
+ @name = name
10
+ @color = name.downcase.to_sym
11
+ @stations = stations
12
+ @trips = {}
13
+ end
14
+
15
+ def stations(name_or_direction = nil)
16
+ case name_or_direction
17
+ when String then self[name_or_direction]
18
+ when Symbol then nil
19
+ else @stations
20
+ end
21
+ end
22
+
23
+ def [](name_or_key)
24
+ case name_or_key
25
+ when String then @stations.find { |station| station.name == name_or_key }
26
+ when Symbol then platforms.find { |platform| platform.key == name_or_key }
27
+ end
28
+ end
29
+
30
+ def platforms
31
+ stations.inject(Array.new) { |memo, station| memo << station.platforms }.flatten
32
+ end
33
+
34
+ def parse_announcements(json)
35
+ data = JSON.parse(json)
36
+ _trips = data.inject(Array.new) { |memo, announcement| memo << announcement['Trip'].to_i }.uniq
37
+ _trips.each do |trip|
38
+ self.trips[trip] = MBTA::HeavyRail::Trip.new(self, trip)
39
+ end
40
+
41
+ data.each do |a|
42
+ trip = self.trips[a['Trip'].to_i]
43
+ time = Time.strptime(a['Time'], '%m/%d/%Y %H:%M:%S %p')
44
+ platform = self[a['PlatformKey'].downcase.to_sym]
45
+
46
+ announcement_type = case a['InformationType']
47
+ when 'Arrived' then MBTA::HeavyRail::Announcements::Arrival
48
+ when 'Predicted' then MBTA::HeavyRail::Announcements::Prediction
49
+ end
50
+
51
+ trip.announcements << announcement_type.new(trip, platform, time) if announcement_type
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,23 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ class Platform
4
+ attr_reader :station, :direction, :key
5
+
6
+ def initialize(station, direction, order, key)
7
+ @station = station
8
+ @direction = direction
9
+ @order = order
10
+ @key = key
11
+ end
12
+
13
+ def predictions
14
+ station.line.trips.inject(Array.new) do |memo, trip|
15
+ memo << trip[1].announcements.inject(Array.new) do |m, a|
16
+ m << a if (self.key == a.platform.key && a.type == :prediction)
17
+ m
18
+ end
19
+ end.flatten.compact.sort {|x, y| x.time <=> y.time }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ class Station
4
+ attr_reader :line, :name
5
+ attr_writer :platforms
6
+
7
+ def initialize(line, name, platforms = [])
8
+ @line = line
9
+ @name = name
10
+ @platforms = platforms
11
+ end
12
+
13
+ def inspect
14
+ %("#{@name}")
15
+ end
16
+
17
+ def platforms(direction = nil)
18
+ if (direction)
19
+ self[direction]
20
+ else
21
+ @platforms
22
+ end
23
+ end
24
+
25
+ def [](direction)
26
+ @platforms.find { |platform| platform.direction == direction }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ require './mbta/heavy_rail/line'
2
+ require './mbta/heavy_rail/station'
3
+ require './mbta/heavy_rail/platform'
4
+
5
+ module MBTA
6
+ module HeavyRail
7
+ module System
8
+ class Map
9
+ attr_reader :lines
10
+
11
+ def initialize(lines = {})
12
+ @lines = lines
13
+ end
14
+ end
15
+
16
+ class Builder
17
+ def initialize
18
+ @data = ::CSV.read("../data/RealTimeHeavyRailKeys.csv", { headers: true })
19
+ end
20
+
21
+ def map
22
+ data = @data
23
+ line_names = data.inject(Array.new) {|memo, platform| memo.push(platform['Line']) }.uniq
24
+ lines = line_names.map{|name| MBTA::HeavyRail::Line.new(name) }
25
+
26
+ lines.each do |line|
27
+ platforms = data.select {|row| row['Line'] == line.name }
28
+ station_names = platforms.inject(Array.new) {|memo, platform| memo.push(platform['StationName']) }.uniq
29
+ line.stations = station_names.map {|name| MBTA::HeavyRail::Station.new(line, name.capitalize) }
30
+
31
+ line.stations.each do |station|
32
+ data.select{|row| row['StationName'].capitalize == station.name }.each do |row|
33
+ direction = case row['Direction']
34
+ when 'NB' then :northbound
35
+ when 'SB' then :southbound
36
+ when 'EB' then :eastbound
37
+ when 'WB' then :westbound
38
+ end
39
+ key = row['PlatformKey'].downcase.to_sym
40
+ station.platforms.push(MBTA::HeavyRail::Platform.new(station, direction, row['PlatformOrder'], key))
41
+ end
42
+ end
43
+ end
44
+
45
+ _lines = lines.inject(Hash.new){|lines, line| lines[line.name] = line; lines }
46
+
47
+ map = MBTA::HeavyRail::System::Map.new(_lines)
48
+ return map
49
+ end
50
+ end
51
+
52
+ MAP = Builder.new.map
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ module MBTA
2
+ module HeavyRail
3
+ class Trip
4
+ attr_reader :line, :id
5
+ attr_accessor :announcements
6
+
7
+ def initialize(line, id)
8
+ @line = line
9
+ @id = id
10
+ @announcements = []
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module MBTA
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -2,16 +2,18 @@
2
2
  require File.expand_path('../lib/mbta/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Chris Kalafarski"]
6
- gem.email = ["chris@farski.com"]
5
+ gem.authors = ['Chris Kalafarski']
6
+ gem.email = ['chris@farski.com']
7
7
  gem.description = %q{Library for accessing real-time MBTA data}
8
8
  gem.summary = %q{Not a product of the MBTA. See mbta.com/developers for more info about their API}
9
- gem.homepage = "https://github.com/farski/mbta"
9
+ gem.homepage = 'https://github.com/farski/mbta'
10
10
 
11
11
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
12
  gem.files = `git ls-files`.split("\n")
13
13
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
- gem.name = "mbta"
15
- gem.require_paths = ["lib"]
14
+ gem.name = 'mbta'
15
+ gem.require_paths = ['lib']
16
16
  gem.version = MBTA::VERSION
17
+
18
+ gem.add_dependency('httparty', '>= 0.8.0')
17
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-24 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70218718288260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70218718288260
14
25
  description: Library for accessing real-time MBTA data
15
26
  email:
16
27
  - chris@farski.com
@@ -18,15 +29,25 @@ executables: []
18
29
  extensions: []
19
30
  extra_rdoc_files: []
20
31
  files:
21
- - .DS_Store
22
32
  - .gitignore
23
33
  - Gemfile
24
34
  - LICENSE
25
35
  - README.md
26
36
  - Rakefile
27
- - data/.DS_Store
28
37
  - data/RealTimeHeavyRailKeys.csv
29
38
  - lib/mbta.rb
39
+ - lib/mbta/bus.rb
40
+ - lib/mbta/commuter_rail.rb
41
+ - lib/mbta/heavy_rail.rb
42
+ - lib/mbta/heavy_rail/announcements.rb
43
+ - lib/mbta/heavy_rail/announcements/arrival.rb
44
+ - lib/mbta/heavy_rail/announcements/base.rb
45
+ - lib/mbta/heavy_rail/announcements/prediction.rb
46
+ - lib/mbta/heavy_rail/line.rb
47
+ - lib/mbta/heavy_rail/platform.rb
48
+ - lib/mbta/heavy_rail/station.rb
49
+ - lib/mbta/heavy_rail/system.rb
50
+ - lib/mbta/heavy_rail/trip.rb
30
51
  - lib/mbta/version.rb
31
52
  - mbta.gemspec
32
53
  homepage: https://github.com/farski/mbta
data/.DS_Store DELETED
Binary file
Binary file