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

Philly.rb gem day: Rack

Philly.rb gem day: Rack

Brief intro to Rack for Ruby gem day at Philly.rb

Ernesto Tagwerker

April 18, 2017
Tweet

More Decks by Ernesto Tagwerker

Other Decks in Technology

Transcript

  1. # config.ru run Proc.new { |env| [ # HTTP Response

    Code '200', # Headers Hash {'Content-Type' => 'text/html'}, # Response Body ['rack & roll'] ] }
  2. # Terminal (Server) $ rackup [2017-04-16 20:10:34] INFO WEBrick 1.3.1

    [2017-04-16 20:10:34] INFO ruby 2.3.3 (2016-11-21) [x86_64-darwin16] [2017-04-16 20:10:34] INFO WEBrick::HTTPServer#start: pid=60003 port=9292
  3. # Terminal (Client) - GET / $ curl -v http://localhost:9292

    < HTTP/1.1 200 OK < Content-Type: text/html < Transfer-Encoding: chunked < Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21) < Date: Mon, 17 Apr 2017 00:19:39 GMT < Connection: Keep-Alive < * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact rack & roll%
  4. # Terminal (Client) - GET /pepe $ curl -v http://localhost:9292/pepe

    < HTTP/1.1 200 OK < Content-Type: text/html < Transfer-Encoding: chunked < Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21) < Date: Mon, 17 Apr 2017 00:25:37 GMT < Connection: Keep-Alive < * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact rack & roll%
  5. # Gemfile source "https://rubygems.org" ruby "2.3.3" gem "rack" group :development

    do gem "thin" end group :production do gem "puma" end
  6. # Terminal (Server) $ thin start Using rack adapter Thin

    web server (v1.7.0 codename Dunder Mifflin) Maximum connections set to 1024 Listening on 0.0.0.0:3000, CTRL+C to stop
  7. # config.ru run Proc.new { |env| [ # HTTP Response

    Code nil, # Headers Hash {'Content-Type' => 'text/html'}, # Response Body ['rack & roll'] ] }
  8. # config.ru run Proc.new { |env| [ # HTTP Response

    Code '200', # Headers Hash {'Content-Type' => 'text/html'}, # Response Body ['rack & roll'] ] }
  9. # Terminal (Client) - HEAD / $ curl -I http://localhost:9292

    HTTP/1.1 500 Internal Server Error Content-Type: text/html; charset=ISO-8859-1 Transfer-Encoding: chunked Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21) Date: Mon, 17 Apr 2017 00:32:36 GMT Connection: close
  10. # Terminal (Server) ::1 - - [16/Apr/2017:20:32:36 -0400] "HEAD /

    HTTP/1.1" 200 - 0.0004 [2017-04-16 20:32:36] ERROR Rack::Lint::LintError: Response body was given for HEAD request, but should be empty gems/rack-2.0.1/lib/rack/lint.rb:20:in `assert' gems/rack-2.0.1/lib/rack/lint.rb:688:in `verify_content_length' gems/rack-2.0.1/lib/rack/lint.rb:716:in `each' gems/rack-2.0.1/lib/rack/body_proxy.rb:36:in `each' gems/rack-2.0.1/lib/rack/chunked.rb:23:in `each'
  11. # config.ru run Proc.new { |env| # this is a

    hash req = Rack::Request.new(env) [ # HTTP Response Code 200, # Headers Hash {'Content-Type' => 'text/html'}, # Response Body req.head? ? '' : ['rack & roll'] ] }
  12. # Terminal (Client) $ curl -vI http://localhost:9292 * Rebuilt URL

    to: http://localhost:9292/ * Trying ::1... * TCP_NODELAY set * Connected to localhost (::1) port 9292 (#0) > HEAD / HTTP/1.1 > Host: localhost:9292 > User-Agent: curl/7.51.0 > Accept: */* > < HTTP/1.1 200 OK HTTP/1.1 200 OK
  13. # Gemfile source "https://rubygems.org" ruby "2.3.3" gem "rack" gem "rack-tracker"

    group :development do gem "thin" end group :production do gem "puma" end
  14. # config.ru app = Rack::Builder.app do use Rack::Tracker do handler

    :google_analytics, { tracker: 'U-1234-5' } end run Proc.new { |env| # this is a hash req = Rack::Request.new(env) [ 200, {'Content-Type' => 'text/html'}, req.head? ? '' : [ '<html><head></head><body>rack & roll</ body></html>'] ] } end run app
  15. # Terminal (Client) $ curl -v http://localhost:3000 <html><head><script type="text/javascript"> if(typeof

    ga === 'undefined') { (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o) , m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a, m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'U-1234-5', {}); } ga('send', 'pageview', window.location.pathname + window.location.search); </script> * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact </head><body>rack & roll</body></html>%
  16. # rack/random_gif class RandomGif KEYWORDS = ['cat', 'dog', 'panda'].freeze API_KEY

    = "dc6zaTOxFJmzC" def initialize(app) @keyword = KEYWORDS.shuffle.first @app = app end def call(env) status, headers, body = @app.call(env) [status, headers, random_bodies(body)] end private def img_tag giphy = Giphy.random(@keyword) source_url = giphy.image_original_url.to_s "<br/><img src='#{source_url}'/><br/>" end