Class: DCMetro::Information

Inherits:
Object
  • Object
show all
Defined in:
lib/dcmetro.rb

Constant Summary collapse

BASE_URL =
"https://api.wmata.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInformation

Returns a new instance of Information.



10
11
12
13
14
15
16
17
# File 'lib/dcmetro.rb', line 10

def initialize
  @metro_incidents = metro_incidents
  @metro_lines = metro_lines
  @metro_stations = metro_stations
  @station_code = ""
  @metro_time = metro_time
  @travel_info = travel_info
end

Instance Attribute Details

#metro_incidentsObject

Returns the value of attribute metro_incidents.



6
7
8
# File 'lib/dcmetro.rb', line 6

def metro_incidents
  @metro_incidents
end

#metro_linesObject

Returns the value of attribute metro_lines.



6
7
8
# File 'lib/dcmetro.rb', line 6

def metro_lines
  @metro_lines
end

#metro_stationsObject

Returns the value of attribute metro_stations.



6
7
8
# File 'lib/dcmetro.rb', line 6

def metro_stations
  @metro_stations
end

#metro_timeObject

Returns the value of attribute metro_time.



6
7
8
# File 'lib/dcmetro.rb', line 6

def metro_time
  @metro_time
end

#station_codeObject

Returns the value of attribute station_code.



6
7
8
# File 'lib/dcmetro.rb', line 6

def station_code
  @station_code
end

#travel_infoObject

Returns the value of attribute travel_info.



6
7
8
# File 'lib/dcmetro.rb', line 6

def travel_info
  @travel_info
end

Instance Method Details

#alertsObject



19
20
21
22
23
24
25
26
27
# File 'lib/dcmetro.rb', line 19

def alerts
  #
  # Makes the api call and returns the alerts
  @metro_incidents = RestClient.get "#{BASE_URL}/Incidents.svc/json/Incidents", :params => {
      "api_key" => API_KEY,
      "subscription-key" => API_KEY
  }

end

#line(color = nil) ⇒ Object

alerts



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dcmetro.rb', line 29

def line(color=nil)
  #
  # Makes the api call and returns either the stations on a particular line or
  # if no color is passed, returns the metro lines

  if !color.nil?
    color = color.downcase

    case color
    when "red"
      color = "RD"
    when "green"
      color = "GR"
    when "yellow"
      color = "YL"
    when "blue"
      color = "BL"
    when "orange"
      color = "OR"
    else
      color = "SV"
    end
    
    @metro_stations = RestClient.get "#{BASE_URL}/Rail.svc/json/jStations", :params => {
    "LineCode" => color,
    "api_key" => API_KEY,
    "subscription-key" => API_KEY
    }

    # @metro_stations = parse_json metro_stations
    # @metro_stations['Stations']
  else
    @metro_lines = RestClient.get "#{BASE_URL}/Rail.svc/json/JLines", :params => {
    "api_key" => API_KEY,
    "subscription-key" => API_KEY
    }

    # @metro_lines = metro_lines
    # @metro_lines['Lines']

    # @metro_lines = get_all_stations
  end
end

#station(source, destination = nil) ⇒ Object

line



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/dcmetro.rb', line 73

def station(source,destination=nil)
  #
  # Makes the api call to return all stations in the Metro rail system and
  # then grabs the specific station passed by the user

  # instantiates a new array to help check for multiple matching stations
  stations_check = []

  # forming the api call
  @metro_stations = JSON.parse(get_all_stations)

  if destination.nil?
    # Iterates through the response checking if the station name passed by the user
    # is included in the return response
    @metro_stations['Stations'].each do |station_name|
      if station_name['Name'].downcase.include? source.downcase
        # if the names of the station matches the user's station, the station
        # is pushed to an array
        stations_check.push(station_name)
      end
    end
    # Oddly, the api seems to return some stations twice - since some stations have more than
    # one line.  Though the additional station information is contained in each instance of the 
    # station. 
    # We limit our array to only unique station names, hopefully limiting the array to a single item
    stations_check.uniq! { |station| station['Name'] }

    # If the array length is greater than 1, we ask the user to be more specific and 
    # return the names of the stations
    if stations_check.length > 1
      puts "****Multiple stations found****"
      stations_check.each_with_index do |station,i|
        puts  "#{i} #{station['Name']}"
      end
      puts "****Please be more specific, enter the number below ****"
      specific = STDIN.gets.chomp.to_i
      station_time stations_check[specific]
    else
      # We pass the station the station_time method to grab the predictions
      station_time stations_check[0]
    end
  else
    stations = [source, destination]
    station_code = []
    stations.each do |station|
      @metro_stations['Stations'].each do |station_name|
        if station_name['Name'].downcase.include? station.downcase
          station_code << station_name
        end
      end
    end
    station_code.uniq! { |station| station['Name'] }
    if station_code.length > 2
      puts "****Multiple stations found****"
      station_code.each_with_index do |station,i|
        puts  "#{i} #{station['Name']}"
      end
      puts "****Please be more specific****"
      puts "Enter the number of your starting station."
      start = STDIN.gets.chomp.to_i
      puts "Enter the number of your destination station."
      destination = STDIN.gets.chomp.to_i
      @travel_info = RestClient.get "#{BASE_URL}/Rail.svc/json/jSrcStationToDstStationInfo", :params => {
        "FromStationCode" => station_code[start]['Code'],
        "ToStationCode" => station_code[destination]['Code'],
        "api_key" => API_KEY,
        "subscription-key" => API_KEY
      }
    else
      @travel_info = RestClient.get "#{BASE_URL}/Rail.svc/json/jSrcStationToDstStationInfo", :params => {
        "FromStationCode" => station_code[0]['Code'],
        "ToStationCode" => station_code[1]['Code'],
        "api_key" => API_KEY,
        "subscription-key" => API_KEY
      }
    end
  end
end