-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.rb
More file actions
52 lines (43 loc) · 1.23 KB
/
Copy pathapi.rb
File metadata and controls
52 lines (43 loc) · 1.23 KB
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
module Api
class Root < Grape::API
version 'v1', using: :header, vendor: "your_api"
format :json
rescue_from ActiveRecord::RecordNotFound do |e|
message = "Record Not Found"
Rack::Response.new(
[{ status: 404, status_code: "not_found", error: message }.to_json],
404,
{ 'Content-Type' => 'application/json' }
)
end
rescue_from ActiveRecord::RecordInvalid do |e|
message = e.message.downcase.capitalize
Rack::Response.new(
[{ status: 403, status_code: "record_invalid", error: message }.to_json],
403,
{ 'Content-Type' => 'application/json' }
)
end
module CurrentResources
def current_user
@current_user ||= User.authenticate(env["X-Auth-Token"])
end
end
module Auth
def authenticate!
error!({status: 401, status_code: 'unauthorized'}, 401) unless current_user
end
end
helpers Api::Root::CurrentResources
helpers Api::Root::Auth
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
end
mount Api::Status
mount Api::Users
route :any, "*path" do
error!("404 Not Found", 404)
end
end
end