Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bk bus gtfsrt #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
####################
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -175,7 +175,7 @@ PLATFORMS

DEPENDENCIES
awesome_print
beefcake (= 1.1.0.pre1)
beefcake (= 1.1.0)
better_errors
binding_of_caller
coffee-rails
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/buses_controller.rb
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions app/models/miami_dade_bus_translator.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/buses_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class BusesControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end

end