dcmetro 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90891b8942382cbf830daf11826f4d4d019931a9
4
- data.tar.gz: 18fcafc5a952ae46cc8264404e8a459841398307
3
+ metadata.gz: 26b4e560cc10b94ec132c545fdfbaf56ab923126
4
+ data.tar.gz: 10e8f6c5602fe8de59a982a6f31d4880be270623
5
5
  SHA512:
6
- metadata.gz: 9203bd85d858944c7f9729f98b5d4b3d2b85937c60d676701b3f1ff616d3960b960a5d5fe411d7be5db5196efac575b3157dd36eb6d8a01c7b8d0e7c24f52428
7
- data.tar.gz: 82a9046f638381d1e2f808cb819a7d523184d1f2fdf72fae2e3846304f4499928ff00cdde1e3040b3291c73b5245877d588e00079d6cf3f3f114e73734f79f5f
6
+ metadata.gz: 5e321972a4fecd6ba6b16d6f3ff47ed1a199f7aebbf266fbccf8072af52ecfd979b80a2413f6b09ab522017d9c270878a838be281073f881b81a255727de1d85
7
+ data.tar.gz: 345a453a721b5583d19c4e96bdc7ab550d92133ddb62ecba4f00dd97df92b3fac7d6fb9ea5f54e2efd787fbe391d9f7440db4d5e9da4005f9e16309dd3946f40
data/bin/dcmetro CHANGED
@@ -1,7 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- API_KEY="n2z9aq3redes6k7jekjfzk8q" #"kfgpmgvfgacx98de9q3xazww" #developer's key
3
+ # You need to set an ENV variable in the shell which will be the Api Key.
4
+ # It is free from WMATA, https://developer.wmata.com/
5
+ # Referenced below was the old generic key used for testing the API on WMATA's site.
6
+ # Since api keys are free I would suggest registering for one, the rate limit is
7
+ # 10 calls/second, 50,000 calls/day which is pretty good. The generic key below had
8
+ # significant limits
9
+ #
10
+ #
11
+ API_KEY=ENV['DCMETRO_KEY'] #generic developer's key "kfgpmgvfgacx98de9q3xazww"
4
12
 
13
+ # The below colors are for experimenting with providing color
5
14
  # Color Reset
6
15
  COLOR_OFF="\033[0m" # Text Reset
7
16
 
@@ -14,6 +23,5 @@ YELLOW="\033[0;93m" # IYellow
14
23
  BLUE="\033[0;94m" # IBlue
15
24
 
16
25
  require 'dcmetro'
17
- # require_relative "dcmetro/cli/application"
18
26
 
19
27
  DCMetro::Cli::Application.start(ARGV)
data/dcmetro.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Ken Crocken"]
10
10
  spec.email = ["kcrocken@gmail.com"]
11
11
  spec.summary = %q{Returns DC Metro information, including train schedules.}
12
- # spec.description = %q{TODO: Write a longer description. Optional.}
13
- spec.homepage = ""
12
+ spec.description = %q{Returns DC Metro information, including train schedules.}
13
+ spec.homepage = "https://github.com/kencrocken/dcmetro"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'thor', '~> 0.18'
22
22
  spec.add_dependency 'json'
23
- spec.add_dependency 'rest_client'
23
+ spec.add_dependency 'rest-client'
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.6"
26
26
  spec.add_development_dependency "rake"
@@ -2,18 +2,32 @@ require 'thor'
2
2
  require 'json'
3
3
  require 'rest_client'
4
4
 
5
+ #
6
+ # This is the command line interface using Thor
7
+
5
8
  module DCMetro
6
9
  module Cli
7
10
  class Application < Thor
8
11
 
9
12
  desc 'alerts', 'Display DC Metro system wide alerts.'
10
13
  def alerts
14
+ #
15
+ # $dcmetro alerts
16
+ # => *** Alert! Alert! ***
17
+
11
18
  x = DCMetro::Information.new
12
19
  display_alerts x.alerts
13
20
  end
14
21
 
15
22
  desc 'line COLOR', 'Display metro rail lines, if COLOR, displays rail stations on the COLOR line'
16
23
  def line color=nil
24
+ #
25
+ # $dcmetro line
26
+ # => Orange, Blue, Silver, Red, etc ...
27
+ #
28
+ # $dcmetro line Red
29
+ # => Displays the stations on the Red Line
30
+
17
31
  x = DCMetro::Information.new
