Created
July 9, 2012 19:55
-
-
Save stevie-chambers/3078525 to your computer and use it in GitHub Desktop.
SinatraAPI all-in-one script for testy-learny REST APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# [email protected] July 2012 | |
# | |
# I'm playing around with APIs, and sharing my learnings | |
# You can follow my trials and tribulations at http://viewyonder.com/apis | |
# | |
# This is a simple Ruby script to show how a simple API might work. | |
# The resource model is just a simple array of a single class - no back-end store (yet) | |
# | |
# The only goal for this version of the script is to all a human using a browser to GET resources | |
# Next version will do the POST, PUT and DELETE | |
# | |
require 'sinatra' | |
class Person | |
attr_accessor :id, :name | |
def initialize(id,name) | |
@id = id | |
@name = name | |
end | |
end | |
steve = Person.new('1','Steve') | |
alan = Person.new('2','Alan') | |
dave = Person.new('3','Dave') | |
team = { '1' => steve, '2' => alan, '3' => dave } | |
def request_html(request) | |
html = Array.new | |
html << "<h3>Request Object</h3>" | |
html << "<p>Request Method - #{request.request_method}</p>" | |
html << "<p>URL - #{request.url}</p>" | |
html << "<p>Accept - #{request.accept}</p>" | |
html << "<p>Body - #{request.body}</p>" | |
html << "<p>Query String - #{request.query_string}</p>" | |
html << "<p>Content Length - #{request.content_length}</p>" | |
html << "<p>Media Type - #{request.media_type}</p>" | |
html << "<p>HEADER - #{request["SOME_HEADER"]}</p>" # Use this later to authentication | |
html << "<p>User Agent - #{request.user_agent}</p>" | |
html | |
end | |
get '/' do | |
html = Array.new | |
html << %{<h1>Sinatra Web Server</h1>} | |
html << %{<h3>Resources</h3>} | |
html << %{<p><a href="/api" rel="api">Team API Root End Point</a></p>} | |
request_html(request).each { |p| html << p } | |
status 200 | |
content_type 'text/html' | |
body(html) | |
end | |
get '/api/?' do | |
html = Array.new | |
html << %{<h1>Team API</h1>} | |
html << %{<h3>Resources</h3>} | |
html << %{<p><a href="/api/team" rel="team">Team Resources</a></p>} | |
request_html(request).each { |p| html << p } | |
status 200 | |
content_type 'text/html' | |
body(html) | |
end | |
get '/api/team/?' do | |
html = Array.new | |
html << %{<h1>Team Resources</h1>} | |
html << %{<h3>Resources</h3>} | |
team.each do |key, person| | |
html << %{<li><a href="/api/team/#{person.id}" rel="#{person.name}">#{person.name}</a></li>} | |
end | |
request_html(request).each { |p| html << p } | |
status 200 | |
content_type 'text/html' | |
body(html) | |
end | |
get '/api/team/:id' do | |
person = team[params[:id]] | |
if person.nil? then | |
html = Array.new | |
html << %{<h1>Team Resource - Error</h1>} | |
html << %{<p>Unable to find the team member with ID: #{params[:id]}</p>} | |
request_html(request).each { |p| html << p } | |
status 404 | |
content_type 'text/html' | |
body(html) | |
else | |
html = Array.new | |
html << %{<h1>Team Resource - #{person.name}</h1>} | |
html << %{<p>ID - #{person.id}</p>} | |
html << %{<p>Name - #{person.name}</p>} | |
request_html(request).each { |p| html << p } | |
status 200 | |
content_type 'text/html' | |
body(html) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment