-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalmost_sinatra.html
504 lines (368 loc) · 15.5 KB
/
almost_sinatra.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Almost Sinatra in Six Lines of Ruby - A Breakdown</title>
<meta name="generator" content="Slide Show (S9) v2.5.0 on Ruby 2.1.5 (2014-11-13) [i386-mingw32]">
<meta name="author" content="Your Name Here" >
<!-- helper/macro that lets you add (CSS3) gradient using headers
see http://slideshow.rubyforge.org/themes.html
-->
<!-- S6 style sheet links -->
<link rel="stylesheet" href="almost_sinatra.css" media="projection" id="styleProjection">
<link rel="stylesheet" href="s6/screen.css" media="screen" id="styleScreen">
<link rel="stylesheet" href="s6/print.css" media="print">
<!-- S6 JS -->
<script src="s6/jquery.js"></script>
<script src="s6/jquery.slideshow.js"></script>
<script>
$(document).ready( function() {
Slideshow.init();
} );
</script>
<!-- Better Browser Banner for Microsoft Internet Explorer (IE) -->
<!--[if IE]>
<script src="s6/jquery.microsoft.js"></script>
<![endif]-->
</head>
<body>
<div class="layout">
<div id="header"></div>
<div id="footer">
<h1>Your Footer Here</h1>
<h2>Your Subfooter Here</h2>
</div>
</div>
<div class="presentation">
<div class='slide '>
<!-- === begin markdown block ===
generated by markdown/1.2.0 on Ruby 2.1.5 (2014-11-13) [i386-mingw32]
on 2015-05-07 16:36:14 +0200 with Markdown engine kramdown (1.5.0)
using options {}
-->
<!-- _S9SLIDE_ -->
<h1 id="agenda">Agenda</h1>
<p>Almost Sinatra in Six Lines of Ruby - A Breakdown</p>
<ul>
<li>What’s Sinatra?</li>
<li>What’s Almost Sinatra?</li>
<li>Why? Why? Why?</li>
<li>Almost Sinatra - A Breakdown - Line 1/6</li>
<li>What’s Rack?</li>
<li>What’s Tilt?</li>
<li>Almost Sinatra - A Breakdown - Line 2/6</li>
<li>Almost Sinatra - A Breakdown - Line 3/6</li>
<li>Almost Sinatra - A Breakdown - Line 4/6</li>
<li>Almost Sinatra - A Breakdown - Line 5/6</li>
<li>Almost Sinatra - A Breakdown - Line 6/6</li>
<li>The Proof of the Pudding - example.rb</li>
<li>Got Inspired? Build Your Own Microframework</li>
<li>Thanks - Links, Links, Links</li>
<li>Real World Case Study - webservice gem - (Yet Another) HTTP JSON API (Web Service) Builder </li>
<li>Bonus: Fun Almost Sinatra Obfuscation Hacks</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="awesome-viennarb---awesome-awesomeness">Awesome Vienna.rb - Awesome Awesomeness</h1>
<blockquote>
<p>aweseome /ˈɔːsəm/ - adjective -
inspiring, excellent, outstanding, wonderful, cool, terrific, extraordinary</p>
</blockquote>
<h3 id="whats-awesome-x">What’s Awesome <x>?</x></h3>
<p>A collection of awesome links. A single README page on GitHub.
Contributions welcome. Send in a pull request etc.</p>
<ul>
<li>
<p>Awesome Ruby</p>
</li>
<li><a href="https://github.com/planetruby/awesome-events">Awesome Events @ Planet Ruby</a></li>
<li><a href="https://github.com/planetruby/awesome-rubies">Awesome Rubies @ Planet Ruby</a></li>
<li>
<p><a href="https://github.com/planetruby/awesome-activerecord">Awesome ActiveRecord @ Planet Ruby</a></p>
</li>
<li><a href="https://github.com/writekit/awesome-markdown">Awesome Markdown</a></li>
<li>
<p><a href="https://github.com/writekit/awesome-books">Awesome Books</a></p>
</li>
<li><a href="https://github.com/planetopendata/awesome-sqlite">Awesome SQLite @ Planet Open Data</a></li>
<li><a href="https://github.com/planetopendata/awesome-world">Awesome World @ Planet Open Data</a></li>
<li><a href="https://github.com/planetopendata/awesome-football">Awesome Football @ Planet Open Data</a></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 Vienna! Servus Wien!'
end
</code></pre>
<p>Trivia Quiz - How Many Lines of Ruby Code?</p>
<p>(A) 20 Lines
(B) 200 Lines
(C) 2 000 Lines
(D) 20 000 Lines</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-almost-sinatra">What’s Almost Sinatra?</h1>
<p>Sinatra refactored, only six lines now. A hack by Konstantin Haase.</p>
<p><a href="https://github.com/rkh/almost-sinatra/blob/master/almost_sinatra.rb">almost_sinatra.rb</a>:</p>
<pre><code>%w.rack tilt date INT TERM..map{|l|trap(l){$r.stop}rescue require l};$u=Date;$z=($u.new.year + 145).abs;puts "== Almost Sinatra/No Version has taken the stage on #$z for development with backup from Webrick"
$n=Module.new{extend Rack;a,D,S,q=Rack::Builder.new,Object.method(:define_method),/@@ *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m
%w[get post put delete].map{|m|D.(m){|u,&b|a.map(u){run->(e){[200,{"Content-Type"=>"text/html"},[a.instance_eval(&b)]]}}}}
Tilt.mappings.map{|k,v|D.(k){|n,*o|$t||=(h=$u._jisx0301("hash, please");File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n=="#{n}"?n:$t[n.to_s]}.render(a,o[0].try(:[],:locals)||{})}}
%w[set enable disable configure helpers use register].map{|m|D.(m){|*_,&b|b.try :[]}};END{Rack::Handler.get("webrick").run(a,Port:$z){|s|$r=s}}
%w[params session].map{|m|D.(m){q.send m}};a.use Rack::Session::Cookie;a.use Rack::Lock;D.(:before){|&b|a.use Rack::Config,&b};before{|e|q=Rack::Request.new e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="why-why-why">Why? Why? Why?</h1>
<blockquote>
<p>Until programmers stop acting like obfuscation is morally hazardous,
they’re not artists, just kids who don’t want their food to touch.
– _why</p>
</blockquote>
<pre><code>$ sloccount alomst_sinatra.rb
Total Physical Source Lines of Code (SLOC) = 8
Development Effort Estimate, Person-Years (Person-Months) = 0.00 (0.02)
(Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
Schedule Estimate, Years (Months) = 0.04 (0.51)
(Basic COCOMO model, Months = 2.5 * (person-months**0.38))
Estimated Average Number of Developers (Effort/Schedule) = 0.03
Total Estimated Cost to Develop = $ 170
(average salary = $56,286/year, overhead = 2.40).
</code></pre>
<p>Performance. It’s just amazing. No code is faster than no code.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="almost-sinatra---a-breakdown---line-16">Almost Sinatra - A Breakdown - Line 1/6</h1>
<p>(Like) Sinatra = Rack + Tilt</p>
<p>Line 1:</p>
<pre><code>%w.rack tilt date INT TERM..map{|l|trap(l){$r.stop}rescue require l};$u=Date;$z=($u.new.year + 145).abs;puts "== Almost Sinatra/No Version has taken the stage on #$z for development with backup from Webrick"
</code></pre>
<p>Breakdown:</p>
<pre><code>require 'rack'
require 'tilt'
trap( 'INT' ) { $server.stop } # rename $r to $server
trap( 'TERM' ) { $server.stop }
$port = 4567 # rename $z to $port
puts "== Almost Sinatra has taken the stage on #{$port} for development with backup from Webrick"
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-rack">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>
<p><img src="i/racksite.png" alt="" /></p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-tilt">What’s tilt?</h1>
<p>Tilt offers a standard “generic” interface for template engines.</p>
<p>Let’s check-up what formats and template engines tilt includes out-of-the-box:</p>
<pre><code>require 'tilt'
Tilt.mappings.each do |ext, engines|
puts "#{ext.ljust(12)} : #{engines.inspect}"
end
</code></pre>
<p>Will result in:</p>
<pre><code>str : [Tilt::StringTemplate]
erb : [Tilt::ErubisTemplate, Tilt::ERBTemplate]
rhtml : [Tilt::ErubisTemplate, Tilt::ERBTemplate]
erubis : [Tilt::ErubisTemplate]
etn : [Tilt::EtanniTemplate]
etanni : [Tilt::EtanniTemplate]
haml : [Tilt::HamlTemplate]
sass : [Tilt::SassTemplate]
scss : [Tilt::ScssTemplate]
less : [Tilt::LessTemplate]
rcsv : [Tilt::CSVTemplate]
coffee : [Tilt::CoffeeScriptTemplate]
nokogiri : [Tilt::NokogiriTemplate]
builder : [Tilt::BuilderTemplate]
mab : [Tilt::MarkabyTemplate]
liquid : [Tilt::LiquidTemplate]
radius : [Tilt::RadiusTemplate]
markdown : [Tilt::RedcarpetTemplate, Tilt::RedcarpetTemplate::Redcarpet2, Tilt::RedcarpetTemplate::Redcarpet1, Tilt::RDiscountTemplate, Tilt::BlueClothTemplate, Tilt::KramdownTemplate, Tilt::MarukuTemplate]
mkd : [Tilt::RedcarpetTemplate, Tilt::RedcarpetTemplate::Redcarpet2, Tilt::RedcarpetTemplate::Redcarpet1, Tilt::RDiscountTemplate, Tilt::BlueClothTemplate, Tilt::KramdownTemplate, Tilt::MarukuTemplate]
md : [Tilt::RedcarpetTemplate, Tilt::RedcarpetTemplate::Redcarpet2, Tilt::RedcarpetTemplate::Redcarpet1, Tilt::RDiscountTemplate, Tilt::BlueClothTemplate, Tilt::KramdownTemplate, Tilt::MarukuTemplate]
textile : [Tilt::RedClothTemplate]
rdoc : [Tilt::RDocTemplate]
wiki : [Tilt::WikiClothTemplate, Tilt::CreoleTemplate]
creole : [Tilt::CreoleTemplate]
mediawiki : [Tilt::WikiClothTemplate]
mw : [Tilt::WikiClothTemplate]
yajl : [Tilt::YajlTemplate]
ad : [Tilt::AsciidoctorTemplate]
adoc : [Tilt::AsciidoctorTemplate]
asciidoc : [Tilt::AsciidoctorTemplate]
html : [Tilt::PlainTemplate]
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="almost-sinatra---a-breakdown---line-26">Almost Sinatra - A Breakdown - Line 2/6</h1>
<p>Line 2:</p>
<pre><code>$n=Module.new{extend Rack;a,D,S,q=Rack::Builder.new,Object.method(:define_method),/@@ *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m
</code></pre>
<p>Breakdown:</p>
<pre><code>$n = Module.new do
app = Rack::Builder.new # rename a to app
req = nil # rename q to req
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="almost-sinatra---a-breakdown---line-36">Almost Sinatra - A Breakdown - Line 3/6</h1>
<p>Line 3:</p>
<pre><code>%w[get post put delete].map{|m|D.(m){|u,&b|a.map(u){run->(e){[200,{"Content-Type"=>"text/html"},[a.instance_eval(&b)]]}}}}
</code></pre>
<p>Breakdown:</p>
<pre><code> ['get','post','put','delete'].each do |method|
define_method method do |path, &block|
app.map( path ) do
run ->(env){ [200, {'Content-Type'=>'text/html'}, [app.instance_eval( &block )]]}
end
end
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="almost-sinatra---a-breakdown---line-46">Almost Sinatra - A Breakdown - Line 4/6</h1>
<p>Line 4:</p>
<pre><code>Tilt.mappings.map{|k,v|D.(k){|n,*o|$t||=(h=$u._jisx0301("hash, please");File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n=="#{n}"?n:$t[n.to_s]}.render(a,o[0].try(:[],:locals)||{})}}
</code></pre>
<p>Breakdown:</p>
<pre><code> Tilt.mappings.each do |ext, engines| # rename k to ext and v to engines
define_method ext do |text, *args| # rename n to text and o to args
template = engines[0].new(*args) do
text
end
locals = (args[0].respond_to?(:[]) ? args[0][:locals] : nil) || {} # was o[0].try(:[],:locals)||{}
template.render( app, locals )
end
end
</code></pre>
<p>Commentary: Almost Sinatra will define a method for every format so you can use, for example:</p>
<pre><code>markdown "Strong emphasis, aka bold, with **asterisks** or __underscores__."
</code></pre>
<p>or</p>
<pre><code>erb "Hello <%= name %>!", locals: { name: params['name'] }
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="almost-sinatra---a-breakdown---line-56">Almost Sinatra - A Breakdown - Line 5/6</h1>
<p>Line 5:</p>
<pre><code>%w[set enable disable configure helpers use register].map{|m|D.(m){|*_,&b|b.try :[]}};END{Rack::Handler.get("webrick").run(a,Port:$z){|s|$r=s}}
</code></pre>
<p>Breakdown:</p>
<pre><code> # was END { ... }; change to run! method
define_method 'run!' do
Rack::Handler.get('webrick').run( app, Port:$port ) {|server| $server=server }
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="almost-sinatra---a-breakdown---line-66">Almost Sinatra - A Breakdown - Line 6/6</h1>
<p>Line 6:</p>
<pre><code>%w[params session].map{|m|D.(m){q.send m}};a.use Rack::Session::Cookie;a.use Rack::Lock;D.(:before){|&b|a.use Rack::Config,&b};before{|e|q=Rack::Request.new e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
</code></pre>
<p>Breakdown:</p>
<pre><code> ['params','session'].each do |method|
define_method method do
req.send method
end
end
app.use Rack::Session::Cookie
app.use Rack::Lock
app.use Rack::Config do |env|
req = Rack::Request.new( env )
end
end # Module.new
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="the-proof-of-the-pudding----examplerb">The Proof of the Pudding - example.rb</h1>
<p><code>example.rb</code>:</p>
<pre><code>require_relative 'almost_sinatra'
include $n # include "anonymous" Almost Sinatra DSL module
get '/hello' do
erb "Hello <%= name %>!", locals: { name: params['name'] }
end
get '/' do
markdown <<EOS
## Welcome to Almost Sinatra
A six line ruby hack by Konstantin Haase.
Try:
- [Say hello!](/hello?name=Nancy)
Powered by Almost Sinatra (#{Time.now})
EOS
end
run!
</code></pre>
<p>Use</p>
<pre><code>$ ruby ./example.rb
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="got-inspired-build-your-own-microframework">Got Inspired? Build Your Own Microframework</h1>
<ul>
<li>New York, New York</li>
<li>Nancy</li>
<li>Rum</li>
<li>Cuba</li>
<li>Roda</li>
<li>and many more</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="real-world-case-study---webservice-gem---yet-another-http-json-api-web-service-builder">Real World Case Study - webservice gem - (Yet Another) HTTP JSON API (Web Service) Builder</h1>
<p>Micro “framework” for building HTTP JSON APIs (about 100 lines of code).
Example:</p>
<pre><code>get '/beer/:key' do
Beer.find_by_key!( params[ :key ] )
end
get '/brewery/:key' do
Brewery.find_by_key!( params[ :key ] )
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="thanks---links-links-links">Thanks - Links, Links, Links</h1>
<ul>
<li>Article :: <a href="https://robm.me.uk/2013/12/13/decoding-almost-sinatra.html">Decoding “Almost Sinatra</a>, by Rob Miller</li>
<li>Article :: <a href="http://www.sitepoint.com/code-safari-almost-sinatra-almost-readable/">Code Safari: Almost Sinatra, Almost Readable</a>, by Xavier Shay</li>
<li>Article :: <a href="http://planetruby.github.io/gems/tilt.html">tilt gem - let’s build (yet another) micro web framework in less than 33 lines of code</a>, by Gerald Bauer</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="bonus-fun-almost-sinatra-obfuscation-hacks">Bonus: Fun Almost Sinatra Obfuscation Hacks</h1>
<p>“Calculate” the Sinatra Port 4567:</p>
<pre><code>Date.new.year + 145).abs # Date.new.year always returns -4712, the default value for years
# => 4567
</code></pre>
<p>“Get” empty Hash (e.g. <code>{}</code>):</p>
<pre><code>Date._jisx0301("hash, please")
# => {}
</code></pre>
<!-- === end markdown block === -->
</div>
</div><!-- presentation -->
</body>
</html>