-
Notifications
You must be signed in to change notification settings - Fork 5
/
service.rb
72 lines (65 loc) · 1.34 KB
/
service.rb
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
require 'rubygems'
require 'sinatra'
require 'models/user'
require 'mongoid'
require 'yajl'
configure do
Mongoid.configure do |config|
name = "fsg_ident"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
end
end
before do
content_type :json
end
# get a user by id
get '/api/v1/users/:id' do
user = User.find(params[:id])
if user
user.to_json
else
error 404, {:error => "user not found"}.to_json
end
end
# authenticate user
post '/api/v1/sessions/' do
begin
attributes = Yajl::Parser.parse(request.body.read)
user = User.authenticate(attributes["email"],attributes["password"])
if user
user.to_json
else
error 404, "invalid login credentials"
end
rescue => e
error 400, e.message.to_json
end
end
# create user
post '/api/v1/users/' do
begin
user = User.new(Yajl::Parser.parse(request.body.read))
if user.save
user.to_json
else
error 400, user.errors.to_json
end
rescue => e
error 400, e.message.to_json
end
end
# update an existing user
put '/api/v1/users/:id' do
user = User.find(params[:id])
if user
begin
user = user.update_attributes!(Yajl::Parser.parse(request.body.read))
user.to_json
rescue => e
error 400, e.message.to_json
end
else
error 404, "user not found".to_json
end
end