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
Arranquemos un servidor dentro de pry
:
[~/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
Si visitamos la página:
$ curl -v localhost:8080/jkdfkdjg
Esto hace que se alcance 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: endAhora creamos un objeto Rack::Request:
[3] pry(#<HelloWorld>)> req = Rack::Request.new(env) => #<Rack::Request:0x007fbba4ff3298 @env= {"GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/jkdfkdjg", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"::1", "REMOTE_HOST"=>"localhost", "REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"http://localhost:8080/jkdfkdjg", "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:0x007fbba4e74980>, "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"=>"/jkdfkdjg"}>Este objeto Rack::Request tiene métodos para informarnos del Rack::Request:
[4] pry(#<HelloWorld>)> req.get? => true [5] pry(#<HelloWorld>)> req.post? => false [7] pry(#<HelloWorld>)> req.port => 8080 [12] pry(#<HelloWorld>)> req.host() => "localhost" [13] pry(#<HelloWorld>)> req.host_with_port() => "localhost:8080" [15] pry(#<HelloWorld>)> req.path() => "/jkdfkdjg" [18] pry(#<HelloWorld>)> req.url() => "http://localhost:8080/jkdfkdjg" [19] pry(#<HelloWorld>)> req.user_agent => "curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5"