18
32
 
19
33
  if !color.nil?
@@ -26,6 +40,14 @@ module DCMetro
26
40
  desc 'station NAME', 'Display metro station train arrival and departure times.'
27
41
  method_option :alerts, :aliases => '-a', :type => :boolean, :description => "Display Metro wide alerts."
28
42
  def station(name)
43
+ #
44
+ # $dcmetro station Greenbelt
45
+ # => Displays the departure and arrival times at the Greenbelt Station
46
+ #
47
+ # $dcmetro station Greenbelt -a
48
+ # => Displays the alerts, departure and arrival times at the Greenbelt Station
49
+ #
50
+
29
51
  x = DCMetro::Information.new
30
52
 
31
53
  if options[:alerts]
@@ -37,7 +59,6 @@ module DCMetro
37
59
  train_time = x['Trains'].empty? ? "Sorry, there is no information for #{name}." : display_trains(x['Trains'])
38
60
  puts train_time if !train_time.kind_of?(Array)
39
61
  train_time
40
-
41
62
  end
42
63
 
43
64
  private
@@ -45,6 +66,9 @@ module DCMetro
45
66
  no_commands do
46
67
 
47
68
  def display_alerts alerts
69
+ #
70
+ # Formats the display of the alerts
71
+
48
72
  if alerts['Incidents'].empty?
49
73
  puts "*** No alerts reported. ***"
50
74
  else
@@ -54,6 +78,9 @@ module DCMetro
54
78
  end
55
79
 
56
80
  def display_trains trains
81
+ #
82
+ # Formats the display of the train arrival and departures
83
+
57
84
  puts "===== #{trains[0]['LocationName']} ====="
58
85
  trains.each do |prediction|
59
86
  puts "Line: #{prediction['Line']} | Towards: #{prediction['DestinationName']} | Arriving: #{prediction['Min']}"
@@ -1,3 +1,13 @@
1
1
  module Dcmetro
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
4
+
5
+ ### Changes in 0.0.2
6
+ #
7
+ # Updates the api calls to the new format
8
+ #
9
+ # Fixes a bug if multiple stations are returned
10
+ #
11
+ # Requires the use of an env variable for the API key
12
+ #
13
+ # Requires rest-client
data/lib/dcmetro.rb CHANGED
@@ -6,21 +6,28 @@ module DCMetro
6
6
  attr_accessor :metro_incidents, :metro_lines, :metro_stations, :station_code, :metro_time
7
7
 
8
8
  def initialize
9
- @metro_incidents = JSON.parse(RestClient.get "http://api.wmata.com/Incidents.svc/json/Incidents?api_key=#{API_KEY}")
10
- @metro_lines = JSON.parse(RestClient.get "http://api.wmata.com/Rail.svc/json/JLines?api_key=#{API_KEY}")
9
+ @metro_incidents = JSON.parse(RestClient.get "http://api.wmata.com/Incidents.svc/json/Incidents?api_key=#{API_KEY}&subscription-key=#{API_KEY}")
10
+ @metro_lines = JSON.parse(RestClient.get "http://api.wmata.com/Rail.svc/json/JLines?api_key=#{API_KEY}&subscription-key=#{API_KEY}")
11
11
  @metro_stations = metro_stations
12
12
  @station_code = ""
13
13
  @metro_time = metro_time
14
14
  end
15
15
 
16
16
  def alerts
17
+ #
18
+ # Makes the api call and returns the alerts
19
+
17
20
  @metro_incidents
18
21
  end ### alerts
19
22
 
20
23
  def line(color=nil)
24
+ #
25
+ # Makes the api call and returns either the stations on a particular line or
26
+ # if no color is passed, returns the metro lines
27
+
21
28
  if !color.nil?
22
29
  color = color.downcase
23
- @metro_stations = JSON.parse(RestClient.get "http://api.wmata.com/Rail.svc/json/jStations?LineCode=#{color}&api_key=#{API_KEY}")
30
+ @metro_stations = JSON.parse(RestClient.get "http://api.wmata.com/Rail.svc/json/jStations?LineCode=#{color}&api_key=#{API_KEY}&subscription-key=#{API_KEY}")
24
31
  @metro_stations['Stations']
