Subsecciones

Conexión con Parámetros

Partimos del mismo código fuente que en la sección anterior:

[~/local/src/ruby/sinatra/rack/rack-debugging]$ cat hello.rb 
require 'rack'
require 'pry-debugger'

class HelloWorld
  def call env
    binding.pry
    [200, {"Content-Type" => "text/plain"}, ["Hello world"]]
  end
end
Arrancamos el servidor:
[~/local/src/ruby/sinatra/rack/rack-debugging]$ pry
[1] pry(main)> require './hello'
=> true
[2] pry(main)> Rack::Handler::WEBrick::run HelloWorld.new
[2013-09-23 13:10:42] INFO  WEBrick 1.3.1
[2013-09-23 13:10:42] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin11.4.2]
[2013-09-23 13:10:42] INFO  WEBrick::HTTPServer#start: pid=10395 port=8080
En el cliente tendríamos:
$ curl -v 'localhost:8080?a=1&b=2&c=3'
comienza produciendo esta salida:
* About to connect() to localhost port 8080 (#0)
*   Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
> GET /?a=1&b=2&c=3 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:8080
> Accept: */*
>

En la ventana del servidor se produce el break:

From: /Users/casiano/local/src/ruby/sinatra/rack/rack-debugging/hello.rb @ line 6 HelloWorld#call:

    5: def call env
 => 6:   binding.pry
    7:   [200, {"Content-Type" => "text/plain"}, ["Hello world"]]
    8: end

Rack::Request.new

Creamos un objeto Rack::Request:

[1] pry(#<HelloWorld>)> req = Rack::Request.new env
=> #<Rack::Request:0x007fafd27946c0
 @env=
  {"GATEWAY_INTERFACE"=>"CGI/1.1",
   "PATH_INFO"=>"/",
   "QUERY_STRING"=>"a=1&b=2&c=3",
   "REMOTE_ADDR"=>"::1",
   "REMOTE_HOST"=>"localhost",
   "REQUEST_METHOD"=>"GET",
   "REQUEST_URI"=>"http://localhost:8080/?a=1&b=2&c=3",
   "SCRIPT_NAME"=>"",
   "SERVER_NAME"=>"localhost",
   "SERVER_PORT"=>"8080",
   "SERVER_PROTOCOL"=>"HTTP/1.1",
   "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/1.9.3/2013-02-22)",
   "HTTP_USER_AGENT"=>
    "curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5",
   "HTTP_HOST"=>"localhost:8080",
   "HTTP_ACCEPT"=>"*/*",
   "rack.version"=>[1, 2],
   "rack.input"=>#<StringIO:0x007fafd26bbbe0>,
   "rack.errors"=>#<IO:<STDERR>>,
   "rack.multithread"=>true,
   "rack.multiprocess"=>false,
   "rack.run_once"=>false,
   "rack.url_scheme"=>"http",
   "HTTP_VERSION"=>"HTTP/1.1",
   "REQUEST_PATH"=>"/"}>

req.params

Ahora podemos interrogarle:
[2] pry(#<HelloWorld>)> req.params
=> {"a"=>"1", "b"=>"2", "c"=>"3"}

Indexación de los objetos Rack::Request

Recordemos que la URL visitada fué: localhost:8080?a=1&b=2&c=3
[3] pry(#<HelloWorld>)> req["a"]
=> "1"
[4] pry(#<HelloWorld>)> req["b"]
=> "2"
[5] pry(#<HelloWorld>)> req["c"]
=> "3"

req.path

[6] pry(#<HelloWorld>)> req.path
=> "/"
[7] pry(#<HelloWorld>)> req.fullpath
=> "/?a=1&b=2&c=3"
[9] pry(#<HelloWorld>)> req.path_info
=> "/"
[10] pry(#<HelloWorld>)> req.query_string
=> "a=1&b=2&c=3"

req.url

[11] pry(#<HelloWorld>)> req.url
=> "http://localhost:8080/?a=1&b=2&c=3"

req.values

[12] pry(#<HelloWorld>)> req.values_at("a")
=> ["1"]
[13] pry(#<HelloWorld>)> req.values_at("a", "b")
=> ["1", "2"]
[14] pry(#<HelloWorld>)> req.values_at("a", "b", "c")
=> ["1", "2", "3"]

[16] pry(#<HelloWorld>)> continue
localhost - - [23/Sep/2013:13:10:49 WEST] "GET /?a=1&b=2&c=3 HTTP/1.1" 200 11
- -> /?a=1&b=2&c=3

$ curl -v 'localhost:8080?a=1&b=2&c=3'
* About to connect() to localhost port 8080 (#0)
*   Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
> GET /?a=1&b=2&c=3 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK 
< Content-Type: text/plain
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-02-22)
< Date: Mon, 23 Sep 2013 12:35:37 GMT
< Content-Length: 11
< Connection: Keep-Alive
< 
* Connection #0 to host localhost left intact
* Closing connection #0
Hello world

Casiano Rodriguez León 2015-01-07