Class: Sinatra::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/unit.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_sinatra_unitObject Also known as: new

Sinatra makes new do a bunch of stuff with rack middleware wrappers that are useful if you’re actually running the app, but provides new! for regular instantiation. I’m just re-standardizing names



92
93
94
95
96
97
# File 'lib/sinatra/unit.rb', line 92

def self.new_sinatra_unit
  app = new!
  app.env ||= {}
  app.response = Sinatra::Response.new
  app
end

Instance Method Details

#sessionObject

Normal sinatra session method just returns request.session which passes through to env but I don’t initialize the request object until just before the route lookkup, so I need to skip directly to env.



34
35
36
37
# File 'lib/sinatra/unit.rb', line 34

def session
  raise Sinatra::Unit::SessionsDisabledError unless test_sessions_enabled?
  env['rack.session'] ||= {}
end

#session=(session) ⇒ Object

This doesn’t exist in regular sinatra, but it’s convenient for some tests



40
41
42
43
# File 'lib/sinatra/unit.rb', line 40

def session=(session)
  raise Sinatra::Unit::SessionsDisabledError unless test_sessions_enabled?
  env['rack.session'] = session
end

#test_request(method, path, params = {}) ⇒ Object

test_request comes mostly from the guts of route! in Sinatra::Base



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sinatra/unit.rb', line 46

def test_request(method, path, params={})
  @params = indifferent_params(params)

  @request = Sinatra::Request.new(env)
  @request.path_info = path # sinatra 1.3.3

  @__protected_ivars = instance_variables + ["@__protected_ivars"]

  # routes are stored by uppercase method, but I wanted the test interface
  # to accept :get or 'get' as well as 'GET'
  test_request_internal self.class, method.to_s.upcase
end

#test_request_internal(route_holder, method) ⇒ Object

expects @request and @params to be set Don’t call this directly, but I don’t believe in private methods



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sinatra/unit.rb', line 66

def test_request_internal(route_holder, method)
  raise Sinatra::Unit::UnknownRouteError.new(method,@request.path_info,@params) unless route_holder.respond_to?(:routes)

  if route_holder.routes.has_key? method
    routes = route_holder.routes[method]
    routes.each do |pattern, keys, conditions, block|
      process_route(pattern, keys, conditions) do |*args|
        return catch(:halt) { block[*args] }
      end
    end
  end

  test_request_internal(route_holder.superclass, method)
end

#test_sessions_enabled?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
# File 'lib/sinatra/unit.rb', line 20

def test_sessions_enabled?
  return true if self.class.sessions?

  used_middleware = self.class.instance_variable_get "@middleware"
  used_middleware.each do |middleware|
    return true if middleware.first == Rack::Session::Cookie
  end

  return false
end

#variables(name) ⇒ Object



59
60
61
62
# File 'lib/sinatra/unit.rb', line 59

def variables(name)
  name = "@#{name.to_s}"
  instance_variable_get(name) unless @__protected_ivars.include?(name)
end