From a06776e75ff6382b0d1531e70cf53a5e19525e3f Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Wed, 30 Jan 2013 13:28:55 +0000 Subject: [PATCH] Add Sinatra app for feeds. --- Gemfile | 15 +++++++++++++++ Gemfile.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ config.ru | 6 ++++++ tubefeed.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 config.ru create mode 100644 tubefeed.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..bc902e5 --- /dev/null +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a1fa0ed --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..a49618a --- /dev/null +++ b/config.ru @@ -0,0 +1,6 @@ +require 'bundler' + +Bundler.setup +require './tubefeed' + +run Tubefeed diff --git a/tubefeed.rb b/tubefeed.rb new file mode 100644 index 0000000..764fac7 --- /dev/null +++ b/tubefeed.rb @@ -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