From 01ed4ac73e0af3ff633310f464e040e1e43db573 Mon Sep 17 00:00:00 2001 From: Bryce Kerley Date: Mon, 27 Jul 2015 19:48:25 -0400 Subject: [PATCH 1/4] update gems --- Gemfile | 4 ++-- Gemfile.lock | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index fe56a48..0d268cd 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,8 @@ source 'https://rubygems.org' -ruby '2.1.2' +ruby '2.2.2' gem 'rails', '4.2.0' -gem 'beefcake', '1.1.0.pre1' +gem 'beefcake', '1.1.0' # Libraries #################### diff --git a/Gemfile.lock b/Gemfile.lock index 788e55b..06c88e3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -38,7 +38,7 @@ GEM tzinfo (~> 1.1) arel (6.0.0) awesome_print (1.6.1) - beefcake (1.1.0.pre1) + beefcake (1.1.0) better_errors (2.1.1) coderay (>= 1.0.0) erubis (>= 2.6.6) @@ -175,7 +175,7 @@ PLATFORMS DEPENDENCIES awesome_print - beefcake (= 1.1.0.pre1) + beefcake (= 1.1.0) better_errors binding_of_caller coffee-rails From 1f9e1c698436d9946fea4af1c1cf966bee903bc6 Mon Sep 17 00:00:00 2001 From: Bryce Kerley Date: Mon, 27 Jul 2015 19:50:40 -0400 Subject: [PATCH 2/4] start bus controller for gtfsrt integration --- app/controllers/buses_controller.rb | 4 ++++ config/routes.rb | 3 ++- test/controllers/buses_controller_test.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 app/controllers/buses_controller.rb create mode 100644 test/controllers/buses_controller_test.rb diff --git a/app/controllers/buses_controller.rb b/app/controllers/buses_controller.rb new file mode 100644 index 0000000..342a92f --- /dev/null +++ b/app/controllers/buses_controller.rb @@ -0,0 +1,4 @@ +class BusesController < ApplicationController + def index + end +end diff --git a/config/routes.rb b/config/routes.rb index 56f46aa..fefb642 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,8 @@ get 'api/:endpoint' => 'miami_dade_transit#proxy' - get 'trolley.:format' => 'trolley#index' + get 'trolley.:format' => 'trolleys#index' + get 'bus.:format' => 'buses#index' resources :trolleys, only: [:index] do # txt files diff --git a/test/controllers/buses_controller_test.rb b/test/controllers/buses_controller_test.rb new file mode 100644 index 0000000..9e741e4 --- /dev/null +++ b/test/controllers/buses_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class BusesControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + +end From 70a882dc058ffce8ddd342696f7996dde694a958 Mon Sep 17 00:00:00 2001 From: Bryce Kerley Date: Mon, 27 Jul 2015 20:22:03 -0400 Subject: [PATCH 3/4] add json and gtfsrt to new buses_controller --- app/controllers/buses_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/buses_controller.rb b/app/controllers/buses_controller.rb index 342a92f..c44ad2e 100644 --- a/app/controllers/buses_controller.rb +++ b/app/controllers/buses_controller.rb @@ -1,4 +1,11 @@ class BusesController < ApplicationController def index + bus_xml = MiamiDadeTransit.buses.body + translator = MiamiDadeBusTranslator.new bus_xml + + respond_to do |format| + format.json { render json: translator.to_hash.to_json } + format.gtfsrt { send_data translator.to_gtfs } + end end end From 85b5043f80ad00e08651f38f927f62874991eaaf Mon Sep 17 00:00:00 2001 From: Bryce Kerley Date: Mon, 27 Jul 2015 20:22:10 -0400 Subject: [PATCH 4/4] start translating county bus data --- app/models/miami_dade_bus_translator.rb | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 app/models/miami_dade_bus_translator.rb diff --git a/app/models/miami_dade_bus_translator.rb b/app/models/miami_dade_bus_translator.rb new file mode 100644 index 0000000..5b593b2 --- /dev/null +++ b/app/models/miami_dade_bus_translator.rb @@ -0,0 +1,60 @@ +class MiamiDadeBusTranslator + def initialize(bus_xml) + @bus_xml = bus_xml + end + + def to_gtfs + message.encode + end + + def to_hash + message.to_hash + end + + private + def message + return @message if defined? @message + + @message = FeedMessage.new.tap do |msg| + msg.header = header + msg.entity = entities + end + end + + def header + @header ||= FeedHeader.new.tap do |head| + head.gtfs_realtime_version = '1.0' + end + end + + def document + @document ||= Nokogiri::XML.parse @bus_xml + end + + def entities + document.css('Record').map do |record| + bus_hash = record.children.map do |c| + { c.name => c.content } + end.inject(&:merge) + + FeedEntity.new.tap do |entity| + entity.id = "#{ bus_hash['BusID'] }_#{ bus_hash['BusName']}" + + + entity.trip_update = TripUpdate.new.tap do |tu| + tu.trip = TripDescriptor.new.tap do |trip| + trip.route_id = bus_hash['RouteID'] + trip.trip_id = bus_hash['TripID'] + end + end + + entity.vehicle = VehiclePosition.new.tap do |vp| + vp.position = Position.new.tap do |pos| + pos.latitude = bus_hash['Latitude'].to_f + pos.longitude = bus_hash['Longitude'].to_f + end + end + end + end + end +end