-
Notifications
You must be signed in to change notification settings - Fork 2
/
sinatra_http_api.pdf.html
521 lines (357 loc) · 11.8 KB
/
sinatra_http_api.pdf.html
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Building Web Services (HTTP APIs) with Ruby (and Sinatra)</title>
<style>
html, body { margin: 0; padding: 0; }
body { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; }
a:link, a:visited { color: black; }
h1 { font-size: 30pt; }
h2 { font-size: 28pt; }
h3 { font-size: 25pt; }
p, li, dt, dd, td, th { font-size: 18pt; }
pre { font-size: 14pt; }
pre.small { font-size: 11pt; }
pre.code {
background-color: azure;
padding: 5px;
}
ul { list-style-type: square; }
.center { text-align: center; }
.slide { page-break-after: always;
min-height: 100mm;
padding: 40px;
border: 1px dotted black;
/*
background: -moz-linear-gradient( top, maroon, red);
*/
}
pre {
padding: 4px 4px 4px 4px;
border-top: #bbb 1px solid;
border-bottom: #bbb 1px solid;
background: #f3f3f3;
}
/*
for princexml (CSS3 paged media support)
@page { size: A4 landscape }
*/
</style>
</head>
<body>
<div class="presentation">
<div class='slide '>
<!-- === begin markdown block =====================================================
generated by markdown 1.0.0 on Ruby 1.9.2 (2011-07-09) [i386-mingw32]
on 2013-09-05 16:44:38 +0200 with Markdown engine kramdown (1.2.0)
using options { !to be done! }
-->
<!-- _S9SLIDE_ -->
<h1 id="agenda">Agenda</h1>
<ul>
<li>What’s Sinatra?</li>
<li>Let a Thousand Sinatra Clones Bloom</li>
<li>Why Sinatra? Goodies</li>
<li>Example Web Service (HTTP API) - Routes</li>
<li>Sinatra in Action - <code>get '/beer/:key'</code></li>
<li>What’s JSON? </li>
<li>What’s JSONP?</li>
<li>Serializers - From Ruby Objects to JavaScript Objects</li>
<li>Appendix: Sinatra Styles - Classic or Modern (Modular)</li>
<li>Appendix: Database Connection Management</li>
<li>Appendix: Sinatra Books</li>
<li>Appendix: What’s Rack?</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-sinatra">What’s Sinatra?</h1>
<p>Simple (yet powerful and flexible) micro webframework.</p>
<pre><code>require 'sinatra'
get '/' do
'Hallo Wien!'
end
</code></pre>
<p>Sinatra itself <a href="https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb">less than 2000 lines of code</a>. </p>
<p>Installation. Type in your terminal (shell):</p>
<pre><code>$ gem install sinatra
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-sinatra-continued">What’s Sinatra? (Continued)</h1>
<p>Example - <code>hallo.rb</code>:</p>
<pre><code>require 'sinatra'
get '/' do
'Hallo Wien!'
end
</code></pre>
<p>Run script (server):</p>
<pre><code>$ ruby hallo.rb
>> Sinatra has taken the stage...
>> Listening on 0.0.0.0:4567, CTRL+C to stop
</code></pre>
<p>Open browser:</p>
<p><img src="i/hallosinatra.png" alt="" /></p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="let-a-thousand-sinatra-clones-bloom">Let a Thousand Sinatra Clones Bloom</h1>
<p>Micro Frameworks Inspired by Sinatra</p>
<p>Express.js (in Server-Side JavaScript w/ Node.js):</p>
<pre><code>var express = require( 'express' );
var app = express();
app.get( '/', function( req, res ) {
res.send( 'Hallo Wien!' );
});
app.listen( 4567 );
</code></pre>
<p>Scotty (in Haskell):</p>
<pre><code>import Web.Scotty
main :: IO ()
main = scotty 4567 $ do
get "/" $ text "Hallo Wien!"
</code></pre>
<p>Dancer (Perl), Fitzgerald (PHP), Ratpack (Groovy),
Zappa (CoffeeScript), Mercury (Lua), Frank (F#), Nancy (C#/.NET),
Bogart (C), Flask (Python), and <a href="http://en.wikipedia.org/wiki/Sinatra_(software)#Frameworks_inspired_by_Sinatra">many more</a>.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="why-sinatra--goodies">Why Sinatra? Goodies</h1>
<p>1) Single file scripts</p>
<p>2) Easy to package up into a gem. Example:</p>
<pre><code>$ gem install beerdb # Yes, the beerdb includes a Sinatra app.
</code></pre>
<p>3) Lets you build command line tools. Example:</p>
<pre><code>$ beerdb serve # Startup web service (HTTP API).
</code></pre>
<p>4) Lets you mount app inside app (including Rails). Example:</p>
<pre><code>mount BeerDb::Server, :at => '/api/v1'
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="example-web-service-http-api---routes">Example Web Service (HTTP API) - Routes</h1>
<p>Lets build a beer and brewery API.</p>
<p>Get beer by key <code>/beer/:key</code>. Examples:</p>
<ul>
<li><code>/beer/guinness</code></li>
<li><code>/beer/murphysred</code></li>
<li><code>/beer/brooklynlager</code></li>
<li><code>/beer/ottakringerhelles</code></li>
</ul>
<p>Get brewery by key <code>/brewery/:key</code>. Examples:</p>
<ul>
<li><code>/brewery/guinness</code></li>
<li><code>/brewery/fullers</code></li>
<li><code>/brewery/brooklyn</code></li>
<li><code>/brewery/ottakringer</code></li>
</ul>
<p>Bonus:</p>
<p>Get random beer <code>/beer/rand</code> and random brewery <code>/brewery/rand</code>.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="sinatra-in-action---get-beerkey">Sinatra in Action - <code>get '/beer/:key'</code></h1>
<p><code>beerdb/server.rb</code>:</p>
<pre><code>get '/beer/:key' do |key|
beer = Beer.find_by_key!( key )
json_or_jsonp( beer.as_json )
end
get '/brewery/:key' do |key|
brewery = Brewery.find_by_key!( key )
json_or_jsonp( brewery.as_json )
end
</code></pre>
<p>That’s it.</p>
<p>Bonus:</p>
<pre><code>get '/beer/:key' do |key|
if ['r', 'rnd', 'rand', 'random'].include?( key )
beer = Beer.rnd.first
else
beer = Beer.find_by_key!( key )
end
json_or_jsonp( beer.as_json )
end
get '/brewery/:key' do |key|
if ['r', 'rnd', 'rand', 'random'].include?( key )
brewery = Brewery.rnd.first
else
brewery = Brewery.find_by_key!( key )
end
json_or_jsonp( brewery.as_json )
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-json">What’s JSON?</h1>
<p>JSON = JavaScript Object Notation</p>
<p>Example - <code>GET /beer/ottakringerhelles</code>:</p>
<pre><code>{
key: "ottakringerhelles",
title: "Ottakringer Helles",
synonyms: "16er Blech|16er Hüs'n",
abv: "5.2",
og: "11.8",
tags: [ "lager" ],
brewery: {
key: "ottakringer",
title: "Ottakringer Brauerei"
},
country: {
key: "at",
title: "Austria"
}
}
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-jsonp">What’s JSONP?</h1>
<p>JSONP = JSON with Padding. Why?</p>
<p>Call Home Restriction. Cross-Domain Browser Requests Get Blocked. </p>
<p>Hack: Wrap JSON into a JavaScript function/callback
e.g. <code>functionCallback( <json_data_here> )</code>
and serve as plain old JavaScript.</p>
<p>Example - <code>Content-Type: application/json</code>:</p>
<pre><code>{
key: "ottakringerhelles",
title: "Ottakringer Helles",
synonyms: "16er Blech|16er Hüs'n",
abv: "5.2",
...
}
</code></pre>
<p>becomes <code>Content-Type: application/javascript</code>:</p>
<pre><code>functionCallback(
{
key: "ottakringerhelles",
title: "Ottakringer Helles",
synonyms: "16er Blech|16er Hüs'n",
abv: "5.2",
...
}
);
</code></pre>
<p>Bonus: Little Sinatra helper for JSON or JSONP response (depending on callback parameter).</p>
<pre><code>def json_or_jsonp( json )
callback = params.delete('callback')
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="serializers---from-ruby-objects-in-memory-to-javascript-object-in-text">Serializers - From Ruby Objects (in Memory) to JavaScript Object (in Text)</h1>
<p>JSON built into Ruby 2.0 as a standard library. Example:</p>
<pre><code>require 'json'
hash =
{
key: "ottakringerhelles",
title: "Ottakringer Helles"
}
</code></pre>
<h3 id="jsongenerate">1) <code>JSON.generate</code></h3>
<pre><code>puts JSON.generate( hash )
>> {"key":"ottakringerhelles","title":"Ottakringer Helles"}
</code></pre>
<h3 id="tojson">2) <code>#to_json</code></h3>
<pre><code>puts hash.to_json
>> {"key":"ottakringerhelles","title":"Ottakringer Helles"}
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="serializers---from-ruby-objects-in-memory-to-javascript-object-in-text-continued">Serializers - From Ruby Objects (in Memory) to JavaScript Object (in Text) Continued</h1>
<p>Serializers for your Models. Example:</p>
<pre><code>class BeerSerializer
def initialize( beer )
@beer = beer
end
attr_reader :beer
def as_json
data = { key: beer.key,
title: beer.title,
synonyms: beer.synonyms,
abv: beer.abv,
...
}
data.to_json
end
end # class BeerSerializer
</code></pre>
<p>And add <code>as_json</code> to your Model. Example:</p>
<pre><code>class Beer < ActiveRecord::Base
def as_json_v2( opts={} )
BeerSerializer.new( self ).as_json
end
end # class Beer
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="thats-it-thanks">That’s it. Thanks.</h1>
<h3 id="questions-comments">Questions? Comments?</h3>
<p>Learn more about Sinatra @ <a href="http://sinatrarb.com"><code>sinatrarb.com</code></a></p>
<p>Learn more about the open beer ‘n’ brewery database (<code>beer.db</code>) @ <a href="https://github.com/openbeer"><code>github.com/openbeer</code></a></p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="appendix-sinatra-styles---classic-or-modern-modular">Appendix: Sinatra Styles - Classic or Modern (Modular)</h1>
<pre><code>require 'sinatra'
get '/' do
'Hallo Wien!'
end
</code></pre>
<p>vs.</p>
<pre><code>require 'sinatra/base'
class Server < Sinatra::Base
get '/' do
'Hallo Wien!'
end
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="appendix-tip---database-connection-management">Appendix: Tip - Database Connection Management</h1>
<p>Sinatra will NOT auto-magically close your database connection
after every request. It’s up to you.</p>
<p>1) Use the <code>ConnectionManagement</code> middleware. Example:</p>
<pre><code>Server.use ActiveRecord::ConnectionAdapters::ConnectionManagement
</code></pre>
<p>2) Or do it yourself. Example:</p>
<pre><code>Server.after do
ActiveRecord::Base.connection.close
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="appendix-sinatra-books">Appendix: Sinatra Books</h1>
<p><img src="i/sinatra_up_and_running.gif" alt="" /> Sinatra: Up and Running by Alan Harris, Konstantin Haase;
November 2011, O’Reilly, 122 Pages</p>
<p><img src="i/jump_start_sinatra.gif" alt="" /> Jump Start Sinatra by Darren Jones;
January 2013, SitePoint, 150 Pages</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="appendix-whats-rack">Appendix: What’s Rack?</h1>
<p>Lets you mix ‘n’ match servers and apps.</p>
<p>Lets you stack apps inside apps inside apps inside apps inside apps.</p>
<p>Good News: A Sinatra app is a Rack app.</p>
<p>Learn more about Rack @ <a href="http://rack.github.io"><code>rack.github.io</code></a>.</p>
<!-- === end markdown block ===================================================== -->
</div>
</div> <!-- presentation -->
</body>
</html>