File: server.rb

package info (click to toggle)
ruby-sawyer 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 280 kB
  • sloc: ruby: 1,224; sh: 17; makefile: 4
file content (114 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'sinatra'
require 'json'

get '/' do
  app_type

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

def app_type
  content_type "application/vnd.sushihub+json"
end

users = [
  {:id => 1, :login => 'sawyer',  :created_at => Time.utc(2004, 9, 22),
   :_links => {
     :self               => {:href => '/users/sawyer'},
     :favorites          => {:href => '/users/sawyer/favorites', :method => 'get,post'}
   }},
  {:id => 2, :login => 'faraday', :created_at => Time.utc(2004, 12, 22),
   :_links => {
     :self               => {:href => '/users/faraday'},
     :favorites          => {:href => '/users/faraday/favorites', :method => 'get,post'}
   }}
]

nigiri = [
  {:id => 1, :name => 'sake',  :fish => 'salmon',
   :_links => {
     :self => {:href => '/nigiri/sake'}
   }},
  {:id => 2, :name => 'unagi', :fish => 'eel',
   :_links => {
     :self => {:href => '/nigiri/unagi'}
   }}
]

get '/users' do
  app_type

  body JSON.dump users
end

new_users = {}
post '/users' do
  if env['CONTENT_TYPE'].to_s !~ /json/i
    halt 400, "Needs JSON"
  end

  app_type

  hash = JSON.load request.body.read
  new_users[hash[:login]] = hash
  
  headers "Location" => "/users/#{hash[:login]}"
  status 201
  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'}
    }
  )
end

get '/users/:login' do
  headers 'Content-Type' => app_type
  if hash = users.detect { |u| u[:login] == params[:login] }
    body JSON.dump hash
  else
    halt 404
  end
end

get '/users/:login/favorites' do
  app_type

  case params[:login]
  when users[0][:login] then body JSON.dump([nigiri[0]])
  when users[1][:login] then body JSON.dump([])
  else halt 404
  end
end

post '/users/:login/favorites' do
  if params[:id].to_i > 0
    halt 201
  else
    halt 422
  end
end

get '/nigiri' do
  app_type

  body JSON.dump nigiri
end

get '/nigiri/:name' do
  app_type

  if hash = nigiri.detect { |n| n[:name] == params[:name] }
    body JSON.dump hash
  else
    halt(404)
  end
end