class AutoForme::Frameworks::Sinatra

Public Class Methods

new(*) click to toggle source

Add get and post routes when creating the framework. These routes can potentially match other routes, but in that case use pass to try the next route.

Calls superclass method AutoForme::Framework::new
   # File lib/autoforme/frameworks/sinatra.rb
39 def initialize(*)
40   super
41   framework = self
42   block = lambda do
43     if @autoforme_action = framework.action_for(Request.new(self))
44       @autoforme_text = @autoforme_action.handle
45 
46       if @autoforme_action.output_type == 'csv'
47         response['Content-Type'] = 'text/csv'
48         response['Content-Disposition'] = "attachment; filename=#{@autoforme_action.output_filename}"
49         @autoforme_text
50       elsif @autoforme_action.request.xhr?
51         @autoforme_text
52       else
53         erb "<%= @autoforme_text %>".dup
54       end
55     else
56       pass
57     end
58   end
59 
60   prefix = Regexp.escape(framework.prefix) if framework.prefix
61   if ::Sinatra::VERSION < '2'
62     prefix = "\\A#{prefix}"
63     suffix = "\\z"
64   end
65   regexp = %r{#{prefix}/([\w:]+)/(\w+)(?:/([\w-]+))?#{suffix}}
66   @controller.get regexp, &block
67   @controller.post regexp, &block
68 end