Search code examples
rubysinatrarackpry

How to use pry to access a Rack middleware instance?


I'm new to using Pry, and try as I might, I can't find the right commands to access a rack middleware instance that's being run with a Sinatra app. I've set binding.pry in the part of the app where I have an error, but the problem is with a helper that's returning no results, and it gets the results from info put in the env by the middleware. I tried cding into Rack::GeoIPCity, but it didn't give me access to any instance info.

So, I can check the locals, the helper, and the env - how do I get to the middleware from the Sinatra app's code?

e.g.

class App < Sinatra::Base
  # inside the Sinatra app
  use Rack::GeoIPCity, :db => File.expand_path( File.join(File.dirname(__FILE__), "assets/GeoLiteCity.dat"))

  def geo_city_info
    h = {}
    env.select{|x| x =~ /^GEOIP/ }.each {|k,v|
      h[k.split("GEOIP_").last.downcase] = v
    }
    OpenStruct.new( h )
  end

  get "/home" do
    geo = geo_city_info
    binding.pry
  end

end

so at the point where binding.pry is called I can easily access geo, env (which is a helper provided by Sinatra to Rack's env), and I could easily add a binding inside the geo_city_info if I wanted. But, I can't access the Rack::GeoIPCity from those bindings. I'm hoping there's a command that could help me navigate to that, because it must be part of the loaded application code.

Of course, I can open up the code files for the middleware and put in the pry binding (as I install all dependent libs into /vendor using Bundler), but it'd be much better if I knew how to do this without resorting to that.


Solution

  • Not sure what you are trying to do, but you can

    • Monkey patch the middleware class to use pry inside

    • connect directly from the source code (what I understood you dont want)

    • play around with the "use" method from Sinatra to access the middleware you want from the middleware stack

    That are my ideas, hope it helps

    AFTER YOUR EDITED:

    Check out this, you can access the @middleware instance variable to get the used middlewares