4

I have a method that runs in the Sinatra app scope that checks to see if the request is secure:

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

This works fine, but when I call it from another class that does not share the Sinatra app scope, it attempts to make an Rack Test request, raising an error: wrong number of arguments (0 for 1).

So, is there a way to specify the Sinatra app request explicitly, such as self.request or app.request?

B Seven
  • 44,484
  • 66
  • 240
  • 385
  • 1
    Is your app a classic or module Sinatra app? Can you give an example of calling that method from another class? – joews Nov 19 '13 at 13:10

1 Answers1

1

Calling a request method from another class smells like poor code design, tightly coupling that other class to your app. Where is secure_request? defined? Is it a helper?

I would personally call a method from Sinatra to that other class and pass in the request value, instead of having that other method poll to find out. For example:

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
Phrogz
  • 296,393
  • 112
  • 651
  • 745