1
0
mirror of https://github.com/danbee/tube-status-server synced 2025-03-04 08:39:12 +00:00

Add Sinatra app for feeds.

This commit is contained in:
Dan Barber 2013-01-30 13:28:55 +00:00
parent 7cc010258d
commit a06776e75f
4 changed files with 114 additions and 0 deletions

15
Gemfile Normal file
View File

@ -0,0 +1,15 @@
source :rubygems
gem 'sinatra'
gem 'sinatra-jsonp'
gem 'faraday'
gem 'xml-simple'
gem 'foreman'
group :development do
gem 'shotgun'
end
group :production do
gem 'thin'
end

42
Gemfile.lock Normal file
View File

@ -0,0 +1,42 @@
GEM
remote: http://rubygems.org/
specs:
daemons (1.1.9)
eventmachine (1.0.0)
faraday (0.8.4)
multipart-post (~> 1.1)
foreman (0.61.0)
thor (>= 0.13.6)
multi_json (1.3.7)
multipart-post (1.1.5)
rack (1.5.0)
rack-protection (1.3.2)
rack
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
sinatra-jsonp (0.4.1)
multi_json (~> 1.3.7)
sinatra (~> 1.0)
thin (1.5.0)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.17.0)
tilt (1.3.3)
xml-simple (1.1.2)
PLATFORMS
ruby
DEPENDENCIES
faraday
foreman
shotgun
sinatra
sinatra-jsonp
thin
xml-simple

6
config.ru Normal file
View File

@ -0,0 +1,6 @@
require 'bundler'
Bundler.setup
require './tubefeed'
run Tubefeed

51
tubefeed.rb Normal file
View File

@ -0,0 +1,51 @@
require 'sinatra'
require 'json'
require 'xmlsimple'
require 'faraday'
class Tubefeed < Sinatra::Base
def parse_xml_feed(feed_url)
uri = URI(feed_url)
conn = Faraday.new(:url => "#{uri.scheme}://#{uri.host}")
response = conn.get(uri.request_uri)
XmlSimple.xml_in response.body
end
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
get '/now.json' do
feed_data = parse_xml_feed("http://cloud.tfl.gov.uk/TrackerNet/LineStatus")
# Parse the XML into the appropriate structure for our app.
data = feed_data["LineStatus"].map do |line|
{ :id => line["Line"].first["Name"].downcase.gsub(/ (and)?/, ""),
:name => line["Line"].first["Name"],
:status => line["Status"].first["Description"].downcase,
:messages => [] }
end
JSON data
end
get '/weekend.json' do
feed_data = parse_xml_feed("http://www.tfl.gov.uk/tfl/businessandpartners/syndication/feed.aspx?email=#{ENV["TUBESTATUS_FEED_EMAIL"]}&feedId=1")
# Parse the XML into the appropriate structure for our app.
data = feed_data["Lines"].first["Line"].map do |line|
messages = line["Status"].first["Message"].first["Text"]
# Ensure messages is an empty array if there are no messages.
if messages.first.is_a?(Hash)
messages = []
end
{ :id => line["Name"].first.downcase.gsub(/ (&)?/, ""),
:name => line["Name"].first,
:status => line["Status"].first["Text"].first.downcase,
:messages => messages }
end
JSON data
end
end