-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjruby_web_apps.pdf.html
979 lines (691 loc) · 22.6 KB
/
jruby_web_apps.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>(J)Ruby in Action - Web Apps & Services w/ Rails n Jetty</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.1.1 on Ruby 1.9.2 (2011-07-09) [i386-mingw32]
on 2013-10-07 16:20:26 +0200 with Markdown engine kramdown (1.2.0)
using options {}
-->
<!-- _S9SLIDE_ -->
<h1 id="ice-breakers">Ice Breakers</h1>
<ul>
<li>
<p>Friends of Ruby (Rails)? - Anyone?</p>
</li>
<li>
<p>Friends of Functional Programming (Haskell, Yesod) - Anyone?</p>
</li>
<li>
<p>Friends of Scala, Play! - Anyone?</p>
</li>
<li>
<p>Friends of Server-Side JavaScript (Node.js) - Anyone?</p>
</li>
<li>
<p>Friends of Dart - Anyone?</p>
</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="factories-factories-factories">Factories, Factories, Factories</h1>
<p>Java Enterprise Architecture </p>
<ul>
<li>Factories, Factories, Factories</li>
<li>Over-engineering, Cult of Complexity</li>
<li>The COBOL of the 21st Century (Java is Old technology, No Longer Hot Java or the New New Thing => Innovation Happens Elsewhere)</li>
<li>There is only Java, Java, Java - 1,000,0000,000 % Java, The End of History, Java Rules the World Hybris</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="why-ruby">Why Ruby?</h1>
<ul>
<li>Code Blocks (Lambda Expressions) - 20 years in Ruby! Coming to Java 8 in 2014</li>
<li>Open Classes</li>
<li>Mixins</li>
<li>Everything is a Object</li>
<li>Meta Programming (e.g. Ruby Code Creates Code at Runtime on Demand)</li>
<li>List, Tree, Map Data Structures in Ruby</li>
<li>Templates in Ruby</li>
<li>Culture - Programmer Happiness (Productivity, Keep it Simple), Innovation</li>
</ul>
<!--- finally -->
<ul>
<li>Open, Free World (Not a Product and Trademark of Oracle, Inc.)</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="keep-it-simple---web-apps-case-study">Keep it Simple - Web Apps Case Study</h1>
<ol>
<li>No Enterprise Java Application Server
<ul>
<li>Use Embedded Jetty Library to Run Container-Less </li>
</ul>
</li>
<li>No Enterprise Java Database Server
<ul>
<li>Use Embedded SQLite Library</li>
</ul>
</li>
<li>No Enterprise Java Application Framework
<ul>
<li>Use Embedded Ruby Library</li>
</ul>
</li>
<li>No Enterprise Java IDE
<ul>
<li>Use Command Line, Programmer’s Editor</li>
</ul>
</li>
</ol>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="case-study-how-to-install-container-less-jetty-web-server">Case Study: How to Install Container-Less Jetty Web Server</h1>
<p>Step 1: Copy <code>jetty-webapp-7.x.jar</code> (~ 1 Meg)</p>
<p>Step 2: There is no step 2. </p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="think-different---whats-container-less">Think Different - What’s Container-Less?</h1>
<p><img src="i/jetty-container-less.png" alt="" /></p>
<p>Container-Less? Run your web application as a plain old Java process.</p>
<p>Why:</p>
<ul>
<li>Simpler Development</li>
<li>Simpler Testing</li>
<li>Simpler Packaging</li>
<li>Simpler Deployment</li>
</ul>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="a-simple-web-app-using-jetty">A Simple Web App using Jetty</h1>
<p>The simplest possible Jetty server:</p>
<pre><code>import org.eclipse.jetty.server.Server;
public class SimpleServer
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
server.start();
server.join();
}
}
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="a-simple-web-app-using-jetty--cont">A Simple Web App using Jetty (Cont.)</h1>
<pre><code>import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class AppServer
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar( "./links.war");
server.setHandler(webapp);
server.start();
server.join();
}
}
</code></pre>
<h2 id="compile">Compile</h2>
<pre><code>javac -cp .;./jetty-webapp-7.x.jar AppServer.java
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="lets-build-a-link-bookmarking-and-voting-web-app">Let’s Build a Link Bookmarking and Voting Web App</h1>
<ul>
<li>Post links</li>
<li>Vote links up or down</li>
<li>Sort links by the newest or hot</li>
</ul>
<p><img src="i/build-reddit-in-ruby.png" alt="" /></p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="create-a-web-app-using-ruby-on-rails">Create a Web App using Ruby on Rails</h1>
<h2 id="create-web-app-structure">Create Web App Structure</h2>
<pre><code>$ rails links
</code></pre>
<h2 id="create-sqlite-schema-dbmigratecreatedbrb">Create SQLite Schema (<code>db/migrate/create_db.rb</code>)</h2>
<pre><code>create_table :links do |t|
t.string :title, :null => false
t.string :url, :null => false
t.integer :points, :default => 0
t.timestamps # note: macro adds created_at, updated_at
end
</code></pre>
<h2 id="create-sqlite-db">Create SQLite DB</h2>
<pre><code>$ rake db:setup # note: rake is ruby make (simple build tool)
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="model--view--controller-mvc">Model / View / Controller (MVC)</h1>
<h2 id="model-modelslinkrb">Model (<code>models/link.rb</code>)</h2>
<pre><code>class Link < ActiveRecord::Base
# self.table_name = 'links'
attr_accessor :score
def recalc_score
time_elapsed = (Time.now - self.created_at) / 36000 # in hours
self.score = ((self.points-1) / (time_elapsed+2)**1.8)
end
def self.hot
self.all.each { |rec| rec.recalc_score }.sort { |l,r| l.score <=> r.score }.reverse
end
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="model--view--controller-mvc-1">Model / View / Controller (MVC)</h1>
<h2 id="controller-controllerslinkscontrollerrb">Controller (<code>controllers/links_controller.rb</code>)</h2>
<pre><code>class LinksController < ApplicationController
# GET /
def index
@links = Link.order( 'created_at desc' ).all
end
# GET /hot
def hot
@links = Link.hot
render :index
end
# POST /
def create
l = Link.new( params[:link] )
l.save!
redirect_to :back
end
# PUT /:id/vote/:type
def vote
l = Link.find( params[:id] )
l.points += params[:type].to_i
l.save!
redirect_to :back
end
end
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="model--view--controller-mvc-2">Model / View / Controller (MVC)</h1>
<h2 id="view-viewslinksindexhtmlerb">View (<code>views/links/index.html.erb</code>)</h2>
<pre><code><table id='links'>
<% @links.each do |l| %>
<tr>
<td class='points'>
<%= form_for l, :url => vote_link_path( l, :type => '1' ), :method => 'PUT' do |f| %>
<%= f.submit '+1' %>
<% end %>
<%= l.points %>
<%= form_for l, :url => vote_link_path( l, :type => '-1' ), :method => 'PUT' do |f| %>
<%= f.submit '-1' %>
<% end %>
</td>
<td><span class='title'><%= link_to l.title, l.url %></span>
<span class='host'>(<%= l.url_host %>)</span>
<span class='created-at'>posted <%= time_ago_in_words(l.created_at) %> ago</span>
</td>
</tr>
<% end %>
</table>
<div id='post-link'>
<%= form_for :link, :url => links_path() do |f| %>
<%= f.text_field :title, :placeholder => 'Title' %>
<%= f.text_field :url, :placeholder => 'URL' %>
<%= f.submit 'Save Link' %>
<% end %>
</div>
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="zip-up-and-run-it">Zip Up and Run It</h1>
<h2 id="zip-up-as-java-web-archive-linkswar">Zip Up as Java Web Archive (links.war)</h2>
<pre><code>$ rake war # warble
</code></pre>
<h2 id="run-it">Run it</h2>
<pre><code>$ java -cp .;./jetty-webapp-7.x.jar AppServer
</code></pre>
<h2 id="surprise-">Surprise ??</h2>
<p>=> One Plain Old Java Process - Embedded Jetty, Embedded SQLite, Embedded Ruby</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="case-study-how-to-install-embedded-ruby">Case Study: How to Install Embedded Ruby</h1>
<p>Step 1: Copy <code>jruby-complete-1.7.x.jar</code></p>
<p>Step 2: There is no step 2.</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="java-web-archive-war-directory-structure">Java Web Archive (<code>.war</code>) Directory Structure</h1>
<pre><code>links.war
|_ images
| | _<empty>
|_ stylesheets
| |_ application.css
|_ javascripts
| | _<empty>
|_ WEB-INF
|_ app
| |_ controllers
| | |_ links_controller.rb
| |_ models
| | |_ link.rb
| |_ views
| |_ layouts
| | |_ application.html.erb
| |_ links
| |_ index.html.erb
|_ config
| |_ database.yml
| |_ routes.rb
|_ db
| |_ links.sqlite3
|_ lib
|_ jruby-complete-1.7.x.jar
|_ jruby-rack-1.x.jar
|_ gems.jar
</code></pre>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="whats-missing-in-java-whats-wrong-with-java">What’s missing in Java? What’s wrong with Java?</h1>
<p>Anyone?</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="simple-data-structures-maps-arrays">Simple Data Structures (Maps, Arrays)</h1>
<p>Example:</p>
<pre><code>{ customer:
{ id: '12345',
first_name: 'Carlos',
last_name: 'Lafata',
address:
[
{ typ: 'home',
line1: 'Burgring 11',
city: 'Wien',
zip_code: '1010' },
{ typ: 'work',
line1: 'Nestroyplatz 12',
city: 'Wien',
zip_code: '1020' }
] }
}
</code></pre>
<h2 id="quiz--built-into-what-modern-languages">Quiz: Built into what modern languages?</h2>
<p><code>[ X ]</code> JavaScript</p>
<p><code>[ X ]</code> Ruby</p>
<p><code>[ X ]</code> Scala</p>
<p><code>[ ]</code> ~~Java~~</p>
<p><code>[ ]</code> ~~COBOL~~</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="simple-code-blocks-higher-order-functions-functions-as-first-class-types">Simple Code Blocks (Higher Order Functions, Functions as First Class Types)</h1>
<p>Example:</p>
<pre><code>// Assign Function to Variable
var greet = function() { document.write( 'Welcome. Benvenuti. Willkommen.' ); }
// Call Function Stored in Variable
greet();
// Function as Function Parameter
function say( what )
{
what();
what();
what();
}
// Pass Function Stored in Variable to Function
say( greet );
</code></pre>
<h2 id="quiz-built-into-what-modern-languages">Quiz: Built into what modern languages?</h2>
<p><code>[ X ]</code> JavaScript</p>
<p><code>[ X ]</code> Ruby</p>
<p><code>[ X ]</code> Scala</p>
<p><code>[ ]</code> ~~Java~~</p>
<p><code>[ ]</code> ~~COBOL~~</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="part-ii">Part II</h1>
<p>Building Web Services (HTTP APIs) with Ruby (and Sinatra)</p>
<h2 id="agenda">Agenda</h2>
<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="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>
</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="whats-viennarb-ruby-user-group-vienna">What’s vienna.rb? Ruby User Group Vienna</h1>
<p>Next meetup: Fri, October 11th @ Sektor5 </p>
<p>Talks (*)</p>
<p>Jakob Sommerhuber - sponsor talk</p>
<p>Martin Schürrer - Erlang/OTP in production for highly-available, scalable systems</p>
<p>Markus Prinz - How to improve your code</p>
<p>Gerald Bauer - working with Sinatra</p>
<p>Kathrin Folkendt - ‘Chapter one’ (lightning talk on getting started with Rails, and building her first web app)</p>
<p>(*) preliminary program</p>
</div>
<div class='slide '>
<!-- _S9SLIDE_ -->
<h1 id="thats-it-thanks">That’s it. Thanks.</h1>
<p>1) Don’t put yourself in the Java ghetto (or Java rules the world hybris)</p>
<p>=> Learn new concepts or languages </p>
<p>2) Web architecture is more than Java enterprise architecture</p>
<p>=> Learn HTML, JS, CSS, HTTP (REST), SQL/NoSQL, etc.</p>
<h3 id="questions-comments">Questions? Comments?</h3>
<!-- === end markdown block === -->
</div>
</div> <!-- presentation -->
</body>
</html>