How to distinguish a Sinatra request object from a Rack Test request method?

I have a method that works in the scope of a Sinatra application that checks if the request is safe:

secure_request?
  request.env[ 'HTTPS' ] == 'on'
end

It works fine, but when I call it from another class that does not use the area Sinatra application, it tries to perform a query to check stands, creating a mistake wrong number of arguments (0 for 1).

So, is there a way to explicitly specify a request for a Sinatra application, for example, self.requestor app.request?

+4
source share
1 answer

Calling a method requestfrom another class smells like a bad code design, closely linking this class to another class. Where is indicated secure_request?? Is this an assistant?

Sinatra , , , . :

class OtherClass
  def some_method( opts={} )
    if opts[:secure]
      # …
    else
      # …
    end
  end
end

class MyApp < Sinatra::Application
  helpers do
    secure_request?
      request.env[ 'HTTPS' ] == 'on'
    end
  end
  get '/' do
    @otherclass.some_method( secure: secure_request? )
  end
end
+1

All Articles