25
32
  else
26
33
  @metro_lines['Lines']
@@ -28,21 +35,59 @@ module DCMetro
28
35
  end ### line
29
36
 
30
37
  def station(name)
31
- url = "http://api.wmata.com/Rail.svc/json/jStations?api_key=#{API_KEY}"
38
+ #
39
+ # Makes the api call to return all stations in the Metro rail system and
40
+ # then grabs the specific station passed by the user
41
+
42
+ # instantiates a new array to help check for multiple matching stations
43
+ stations_check = []
44
+
45
+ # forming the api call
46
+ url = "https://api.wmata.com/Rail.svc/json/jStations?api_key=#{API_KEY}&subscription-key=#{API_KEY}"
32
47
  @metro_stations = JSON.parse(RestClient.get "#{url}")
48
+
49
+ # Iterates through the response checking if the station name passed by the user
50
+ # is included in the return response
33
51
  @metro_stations['Stations'].each do |station_name|
34
52
  if station_name['Name'].downcase.include? name.downcase
35
- x = station_time station_name
36
- return x
53
+
54
+ # if the names of the station matches the user's station, the station
55
+ # is pushed to an array
56
+ stations_check.push(station_name)
37
57
  end
38
58
  end
59
+
60
+ # Oddly, the api seems to return some stations twice - since some stations have more than
61
+ # one line. Though the additional station information is contained in each instance of the
62
+ # station.
63
+ # We limit our array to only unique station names, hopefully limiting the array to a single item
64
+ stations_check.uniq! { |station| station['Name'] }
65
+
66
+ # If the array length is greater than 1, we ask the user to be more specific and
67
+ # return the names of the stations
68
+ if stations_check.length > 1
69
+ puts "****Multiple stations found****"
70
+ stations_check.each do |station|
71
+ puts station['Name']
72
+ puts station
73
+ end
74
+ abort "****Please be more specific****"
75
+ else
76
+ # We pass the station the station_time method to grab the predictions
77
+ station_time stations_check[0]
78
+ end
39
79
  end ### station
40
80
 
41
81
  private
42
82
 
83
+ #
84
+ # This makes an api call to grab the train arrival and departure predictions.
85
+ # If more than one line is present at a station, such is concatenated and
86
+ # the call is made on all lines.
87
+
43
88
  def station_time(station)
44
89
 
45
- # puts station['Code']
90
+ # If a station has multiple stations codes we join the codes together
46
91
  @station_code = station['Code']
47
92
  if !station['StationTogether1'].empty?
48
93
  @station_code += ",#{station['StationTogether1']}"
@@ -50,18 +95,13 @@ module DCMetro
50
95
  if !station['StationTogether2'].empty?
51
96
  @station_code += ",#{station['StationTogether2']}"
52
97
  end
53
- # p @station_code.class
54
- # @station_code = @station_code[1].nil? ? @station_code[0] : @station_code.join(",")
55
98
 
56
- # p @station_code
57
- url = "http://api.wmata.com/StationPrediction.svc/json/GetPrediction/#{@station_code}?api_key=#{API_KEY}"
58
- # p url
99
+ # The call to the api is made and the prediction times are returned
100
+ url = "http://api.wmata.com/StationPrediction.svc/json/GetPrediction/#{@station_code}?api_key=#{API_KEY}&subscription-key=#{API_KEY}"
59
101
  @metro_time = JSON.parse(RestClient.get "#{url}")
60
- # p @metro_time
61
102
  @metro_time
62
103
  end
63
104
 
64
-
65
105
  end ### Information
66
106
 
67
107
  end ### Metro
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dcmetro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Crocken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rest_client
42
+ name: rest-client
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ description: Returns DC Metro information, including train schedules.
84
84
  email:
85
85
  - kcrocken@gmail.com
86
86
  executables:
@@ -98,7 +98,7 @@ files:
98
98
  - lib/dcmetro.rb
99
99
  - lib/dcmetro/cli/application.rb
100
100
  - lib/dcmetro/version.rb
101
- homepage: ''
101
+ homepage: https://github.com/kencrocken/dcmetro
102
102
  licenses:
103
103
  - MIT
104
104
  metadata: {}
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.2.2
121
+ rubygems_version: 2.4.5
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: Returns DC Metro information, including train schedules.