Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Web Applications with Ruby David Padilla @dabit

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Image from: ezilon.com

Slide 6

Slide 6 text

Image from: ezilon.com

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Manzanillo, Mexico

Slide 9

Slide 9 text

June 4 - 6 2014

Slide 10

Slide 10 text

Web applications with Ruby (not Rails)

Slide 11

Slide 11 text

I invented MVC on ASP Classic

Slide 12

Slide 12 text

Not really

Slide 13

Slide 13 text

<% set conn=Server.CreateObject("ADODB.Connection") conn.Open "northwind" %> Super App

Page 1

<% ' Some code here that connects to the DB and ' displays stuff %> page1.asp

Slide 14

Slide 14 text

<% set conn=Server.CreateObject("ADODB.Connection") conn.Open "northwind" %> Super App

Another Page

<% ' Some code here that connects to the DB and ' displays stuff %> AnotherPage.asp

Slide 15

Slide 15 text

Index.asp Super App <% page = Request.QueryString("page") Select Case page Case "page1" %> <% Case "AnotherPage" %> <% End Select %>

Slide 16

Slide 16 text

2000

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

MVC

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

http://therantygirl.files.wordpress.com/2012/07/happy-cat.jpg

Slide 21

Slide 21 text

Mid Programmer Life Crisis

Slide 22

Slide 22 text

Programming Web applications with Rails is BORING

Slide 23

Slide 23 text

Building websites with Rails is not programming

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Most of us don’t know what’s going on outside of Rails

Slide 29

Slide 29 text

We used to have all these problems

Slide 30

Slide 30 text

We used to actually write SQL

Slide 31

Slide 31 text

Bring programming back

Slide 32

Slide 32 text

I invented rhp

Slide 33

Slide 33 text

<% $greeting = "Hello World" %> <% require "includes/stylesheets.rb" %> RHP Rocks! <%= Stylesheets.render() %>

<%= $greeting %>

RHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to RHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual.

cgi/index.erb

Slide 34

Slide 34 text

<% $greeting = "Hello World" %> <% require "includes/stylesheets.rb" %> RHP Rocks! <%= Stylesheets.render() %>

<%= $greeting %>

RHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to RHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual.

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

I invented rhp (not really)

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Rack is cool as Bowties

Slide 39

Slide 39 text

Web applications with Ruby (not Rails)

Slide 40

Slide 40 text

Rack

Slide 41

Slide 41 text

Web Server Web Application A hash with request headers that we call ENV

Slide 42

Slide 42 text

Web Server Web Application A hash with request headers that we call ENV respond_to(:call)

Slide 43

Slide 43 text

Web Server Web Application A hash with request headers that we call ENV

Slide 44

Slide 44 text

[http_code, headers, body] Integer Hash Responds to :each

Slide 45

Slide 45 text

Web Server Web Application [http_code, headers, body]

Slide 46

Slide 46 text

run -> (env) { puts env [200, {}, []] } config.ru

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

run -> (env) { puts env [200, {}, []] } config.ru

Slide 50

Slide 50 text

config.ru run -> (env) { [200, headers, [body]] } body = "

Hello World

" headers = { "Content-Length" => body.length.to_s }

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

config.ru run -> (env) { [200, headers, [body]] } body = "

Hello World

" headers = { "Content-Length" => body.length.to_s }

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Router

Slide 56

Slide 56 text

/posts PostController #index

Slide 57

Slide 57 text

config.ru run -> (env) { [200, headers, [body]] } body = "

Hello World

" headers = { "Content-Length" => body.length.to_s }

Slide 58

Slide 58 text

config.ru $: << '.' require 'app' run App.new

Slide 59

Slide 59 text

app.rb class App def call(env) body = "

Hello World

" headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end

Slide 60

Slide 60 text

controllers/root_controller.rb class RootController def show body = "

Hello World

" headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end

Slide 61

Slide 61 text

app.rb class App def call(env) body = "

Hello World

" headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end RootController.new.show end end

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

app.rb class App def call(env) RootController.new.show end end

Slide 64

Slide 64 text

app.rb class App def call(env) path = env["REQUEST_PATH"] case path when %r{^/$} RootController.new.show else [404, { "Content-Length" => "9"}, ["Not found"]] end end end

Slide 65

Slide 65 text

app.rb class App def call(env) path = env["REQUEST_PATH"] case path when %r{^/$} RootController.new.show else [404, { "Content-Length" => "9"}, ["Not found"]] end end end

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

controllers/root_controller.rb class RootController def show body = "

Hello World

" headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

config.ru $: << '.' require 'app' require 'controllers/root_controller' run App.new require 'haml'

Slide 72

Slide 72 text

views/root/show.html.haml %html %body %h1 Hello World

Slide 73

Slide 73 text

controllers/root_controller.rb class RootController def show body = "

Hello World

" headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end

Slide 74

Slide 74 text

controllers/root_controller.rb class RootController # ... end def render(view_path) template = File.open(view_path, 'r').read Haml::Engine.new(template).render end

Slide 75

Slide 75 text

controllers/root_controller.rb class RootController def show headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end def render # ... end end body = render('views/root/show.html.haml') body = "

Hello World

"

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

views/root/show.html.haml %html %body %h1 Hello World

Slide 78

Slide 78 text

views/layout/application.html.haml %html %body = yield

Slide 79

Slide 79 text

controllers/root_controller.rb class RootController def render(view_path) template = File.open(view_path, 'r').read Haml::Engine.new(template).render end def render_with_layout(view_path) layout = File.open('views/layout/application.html.haml', 'r').re Haml::Engine.new(layout).render do render(view_path) end end end end

Slide 80

Slide 80 text

def render_with_layout(view_path) layout = File.open #... Haml::Engine.new(layout).render do render(view_path) end end

Slide 81

Slide 81 text

controllers/root_controller.rb class RootController def show headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end def render # ... end end body = render('views/root/show.html.haml') body = render_with_layout('views/root/show.html.haml')

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

controllers/root_controller.rb class RootController def show body = render_with_layout('views/root/show.html.haml') headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end def render(view_path) template = File.open(view_path, 'r').read Haml::Engine.new(template).render end def render_with_layout(view_path) layout = File.open('views/layout/application.html.haml', 'r').re Haml::Engine.new(layout).render do render(view_path) end end end class RootController < Controller def show body = render_with_layout('views/root/show.html.haml') headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end

Slide 84

Slide 84 text

config.ru $: << '.' require 'app' require 'controllers/root_controller' run App.new require 'haml' require 'controller'

Slide 85

Slide 85 text

controller.rb class Controller def render(view_path) template = load_template_file(view_path) Haml::Engine.new(template).render end def render_with_layout(view_path) layout_template = load_template_file(layout) Haml::Engine.new(layout_template).render do render_template(view_path) end end end

Slide 86

Slide 86 text

views/root/show.html.haml %h1 Hello World %h1 Hello #{@name}

Slide 87

Slide 87 text

controller.rb class Controller end attr_accessor :env def initialize(env) self.env = env end def params Rack::Utils.parse_nested_query(env["QUERY_STRING"]) end

Slide 88

Slide 88 text

app.rb class App def call(env) path = env["REQUEST_PATH"] case path when %r{^/$} RootController.new(env).show else [404, { "Content-Length" => "9"}, ["Not found"]] end end end

Slide 89

Slide 89 text

@available_on_the_view = true binding = self HAML::Engine.new(template).render(binding)

Slide 90

Slide 90 text

controller.rb class Controller def render(view_path) template = load_template_file(view_path) Haml::Engine.new(template).render end def render_with_layout(view_path) layout_template = load_template_file('... ') Haml::Engine.new(layout_template).render do render(view_path) end end end class Controller def render(view_path, context = self) template = load_template_file(view_path) Haml::Engine.new(template).render(context) end def render_with_layout(view_path, context = self) layout_template = load_template_file('... ') Haml::Engine.new(layout_template).render(context) do render(view_path, context) end end end `

Slide 91

Slide 91 text

controllers/root_controller.rb class RootController < Controller def show body = render_with_layout('views/root/show.html.haml') headers = { "Content-Length" => body.length.to_s } [200, headers, [body]] end end @name = params["name"] || "World"

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Models

Slide 96

Slide 96 text

ActiveRecord::Base

Slide 97

Slide 97 text

require 'active_record' class User < ActiveRecord::Base #... end model/user.rb

Slide 98

Slide 98 text

Sequel

Slide 99

Slide 99 text

model/user.rb require 'sequel' DB = Sequel.sqlite class User def self.all DB[:users].to_a end end

Slide 100

Slide 100 text

pg mysql

Slide 101

Slide 101 text

model/user.rb require 'pg' DB = PG.connect( dbname: 'test', host: 'localhost') class User def self.all result = DB.exec "SELECT * FROM users" fields = result.fields result.collect do |row| hash = {} row.each_with_index do |value, i| hash[fields[i]] = value end end end end

Slide 102

Slide 102 text

Once last piece of advice

Slide 103

Slide 103 text

Don’t actually do this

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Logging Caching Database Pooling Cookies Sessions

Slide 107

Slide 107 text

Read code

Slide 108

Slide 108 text

Try to understand how things work

Slide 109

Slide 109 text

You should start writing your own framework!

Slide 110

Slide 110 text

<3 Rails Core Team

Slide 111

Slide 111 text

github.com/dabit/wawr

Slide 112

Slide 112 text

Disclaimer: I do NOT actually think that writing web applications with Rails is boring

Slide 113

Slide 113 text

@dabit [email protected] Thank You!