-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rb
237 lines (173 loc) · 5.1 KB
/
test.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
class BeerDb::Model::Note < ActiveRecord::Base
def as_json( opts={} )
puts " !!!! as_json_v2"
data = {
beer: { title: beer.title,
key: beer.key },
user: { name: user.name,
key: user.key },
rating: rating,
comments: comments,
place: place,
created_at: created_at,
updated_at: updated_at
}
data
end
end
get '/seed' do
## add some seed data
puts "GET /seed"
alois = User.create!( key: 'alois',
name: 'Alois Mustermann',
email: '[email protected]' )
pp alois
{ status: 'ok' }
end
get '/seed2' do
puts "GET /seed2"
## add random note
user = User.first
beer = Beer.rnd
attribs = {
user_id: user.id,
beer_id: beer.id,
rating: 3,
place: 'Place Here'
}
note = Note.new
note.update_attributes!( attribs )
pp note
{ status: 'ok' }
end
get '/notes/(l|latest)' do # get latest tasting notes (w/ ratings)
Note.order( 'updated_at DESC' ).limit(10).all
end
get '/notes/(h|hot)' do # get latest tasting notes (w/ ratings)
# fix: use log algo for "hotness" - for now same as latest
Note.order( 'updated_at DESC' ).limit(10).all
end
get '/notes/(t|top)' do
Note.order( 'rating DESC, updated_at DESC' ).limit(10).all
end
get '/notes/:key' do
### todo: move to /u/:key/notes - why? why not??
# assume it's a user key
user = User.find_by!( key: params['key'] )
notes = Note.order( 'rating DESC, updated_at DESC' ).where( user_id: user.id ).all
notes
end
post '/hello' do
puts "POST /hello"
pp params
## NameError: uninitialized constant BeerDbService::CONTENT_TYPE
## NoMethodError: undefined method `headers' for #<Rack::Request:0x55dd498>
## pp request.headers
pp request
puts request['Content-Type']
## puts request.headers['Content-Type']
data = request.body.read
puts "request.body.read(1):"
puts data
puts "request.body.read(2):"
puts request.body.read ## note: can read body only once!!!
puts "JSON.parse:"
pp JSON.parse( data )
{ status: 'ok' }
end
post '/notes' do
puts "POST /notes"
pp params
pp JSON.parse( request.body.read )
user = User.find_by!( key: params['user'] )
beer = Beer.find_by!( key: params['beer'] )
rating = params['rating'].to_i
place = params['place'] # assumes for now a string or nil / pass through as is
attribs = {
user_id: user.id,
beer_id: beer.id,
rating: rating,
place: place
}
note = Note.new
note.update_attributes!( attribs )
{ status: 'ok' }
end
get '/drinks/:key' do |key|
puts " handle GET /drinks/:key"
if ['l', 'latest'].include?( key )
# get latest +1 drinks
## todo: order by drunk_at??
drinks = Drink.order( 'updated_at DESC' ).limit(10).all
else
### todo: move to /u/:key/drinks ??
# assume it's a user key
user = User.find_by_key!( key )
drinks = Drink.order( 'updated_at DESC' ).where( user_id: user.id ).all
end
data = []
drinks.each do |drink|
data << {
beer: { title: drink.beer.title,
key: drink.beer.key },
user: { name: drink.user.name,
key: drink.user.key },
place: drink.place,
drunk_at: drink.drunk_at,
created_at: drink.created_at,
updated_at: drink.updated_at
}
end
data
end
get '/drinks' do
if params[:method] == 'post'
puts " handle GET /drinks?method=post"
user = User.find_by_key!( params[:user] )
beer = Beer.find_by_key!( params[:beer] )
place = params[:place] # assumes for now a string or nil / pass through as is
attribs = {
user_id: user.id,
beer_id: beer.id,
place: place
}
drink = Drink.new
drink.update_attributes!( attribs )
end
{ status: 'ok' }
end
## todo: add support for beer of the day, of the week, of the month, of the year?
## /beer/day|d | /beer/month|m | beer/year|y ??
##
## add table daily_beer, weekly_beer, etc. ??
get '/beer/:key' do |key|
if ['r', 'rnd', 'rand', 'random'].include?( key )
# special key for random beer
# Note: use .first (otherwise will get ActiveRelation not Model)
beer = Beer.rnd.first
else
beer = Beer.find_by_key!( key )
end
end
get '/brewery/:key' do |key|
if ['r', 'rnd', 'rand', 'random'].include?( key )
# special key for random brewery
# Note: use .first (otherwise will get ActiveRelation not Model)
brewery = Brewery.rnd.first
else
brewery = Brewery.find_by_key!( key )
end
end
=begin
curl -i -X POST -H "Content-Type: application/json" -d "{\"key\":\"alois\"}" http://localhost:4567/hello
HTTP
Note: curl in windows needs double quotes (escape quotes in quotes) in json data
check: https://x-team.com/blog/how-to-create-a-ruby-api-with-sinatra/
https://github.com/mwpastore/sinja
payload = JSON.parse(request.body.read)
The Rack contrib module Rack::PostBodyContentTypeParser will do this for you as well.
The Rack parameter hash is populated by deserializing the JSON data
provided in the request body when
the Content-Type is application/json.
See github.com/rack/rack-contrib
=end