forked from lostisland/sawyer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6ee3c4
commit 55b72e5
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"type": "object", | ||
"verbs": ["GET"], | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"name": { | ||
"type": "string" | ||
}, | ||
"fish": { | ||
"type": "string" | ||
}, | ||
"_links": { | ||
"type": "object", | ||
"properties": { | ||
"self": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
} | ||
} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'sinatra' | ||
require 'yajl' | ||
|
||
get '/' do | ||
Yajl.dump({ | ||
:name => 'sushihub', | ||
:_links => { | ||
:users => '/users', | ||
:nigiri => '/nigiri' | ||
} | ||
}, :pretty => true) | ||
end | ||
|
||
users = [ | ||
{:id => 1, :login => 'sawyer', :created_at => Time.utc(2004, 9, 22), | ||
:_links => {:self => '/users/sawyer', :favorites => '/users/sawyer/favorites'}}, | ||
{:id => 2, :login => 'faraday', :created_at => Time.utc(2004, 12, 22), | ||
:_links => {:self => '/users/faraday', :favorites => '/users/faraday/favorites'}} | ||
] | ||
|
||
nigiri = [ | ||
{:id => 1, :name => 'sake', :fish => 'salmon', | ||
:_links => {:self => '/nigiri/sake'}}, | ||
{:id => 2, :name => 'unagi', :fish => 'eel', | ||
:_links => {:self => '/nigiri/unagi'}} | ||
] | ||
|
||
get '/users' do | ||
headers 'content-type' => 'application/vnd.sushihub.user+json; profile=/schema/user' | ||
Yajl.dump users, :pretty => true | ||
end | ||
|
||
get '/users/:login' do | ||
headers 'content-type' => 'application/vnd.sushihub.user+json; profile=/schema/user' | ||
if hash = users.detect { |u| u[:login] == params[:login] } | ||
Yajl.dump hash, :pretty => true | ||
else | ||
halt 404 | ||
end | ||
end | ||
|
||
get '/users/:login/favorites' do | ||
headers 'content-type' => 'application/vnd.sushihub.nigiri+json; profile=/schema/nigiri' | ||
case params[:login] | ||
when users[0][:login] then Yajl.dump([nigiri[0]], :pretty => true) | ||
when users[1][:login] then Yajl.dump([], :pretty => true) | ||
else halt 404 | ||
end | ||
end | ||
|
||
get '/nigiri' do | ||
headers 'content-type' => 'application/vnd.sushihub.nigiri+json; profile=/schema/nigiri' | ||
Yajl.dump nigiri, :pretty => true | ||
end | ||
|
||
get '/nigiri/:name' do | ||
headers 'content-type' => 'application/vnd.sushihub.nigiri+json; profile=/schema/nigiri' | ||
if hash = nigiri.detect { |n| n[:name] == params[:name] } | ||
Yajl.dump hash, :pretty => true | ||
else | ||
halt(404) | ||
end | ||
end | ||
|
||
get '/schema' do | ||
Yajl.dump([ | ||
{:content_type => 'application/vnd.sushihub.user+json', :_links => {:self => '/schema/user'}}, | ||
{:content_type => 'application/vnd.sushihub.nigiri+json', :_links => {:self => '/schema/nigiri'}} | ||
], :pretty => true) | ||
end | ||
|
||
get '/schema/:type' do | ||
path = File.expand_path("../#{params[:type]}.schema.json", __FILE__) | ||
if File.exist?(path) | ||
headers 'content-type' => 'application/json' | ||
IO.read path | ||
else | ||
halt 404 | ||
end | ||
end | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"type": "object", | ||
"verbs": ["GET"], | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"login": { | ||
"type": "string" | ||
}, | ||
"created_at": { | ||
"type": "string", | ||
"pattern": "\\d{8}T\\d{6}Z" | ||
}, | ||
"_links": { | ||
"type": "object", | ||
"properties": { | ||
"self": { | ||
"type": "string" | ||
}, | ||
"favorites": { | ||
"type": "string", | ||
"profile": "/schema/user", | ||
"verbs": ["GET", "POST"], | ||
"optional": true | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
} | ||
} | ||
|