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 diff --git a/app/controllers/buses_controller.rb b/app/controllers/buses_controller.rb new file mode 100644 index 0000000..c44ad2e --- /dev/null +++ b/app/controllers/buses_controller.rb @@ -0,0 +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 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 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