-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
79 lines (63 loc) · 1.73 KB
/
app.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
73
74
75
76
77
78
79
require 'active_record'
require './seeds'
require 'kaminari'
require 'sinatra/base'
require 'graphiti'
require 'graphiti/adapters/active_record'
class ApplicationResource < Graphiti::Resource
self.abstract_class = true
self.adapter = Graphiti::Adapters::ActiveRecord
self.base_url = 'http://localhost:4567'
self.endpoint_namespace = '/api/v1'
# implement Graphiti.config.context_for_endpoint for validation
self.validate_endpoints = false
end
class EmployeeResource < ApplicationResource
attribute :first_name, :string
attribute :last_name, :string
attribute :age, :integer
has_many :positions
end
class PositionResource < ApplicationResource
attribute :employee_id, :integer, only: [:filterable]
attribute :department_id, :integer, only: [:filterable]
attribute :title, :string
belongs_to :employee
belongs_to :department
end
class DepartmentResource < ApplicationResource
attribute :name, :string
has_many :positions
end
Graphiti.setup!
class EmployeeDirectoryApp < Sinatra::Application
configure do
mime_type :jsonapi, 'application/vnd.api+json'
end
before do
content_type :jsonapi
end
after do
ActiveRecord::Base.clear_active_connections!
end
get '/api/v1/employees' do
employees = EmployeeResource.all(params)
employees.to_jsonapi
end
get '/api/v1/employees/:id' do
employees = EmployeeResource.find(params)
employees.to_jsonapi
end
get '/api/v1/positions' do
positions = PositionResource.all(params)
positions.to_jsonapi
end
get '/api/v1/positions/:id' do
positions = PositionResource.find(params)
positions.to_jsonapi
end
get '/api/v1/departments/:id' do
departments = DepartmentResource.find(params)
departments.to_jsonapi
end
end