Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
This swaps out Yajl for JSON and fixes a couple of Sinatra errors to get
the examples working.
  • Loading branch information
pengwynn committed Oct 9, 2015
1 parent 714ed7c commit d760db4
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions example/server.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'sinatra'
require 'yajl'
require 'json'

get '/' do
app_type

Yajl.dump({
body JSON.dump({
:_links => {
:users => {:href => "/users", :method => 'get,post'},
:nigiri => {:href => "/nigiri"}
}
}, :pretty => true)
})
end

def app_type
Expand Down Expand Up @@ -43,7 +43,7 @@ def app_type
get '/users' do
app_type

Yajl.dump users, :pretty => true
body JSON.dump users
end

new_users = {}
Expand All @@ -54,25 +54,25 @@ def app_type

app_type

hash = Yajl.load request.body.read, :symbolize_keys => true
hash = JSON.load request.body.read
new_users[hash[:login]] = hash

headers "Location" => "/users/#{hash[:login]}"
status 201
Yajl.dump hash.update(
body JSON.dump hash.update(
:id => 3,
:created_at => Time.now.utc.xmlschema,
:_links => {
:self => {:href => "/users/#{hash[:login]}"},
:favorites => {:href => "/users/#{hash[:login]}/favorites", :method => 'get,post'}
}
), :pretty => true
)
end

get '/users/:login' do
headers 'Content-Type' => app_type
if hash = users.detect { |u| u[:login] == params[:login] }
Yajl.dump hash, :pretty => true
body JSON.dump hash
else
halt 404
end
Expand All @@ -82,8 +82,8 @@ def app_type
app_type

case params[:login]
when users[0][:login] then Yajl.dump([nigiri[0]], :pretty => true)
when users[1][:login] then Yajl.dump([], :pretty => true)
when users[0][:login] then body JSON.dump([nigiri[0]])
when users[1][:login] then body JSON.dump([])
else halt 404
end
end
Expand All @@ -99,14 +99,14 @@ def app_type
get '/nigiri' do
app_type

Yajl.dump nigiri, :pretty => true
body JSON.dump nigiri
end

get '/nigiri/:name' do
app_type

if hash = nigiri.detect { |n| n[:name] == params[:name] }
Yajl.dump hash, :pretty => true
body JSON.dump hash
else
halt(404)
end
Expand Down

0 comments on commit d760db4

Please sign in to comment.