Permalink
Browse files

Implement /stopinfo, which gets data for train stations.

Add the /stopinfo method, which the JavaScript calls to get info about
a train station when the user clicks on it.

Include webmock and use it to test /bikedata and /stopinfo, with mock
HTTP requests that return hard-coded responses. Still need to parse the
XML returned by /bikedata to check its contents.

The Rails version now has all the functionality of the Sinatra version,
plus it's got tests!
  • Loading branch information...
1 parent 3c4f2c8 commit 0359aebe7de38cb780f98a771dd44b8387b1c376 @amonat committed May 13, 2012
Showing with 82 additions and 4 deletions.
  1. +1 −0 Gemfile
  2. +6 −0 Gemfile.lock
  3. +36 −3 app/controllers/bike_controller.rb
  4. +1 −0 config/routes.rb
  5. +36 −1 spec/controllers/bike_controller_spec.rb
  6. +2 −0 spec/spec_helper.rb
View
@@ -13,6 +13,7 @@ end
group :test do
gem 'capybara', '1.1.2'
+ gem "webmock", "~> 1.8.7"
end
group :production do
View
@@ -28,6 +28,7 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
+ addressable (2.2.8)
arel (3.0.2)
builder (3.0.0)
capybara (1.1.2)
@@ -46,6 +47,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.3.1)
+ crack (0.3.1)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.2)
@@ -131,6 +133,9 @@ GEM
uglifier (1.2.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ webmock (1.8.7)
+ addressable (>= 2.2.7)
+ crack (>= 0.1.7)
xpath (0.1.4)
nokogiri (~> 1.3)
@@ -147,3 +152,4 @@ DEPENDENCIES
sass-rails (= 3.2.4)
sqlite3
uglifier (= 1.2.3)
+ webmock (~> 1.8.7)
@@ -11,11 +11,44 @@ def about
def bikedata
@stations = xmlDoc('http://www.thehubway.com/data/stations/bikeStations.xml')
- render :xml => @stations
+ render xml: @stations
end
def stopinfo
- @trains = ['train']
- render :json => @trains
+ line = params[:line]
+ stop = params[:stop]
+ if (line == '' || stop == '')
+ render text: 'Bad request: missing stop or line', status: 400
+ return
+ end
+
+ if (line != 'red' && line != 'blue' && line != 'orange')
+ render text: 'Bad request: line must be red, blue or orange', status: 400
+ return
+ end
+
+ # Capitalize the first letter of the line to get the file name
+ file = line[0..0].upcase + line[1..-1] + '.txt'
+ url = 'http://developer.mbta.com/Data/' + file
+
+ begin
+ trains = txtDoc(url)
+ rescue RestClient::Request::Unauthorized
+ status 403
+ return 'Could not get train information'
+ end
+
+ system = {red: {direction1: 'Ashmont/Braintree', direction2: 'Alewife', compass1: 'S', compass2: 'N'},
+ orange: {direction1: 'Forest Hills', direction2: 'Oak Grove', compass1: 'S', compass2: 'N'},
+ blue: {direction1: 'Wonderland', direction2: 'Bowdoin', compass1: 'E', compass2: 'W'}}
+ lineInfo = system[line.to_sym]
+
+ @trains = { :direction1 => lineInfo[:direction1],
+ :direction2 => lineInfo[:direction2],
+ :times1 => predictedTrainTimes(trains, stop + lineInfo[:compass1]),
+ :times2 => predictedTrainTimes(trains, stop + lineInfo[:compass2])
+ }
+
+ render json: @trains
end
end
View
@@ -2,6 +2,7 @@
root to: 'bike#home'
match 'about' => 'bike#about', as: :about
match 'bikedata' => 'bike#bikedata', as: :bikedata
+ match 'stopinfo' => 'bike#stopinfo', as: :stopinfo
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -5,6 +5,31 @@
describe "GET bikedata XML resource" do
it "assigns a @stations variable" do
+ responseBody =
+ """
+ <?xml version='1.0' encoding='UTF-8'?>
+ <stations lastUpdate='1336854314155' version='2.0'>
+ <station>
+ <id>3</id>
+ <name>Colleges of the Fenway</name>
+ <terminalName>B32006</terminalName>
+ <lat>42.340021</lat>
+ <long>-71.100812</long>
+ <installed>true</installed>
+ <locked>false</locked>
+ <installDate/>
+ <removalDate/>
+ <temporary>false</temporary>
+ <nbBikes>1</nbBikes>
+ <nbEmptyDocks>14</nbEmptyDocks>
+ <latestUpdateTime>1336853983619</latestUpdateTime>
+ </station>
+ </stations>
+ """
+
+ stub_request(:get, "http://www.thehubway.com/data/stations/bikeStations.xml").
+ to_return(:status => 200, :body => responseBody)
+
get :bikedata
assigns(:stations).should_not be_empty
end
@@ -13,8 +38,18 @@
describe "GET stopinfo JSON resource" do
it "assigns a @trains variable" do
- get :stopinfo
+ redLineUrl = 'http://developer.mbta.com/Data/Red.txt'
+ stub_request(:get, redLineUrl).
+ to_return(:body => 'Red, 579, RDAVN, Predicted, 5/12/2012 4:08:46 PM, 00:01:56, Revenue, 0')
+
+ get :stopinfo, line: 'red', stop: 'RDAV'
assigns(:trains).should_not be_empty
+ assigns(:trains)[:direction1].should eq('Ashmont/Braintree')
+ assigns(:trains)[:direction2].should eq('Alewife')
+ assigns(:trains)[:times1].should be_empty
+ assigns(:trains)[:times2].should eq(["1 min."])
+
+ stub_request(:get, redLineUrl).with().should have_been_made.once
end
end
View
@@ -3,13 +3,15 @@
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
+require 'webmock/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# ## Mock Framework
+ include WebMock::API
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#

0 comments on commit 0359aeb

Please sign in to comment.