forked from sethherr/grape-doorkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme.rb
64 lines (57 loc) · 2.1 KB
/
me.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
module API
module V1
class Me < API::V1::Root
include API::V1::Defaults
resource :me, desc: 'Operations about the current user' do
desc "Current user's information in access token's scope<span class='accstr'>*</span>" do
detail <<-NOTE
Current user is the owner of the `access_token` you use in the request.
NOTE
end
oauth2 # This endpoint requires authentication
get '/' do
{
id: current_user.id.to_s,
user: {
email: current_user.email,
name: current_user.name
}
}
end
# This endpoint doesn't require authentication
paginate per_page: 10 # This paginates, with a default of 10 per page
get '/items', each_serializer: ItemSerializer do
# create some imaginary items
items = (0..19).map{ |i|
ImaginaryItem.new(name: "Foo #{i*7}", id: i, secret: 'Bar')
}
# We use the item serializer to serialize these items!
paginate items
end
desc 'Update user, Protected, only accessible with write user'
params do
optional :email, type: String, desc: 'User email'
optional :name, type: String, desc: 'User name'
# Demo of value list, displays as select in swagger-ui
optional :demo_value, type: String, values: ['value 1', 'value 2', '3'], default: 'value 1', desc: 'Special value list'
end
oauth2 'write_user'
put '/' do
# By using declared params, we ensure we only use the whitelisted params
# and ignore missing params (not setting their values to nil)
declared_p = declared(params, include_missing: false)
demo_value = declared_p.delete(:demo_value)
current_user.update_attributes(declared_p)
{
id: current_user.id.to_s,
user: {
value: demo_value,
email: current_user.email,
name: current_user.name
}
}
end
end
end
end
end