-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
49 lines (39 loc) · 1.15 KB
/
api.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
require 'bundler'
Bundler.require
lib_dir = File.expand_path('../lib', __FILE__)
$:.unshift lib_dir unless $:.include?(lib_dir)
require 'representers'
require 'models'
require 'grape_hack'
class API < Grape::API
format :json
helpers do
def expose(resource, representer)
return resource.map{|r| r.extend representer } if resource.is_a?(DataMapper::Collection)
resource.extend representer
end
end
resources :stages do
get do
expose Stage.all, StageRepresenter
end
get ':id' do
expose Stage.get(params[:id]), StageRepresenter
end
get ':stage_id/attractions' do
conditions = {}
conditions[:title.like] = "%#{params[:title]}%" if params[:title]
conditions[:description.like] = "%#{params[:description]}%" if params[:description]
conditions[:stage_id] = params[:stage_id]
expose Attraction.all(conditions: conditions), AttractionRepresenter
end
get ':stage_id/attractions/:id' do
expose Attraction.get(params[:id]), AttractionRepresenter
end
end
resources :attractions do
get do
expose attraction = Attraction.all, AttractionRepresenter
end
end
end