-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.rb
64 lines (50 loc) · 1.43 KB
/
application.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
# application.rb
require 'grape'
require 'mongoid'
require 'hashie-forbidden_attributes'
Mongoid.load! "config/mongoid.config"
META_DATA = {
name: 'Bloggy',
description: 'A simple blogging API built with Grape.'
}
# Load files from the models and api folders
Dir["#{File.dirname(__FILE__)}/app/models/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/app/api/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/app/yumi/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/app/presenters/**/*.rb"].each {|f| require f}
# Grape API class. We inherit from it in our controllers.
module API
class Root < Grape::API
prefix :api
format :json
formatter :json, -> (object, _env) { object.to_json }
content_type :json, 'application/vnd.api+json'
helpers do
def base_url
"http://#{request.host}:#{request.port}/api/#{version}"
end
def invalid_media_type!
error!('Unsupported media type', 415)
end
def json_api?
request.content_type == 'application/vnd.api+json'
end
end
before do
invalid_media_type! unless json_api?
end
# Simple endpoint to get the current status of our API.
get :status do
{ status: 'ok' }
end
mount V1::Admin::Posts
mount V1::Comments
mount V1::Posts
end
end
# Mounting the Grape application
SamurailsBlog = Rack::Builder.new {
map "/" do
run API::Root
end
}