Skip to content

Commit

Permalink
add JSON API
Browse files Browse the repository at this point in the history
  • Loading branch information
xoebus committed Mar 2, 2016
1 parent 4458619 commit c12ca8c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source "https://rubygems.org"

gem "sinatra", "~> 1.4"
gem "sinatra-contrib", "~> 1.4"

group :test do
gem "rspec", "~> 3.0"
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ GEM
remote: https://rubygems.org/
specs:
addressable (2.4.0)
backports (3.6.8)
crack (0.4.3)
safe_yaml (~> 1.0.0)
diff-lcs (1.2.5)
hashdiff (0.3.0)
multi_json (1.11.2)
rack (1.6.4)
rack-protection (1.5.3)
rack
Expand All @@ -29,6 +31,13 @@ GEM
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sinatra-contrib (1.4.6)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (>= 1.3, < 3)
tilt (2.0.2)
webmock (1.24.0)
addressable (>= 2.3.6)
Expand All @@ -42,6 +51,7 @@ DEPENDENCIES
rack-test (~> 0.6)
rspec (~> 3.0)
sinatra (~> 1.4)
sinatra-contrib (~> 1.4)
webmock (~> 1.24)

BUNDLED WITH
Expand Down
15 changes: 14 additions & 1 deletion lib/app.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'sinatra/base'
require "sinatra/base"
require "sinatra/json"

require_relative "radar"

Expand All @@ -16,4 +17,16 @@ class FlightSchool < Sinatra::Base
erb :no_airport
end
end

get "/api/airports/:code" do
begin
airport_status = Radar.status_for(params[:code])
json code: airport_status.code,
name: airport_status.name,
city: airport_status.city,
weather: airport_status.weather
rescue Radar::NoSuchAirport
status 404
end
end
end
10 changes: 10 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "json"
require "rack/test"

ENV['RACK_ENV'] = 'test'
Expand Down Expand Up @@ -36,4 +37,13 @@ def app
expect(last_response).to be_not_found
expect(last_response.body).to include("Couldn't find that airport!")
end

it "has a JSON API" do
get "/api/airports/SFO"
expect(last_response).to be_ok

parsed_body = JSON.parse(last_response.body)

expect(parsed_body["city"]).to eq("San Francisco")
end
end

0 comments on commit c12ca8c

Please sign in to comment.