-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.rb
92 lines (76 loc) · 1.97 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
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
80
81
82
83
84
85
86
87
88
89
90
91
require 'sinatra'
require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/burgers_and_beers.db")
require "#{Dir.pwd}/models/day_of_week.rb"
require "#{Dir.pwd}/models/offer.rb"
require "#{Dir.pwd}/models/item.rb"
require "#{Dir.pwd}/models/bar.rb"
DataMapper.finalize
before do
response['Access-Control-Allow-Origin'] = '*'
end
get '/' do
"Welcome to the Chamonix Hackathon 2015 API!i <br />"\
"From here, you can browse the API:<br />"\
"Bars: #{Bar.all.map{|b| "#{b.name} (#{b.offers.count} offers)"}.join(', ')}<br />"\
"Days of week: #{DayOfWeek.all.map{|d| d.name}.join(', ')}"
end
get '/bars' do
content_type :json, 'charset' => 'utf-8'
@bars = Bar.all
@bars.to_json(
only: [:id, :name, :image_url],
methods: [:location, :tagged]
)
end
get '/bars/:id' do
content_type :json, 'charset' => 'utf-8'
@bar = Bar.all(id: params[:id])
@bar.to_json(
only: [:id, :image_url, :name],
methods: [:location, :tagged],
relationships: {
ordered_offers: {
only: [:id, :type],
methods: [:day, :starts, :ends, :tagged]
}
})
end
get '/bars/:id/menu' do
content_type :json, 'charset' => 'utf-8'
@items = Item.all(bar_id: params[:id])
@items.to_json(
only: [:id, :type, :price]
)
end
get '/offers' do
content_type :json, 'charset' => 'utf-8'
@offers = Offer.all(order: [:day_of_week_id, :starts_at])
@offers.to_json(
only: [:id, :type, :extra_info],
methods: [:day, :starts, :ends, :tagged],
relationships: {
bar: {
only: [:id, :name, :location]
}
}
)
end
get '/days/:day' do
content_type :json, 'charset' => 'utf-8'
@day = DayOfWeek.all(name: params[:day])
@day.to_json(
only: [:name],
relationships: {
offers: {
only: [:id, :type],
methods: [:day, :starts, :ends, :tagged],
relationships: {
bar: {
only: [:id, :name, :location]
}
}
}
}
)
end