Upgrade to Pro — share decks privately, control downloads, hide ads and more …

eurucamp 2012: Almost Sinatra

eurucamp 2012: Almost Sinatra

A fun code analysis.

Konstantin Haase

August 18, 2012
Tweet

More Decks by Konstantin Haase

Other Decks in Technology

Transcript

  1. Sinatra in 8 Lines
    How to do crazy stuff with ruby
    %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new{extend  Rack;a,D,S,$p,q,Applica)on=Builder.new,Object.method(:define_method),/@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,4567,a
    %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={};File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n.to_s==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{Handler.get("webrick").run(a,Port:$p){|s|$r=s}}
    %w[params  session].map{|m|D.(m){q.send  m}};a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on  #$p  for  development  with  backup  from  Webrick"
    Saturday, August 18, 12

    View full-size slide

  2. Konstantin Travis CI
    @konstan(nhaase  aka  rkh
    Saturday, August 18, 12

    View full-size slide

  3. Sinatra
    Rack,  Tilt,  Rubinius,  ...
    Saturday, August 18, 12

    View full-size slide

  4. “second to K&R, the most
    lagom technical book
    I’ve read.”
    Peter  Cooper  (Ruby  Inside)
    discount code: AUTHD
    50% off ebook ($6.50)
    40% OFF PRINT
    Saturday, August 18, 12

    View full-size slide

  5. Prepare for strange
    code slides
    also,  no  memes  this  4me
    Saturday, August 18, 12

    View full-size slide

  6. Obfuscation
    Saturday, August 18, 12

    View full-size slide

  7. Until programmers stop acting like
    obfuscation is morally hazardous,
    they’re not artists, just kids who
    don’t want their food to touch.
    why  the  lucky  s(ff
    Saturday, August 18, 12

    View full-size slide

  8. !?!
    Saturday, August 18, 12

    View full-size slide

  9. !?! # => false
    Saturday, August 18, 12

    View full-size slide

  10. ?:??::??
    Saturday, August 18, 12

    View full-size slide

  11. ?:??::??
    Saturday, August 18, 12

    View full-size slide

  12. ?:??::?? # => “:”
    Saturday, August 18, 12

    View full-size slide

  13. eval \
    '51966'+[46,1935634292,32,49,54].pack('clC3')
    Saturday, August 18, 12

    View full-size slide

  14. eval \
    '51966'+[46,1935634292,32,49,54].pack('clC3')
    # => “cafe”
    Saturday, August 18, 12

    View full-size slide

  15. eval \
    '51966'+['.',1935634292, ' ', '1', '6'].pack('ala3')
    # => “cafe”
    Saturday, August 18, 12

    View full-size slide

  16. eval \
    '51966'+['.', 'to_s', ' ', '1', '6'].pack('A4')
    # => “cafe”
    Saturday, August 18, 12

    View full-size slide

  17. 51966.to_s(16)
    # => “cafe”
    Saturday, August 18, 12

    View full-size slide

  18. 0xcaFE
    # => “cafe”
    Saturday, August 18, 12

    View full-size slide

  19. enter the heredocs
    Saturday, August 18, 12

    View full-size slide

  20. Saturday, August 18, 12

    View full-size slide

  21. Saturday, August 18, 12

    View full-size slide

  22. Distraction
    Saturday, August 18, 12

    View full-size slide

  23. Saturday, August 18, 12

    View full-size slide

  24. Saturday, August 18, 12

    View full-size slide

  25. github.com / rkh /
    almost-sinatra
    more  popular  than  a  pair  of  socks
    Saturday, August 18, 12

    View full-size slide

  26. As little code
    as possible
    just  eight  lines
    Saturday, August 18, 12

    View full-size slide

  27. Obfuscation was
    never the goal
    just  a  by-­‐product
    Saturday, August 18, 12

    View full-size slide

  28. %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new{extend  Rack;a,D,S,$p,q,Applica)on=Builder.new,Object.method(:define_method),/@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,4567,a
    %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={};File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n.to_s==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{Handler.get("webrick").run(a,Port:$p){|s|$r=s}}
    %w[params  session].map{|m|D.(m){q.send  m}};a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on  #$p  for  development  with  backup  from  Webrick"
    the code
    Saturday, August 18, 12

    View full-size slide

  29. What works?
    Saturday, August 18, 12

    View full-size slide

  30. Saturday, August 18, 12

    View full-size slide

  31. Saturday, August 18, 12

    View full-size slide

  32. Saturday, August 18, 12

    View full-size slide

  33. Saturday, August 18, 12

    View full-size slide

  34. Simplify and
    compress
    Saturday, August 18, 12

    View full-size slide

  35. Saturday, August 18, 12

    View full-size slide

  36. Saturday, August 18, 12

    View full-size slide

  37. Saturday, August 18, 12

    View full-size slide

  38. Saturday, August 18, 12

    View full-size slide

  39. Saturday, August 18, 12

    View full-size slide

  40. Saturday, August 18, 12

    View full-size slide

  41. Saturday, August 18, 12

    View full-size slide

  42. Saturday, August 18, 12

    View full-size slide

  43. %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new{extend  Rack;a,D,S,$p,q,Applica)on=Builder.new,Object.method(:define_method),/@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,4567,a
    %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={};File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n.to_s==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{Handler.get("webrick").run(a,Port:$p){|s|$r=s}}
    %w[params  session].map{|m|D.(m){q.send  m}};a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on  #$p  for  development  with  backup  from  Webrick"
    the code
    Saturday, August 18, 12

    View full-size slide

  44. Fake it ‘till you
    make it
    Saturday, August 18, 12

    View full-size slide

  45. %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new{
    extend  Rack;
    a,D,S,$p,q,Applica)on
    =Builder.new,
    Object.method(:define_method),/@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,4567,a
    %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={};File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n.to_s==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{Handler.get("webrick").run(a,Port:$p){|s|$r=s}}
    %w[params  session].map{|m|D.(m){q.send  m}};
    a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on  #$p  for  development  with  backup  from  Webrick"
    Saturday, August 18, 12

    View full-size slide

  46. %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new{extend  Rack;a,
    D,S,$p,q,Applica)on
    =Builder.new,
    Object.method(:define_method)
    ,/@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,4567,a
    %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={};File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n.to_s==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{Handler.get("webrick").run(a,Port:$p){|s|$r=s}}
    %w[
    params  session].map{|m|
    D.(m){q.send  m}};a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};
    before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on  #$p  for  development  with  backup  from  Webrick"
    Saturday, August 18, 12

    View full-size slide

  47. %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new{extend  Rack;a,D,
    S,$p,q,Applica)on
    =Builder.new,Object.method(:define_method),
    /@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,4567,a
    %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={};File.read(caller[0][/^[^:]+/])
    .scan(S){|a,b|h[a]=b};h);
    v[0].new(*o){n.to_s==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{Handler.get("webrick").run(a,Port:$p){|s|$r=s}}
    %w[params  session].map{|m|D.(m){q.send  m}};a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on  #$p  for  development  with  backup  from  Webrick"
    Saturday, August 18, 12

    View full-size slide

  48. %w.rack  )lt  backports  INT  TERM..map{|l|trap(l){$r.stop}rescue  require  l}
    $n=Sinatra=Module.new
    {extend  Rack;
    a,D,S,
    $p,q,Applica)on
    =Builder.new,
    Object.method(:define_method),/@@  *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,
    4567,a
    %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={};File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].
    new(*o){n.to_s==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{Handler.get("webrick").
    run(a,Port:$p){|s|$r=s}}
    %w[params  session].map{|m|D.(m){q.send  m}};a.use  Session::Cookie;a.use  Lock
    D.(:before){|&b|a.use  Rack::Config,&b};before{|e|q=Request.new  e;q.params.dup.map{|k,v|params[k.to_sym]=v}}}
    puts  "==  almost  #$n/No  Version  has  taken  the  stage  on
    #$p  for  development  with  backup  from  Webrick"
    Saturday, August 18, 12

    View full-size slide

  49. It’s all about fun!
    Saturday, August 18, 12

    View full-size slide

  50. “From now on, I’ll start
    quoting myself.”
    Konstan(n  Haase
    Saturday, August 18, 12

    View full-size slide

  51. “If your app does not run with
    Almost Sinatra, please open
    a Sinatra issue.”
    Konstan(n  Haase
    Saturday, August 18, 12

    View full-size slide

  52. “Versions are to Software
    what Subversion is to Git.”
    Konstan(n  Haase
    Saturday, August 18, 12

    View full-size slide

  53. “don't include tests. tests just bloat
    the code base. just commit, the users
    will complain if you break anything.”
    Konstan(n  Haase
    Saturday, August 18, 12

    View full-size slide

  54. BEARS or Horses
    this  slide  is  for  James  Coglan
    Saturday, August 18, 12

    View full-size slide

  55. Thank You
    @konstan(nhaase
    Saturday, August 18, 12

    View full-size slide