metrorail 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/metrorail.rb +168 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b74a62f2e2f80a0a0bfa5263631856748912a6e
4
+ data.tar.gz: 8361f31d7fc54754360335af56b048cc8d3c558b
5
+ SHA512:
6
+ metadata.gz: 7a69979ffea8b96fcb921fa8c5d30e78f57e8e41a63ad03a31bc195b55eb0d6e3189d7c649ea119b351e4d1c02d40a6734cfa5d49ba91268047ca0b3249b27e5
7
+ data.tar.gz: b0cc04ec84369471b152cf77bcec00fe4b8bbd707b007bd398d426dbaf9213b640c303b22dc44670b3b51f1563fecc98516ced59bfb651ac629c47c4fa927102
@@ -0,0 +1,168 @@
1
+ require 'httpclient'
2
+ require 'json'
3
+
4
+ module Metrorail
5
+ @@api_key = nil
6
+ def self.set_api_key(key)
7
+ @@api_key = key
8
+ puts "Updating station cache data."
9
+ Metrorail::Station.update_cache
10
+ puts "Updating line cache data."
11
+ Metrorail::Line.update_cache
12
+ puts "Done."
13
+ end
14
+
15
+ # A class representing a location (i.e. latitude/longitude)
16
+ class Location
17
+ attr_reader :lat, :lon
18
+ def initialize(lat, lon)
19
+ @lat = lat
20
+ @lon = lon
21
+ end
22
+
23
+ def to_s
24
+ return "<Location: #{@lat}, #{@lon}>"
25
+ end
26
+ end
27
+
28
+ # A class representing a station in the Metrorail system.
29
+ class Station
30
+ attr_reader :id, :name, :lines_served, :location, :is_transfer
31
+ @@stations = []
32
+
33
+ def initialize(id, name, lines_served, location)
34
+ @id = [id]
35
+ @name = name
36
+ @lines_served = lines_served
37
+ @location = location
38
+ @is_transfer = false
39
+ end
40
+
41
+ def add_id(id)
42
+ @id << id
43
+ end
44
+
45
+ def add_line_served(line)
46
+ if not @lines_served.include? line
47
+ @lines_served << line
48
+ end
49
+ end
50
+
51
+ def to_s
52
+ return "<Station: #{@name} (#{@id.join(",")}) serving #{@lines_served.join(",")} at #{@location.to_s}>"
53
+ end
54
+
55
+ def self.all(force=false)
56
+ if not force and @@stations.size > 0
57
+ return @@stations
58
+ end
59
+
60
+ stations_request = Metrorail::make_request("Rail", "jStations")["Stations"]
61
+ stations = []
62
+
63
+ stations_request.each do |station|
64
+ same_station = stations.select { |s| s.name == station["Name"] }
65
+ if same_station.size > 0
66
+ old_station = same_station[0]
67
+ old_station.add_id station["Code"]
68
+ ["LineCode1", "LineCode2", "LineCode3", "LineCode4"].each do |linecode|
69
+ if station[linecode] != nil
70
+ old_station.add_line_served(station[linecode])
71
+ end
72
+ end
73
+ else
74
+ new_station = Station.new(station["Code"], station["Name"], [], Location.new(station["Lat"], station["Lon"]))
75
+ ["LineCode1", "LineCode2", "LineCode3", "LineCode4"].each do |linecode|
76
+ if station[linecode] != nil
77
+ new_station.add_line_served(station[linecode])
78
+ end
79
+ end
80
+ stations << new_station
81
+ end
82
+ end
83
+
84
+ return stations
85
+ end
86
+
87
+ def self.find_by_id(id)
88
+ self.update_cache_if_needed
89
+ matching = self.all.select { |station| station.id.include? id }
90
+ if matching.size > 0
91
+ matching[0]
92
+ else
93
+ nil
94
+ end
95
+ end
96
+
97
+ def self.update_cache
98
+ @@stations = self.all(true)
99
+ end
100
+
101
+ def self.update_cache_if_needed
102
+ if @@stations.size == 0
103
+ self.update_cache
104
+ end
105
+ end
106
+ end
107
+
108
+ # A class representing a line in the Metrorail system.
109
+ class Line
110
+ attr_reader :id, :name, :stations
111
+ @@lines = []
112
+
113
+ def initialize(id, name, stations=[])
114
+ @id = id
115
+ @name = name
116
+ @stations = []
117
+ end
118
+
119
+ def add_station(station)
120
+ @stations << station
121
+ end
122
+
123
+ def self.all(force=false)
124
+ if @@lines.size > 0
125
+ return @@lines
126
+ else
127
+ lines_request = Metrorail::make_request("Rail", "jLines")["Lines"]
128
+ lines = []
129
+ lines_request.each do |line|
130
+ new_line = Line.new(line["LineCode"], line["DisplayName"])
131
+ stations = Metrorail::make_request("Rail", "jPath", {fromStationCode: line["StartStationCode"], toStationCode: line["EndStationCode"]})["Path"]
132
+ stations.each do |station|
133
+ new_line.add_station(Metrorail::Station.find_by_id(station["StationCode"]))
134
+ end
135
+ lines << new_line
136
+ end
137
+ return lines
138
+ end
139
+ end
140
+
141
+ def self.find_by_id(id)
142
+ self.update_cache_if_needed
143
+ matching = self.all.select { |line| line.id == id }
144
+ if matching.size > 0
145
+ matching[0]
146
+ else
147
+ nil
148
+ end
149
+ end
150
+
151
+ def self.update_cache
152
+ @@lines = self.all(true)
153
+ end
154
+
155
+ def self.update_cache_if_needed
156
+ if @@lines.size == 0
157
+ self.update_cache
158
+ end
159
+ end
160
+ end
161
+
162
+ private
163
+ def self.make_request(mod, submod, args={})
164
+ client = HTTPClient.new
165
+ args["api_key"] = @@api_key
166
+ JSON::parse(client.get("http://api.wmata.com/#{mod}.svc/json/#{submod}", args).body)
167
+ end
168
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metrorail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fox Wilson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a wrapper for the metrorail API
14
+ email: fwilson@fwilson.me
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/metrorail.rb
20
+ homepage: http://rubygems.org/gems/metrorail
21
+ licenses:
22
+ - BSD 3-clause
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: metrorail
44
+ test_files: []