Class: Quince::SinatraMiddleware

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

Instance Method Summary collapse

Constructor Details

#initializeSinatraMiddleware

Returns a new instance of SinatraMiddleware.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/quince_sinatra.rb', line 13

def initialize
  Quince.underlying_app = Class.new(Sinatra::Base) do
    configure :development do
      if Object.const_defined? "Sinatra::Reloader"
        register Sinatra::Reloader
        dont_reload __FILE__
        also_reload $0
      end
    end
    use Rack::JSONBodyParser
    use Rack::Deflater
    set :public_folder, File.join(File.dirname(File.expand_path($0)), "public")
  end
end

Instance Method Details

#create_route_handler(verb:, route:, component: nil, &blck) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/quince_sinatra.rb', line 28

def create_route_handler(verb:, route:, component: nil, &blck)
  meth = case verb
    when :POST, :post
      :post
    when :GET, :get
      :get
    else
      raise "invalid verb"
    end
  handler = component ? ->(_) { component } : blck
  Quince::SinatraMiddleware.send(:routes)[[verb, route]] = handler

  Quince.underlying_app.public_send meth, route do
    handler = Quince::SinatraMiddleware.send(:routes)[[verb, route]]
    Quince.to_html(handler.call(params))
  end
end