class AutoForme::Frameworks::Sinatra

  1. lib/autoforme/frameworks/sinatra.rb
Superclass: Framework

Methods

Public Class

  1. new

Public Class methods

new (*)

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.

[show source]
# File lib/autoforme/frameworks/sinatra.rb, line 38
def initialize(*)
  super
  framework = self
  block = lambda do
    if @autoforme_action = framework.action_for(Request.new(self))
      @autoforme_text = @autoforme_action.handle

      if @autoforme_action.output_type == 'csv'
        response['Content-Type'] = 'text/csv'
        response['Content-Disposition'] = "attachment; filename=#{@autoforme_action.output_filename}"
        @autoforme_text
      elsif @autoforme_action.request.xhr?
        @autoforme_text
      else
        erb "<%= @autoforme_text %>".dup
      end
    else
      pass
    end
  end

  prefix = Regexp.escape(framework.prefix) if framework.prefix
  if ::Sinatra::VERSION < '2'
    prefix = "\\A#{prefix}"
    suffix = "\\z"
  end
  regexp = %r{#{prefix}/([\w:]+)/(\w+)(?:/([\w-]+))?#{suffix}}
  @controller.get regexp, &block
  @controller.post regexp, &block
end