Skip to content

Instantly share code, notes, and snippets.

@ajosanchez
Created December 2, 2015 02:42
Show Gist options
  • Save ajosanchez/35f66b46013b5e0e5c45 to your computer and use it in GitHub Desktop.
Save ajosanchez/35f66b46013b5e0e5c45 to your computer and use it in GitHub Desktop.
simple api with Sinatra
#!/usr/bin/env/ ruby
require_relative('fp')
require 'sinatra'
require 'json'
t = FlightTracker.new
get '/' do
text = "<h1>*** Welcome to the Flight Tracker API ***</h1>"
text << "<p>To input a flight: localhost:4567/entry?flight=KS4567&altitude=10000</p>"
text << "<p>To get info on all flights: localhost:4567/tracking_info</p>"
erb text
end
get '/entry' do
plane = { :name => params[:flight], :altitude => params[:altitude], :toe => Time.now }
t.make_record_live(plane)
erb "Flight #{plane[:flight]} has been added with a TOE of #{plane[:toe]}."
end
get '/tracking_info' do
results = t.tracking_info
content_type :json
erb results.to_json
end
not_found do
erb "Page not found."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment