Module: Sinatra::RespondTo

Defined in:
lib/sinatra/respond_to.rb

Defined Under Namespace

Modules: Helpers Classes: MissingTemplate, UnhandledFormat

Constant Summary collapse

TEXT_MIME_TYPES =
[:txt, :html, :js, :json, :xml, :rss, :atom, :css, :asm, :c, :cc, :conf,
:csv, :cxx, :diff, :dtd, :f, :f77, :f90, :for, :gemspec, :h, :hh, :htm,
:log, :mathml, :mml, :p, :pas, :pl, :pm, :py, :rake, :rb, :rdf, :rtf, :ru,
:s, :sgm, :sgml, :sh, :svg, :svgz, :text, :wsdl, :xhtml, :xsl, :xslt, :yaml,
:yml, :ics, :png]

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sinatra/respond_to.rb', line 26

def self.registered(app)
  app.helpers RespondTo::Helpers

  app.set :default_charset, 'utf-8'
  app.set :default_content, :html
  app.set :assume_xhr_is_js, true

  # We remove the trailing extension so routes
  # don't have to be of the style
  #
  #   get '/resouce.:format'
  #
  # They can instead be of the style
  #
  #   get '/resource'
  #
  # and the format will automatically be available in <tt>format</tt>
  app.before do

    response['Content-Type'] = '' 
    
    # Let through sinatra image urls in development
    next if self.class.development? && request.path_info =~ %r{/__sinatra__/.*?.png}

    unless options.static? && options.public? && (request.get? || request.head?) && static_file?(request.path_info)
      rpi = request.path_info.sub(%r{\.([^\./]+)$}, '')

      if (not $1) or ($1 and TEXT_MIME_TYPES.include?($1.to_sym))
        request.path_info, ext = rpi, nil
        if ! $1.nil?
          ext = $1
        elsif env['HTTP_ACCEPT'].nil? || env['HTTP_ACCEPT'].empty?
          ext = options.default_content
        end
        if ext
          @mime_types = [ Helpers::mime_type(ext) ]
          format ext
        end
      end
    end
  end

 app.configure :development do |dev|
    dev.error UnhandledFormat do
      content_type :html, :charset => 'utf-8'

      (<<-HTML).gsub(/^ {10}/, '')
      <!DOCTYPE html>
      <html>
      <head>
        <style type="text/css">
        body { text-align:center;font-family:helvetica,arial;font-size:22px;
          color:#888;margin:20px}
        #c {margin:0 auto;width:500px;text-align:left}
        </style>
      </head>
      <body>
        <h2>Sinatra doesn't know this ditty.</h2>
        <img src='/__sinatra__/404.png'>
        <div id="c">
          Try this:
          <pre>#{request.request_method.downcase} '#{request.path_info}' do\n  respond_to do |wants|\n    wants.#{format} { "Hello World" }\n  end\nend</pre>
        </div>
      </body>
      </html>
      HTML
    end

    dev.error MissingTemplate do
      content_type :html, :charset => 'utf-8'
      response.status = request.env['sinatra.error'].code

      engine = request.env['sinatra.error'].message.split('.').last
      engine = 'haml' unless ['haml', 'builder', 'erb'].include? engine

      path = File.basename(request.path_info)
      path = "root" if path.nil? || path.empty?

      format = engine == 'builder' ? 'xml' : 'html'

      layout = case engine
               when 'haml' then "!!!\n%html\n  %body= yield"
               when 'erb' then "<html>\n  <body>\n    <%= yield %>\n  </body>\n</html>"
               when 'builder' then ::Sinatra::VERSION =~ /^1.0/ ? "xml << yield" : "builder do |xml|\n  xml << yield\nend"
               end

      layout = "<small>app.#{format}.#{engine}</small>\n<pre>#{escape_html(layout)}</pre>"

      (<<-HTML).gsub(/^ {10}/, '')
      <!DOCTYPE html>
      <html>
      <head>
        <style type="text/css">
        body { text-align:center;font-family:helvetica,arial;font-size:22px;
          color:#888;margin:20px}
        #c {margin:0 auto;width:500px;text-align:left;}
        small {float:right;clear:both;}
        pre {clear:both;}
        </style>
      </head>
      <body>
        <h2>Sinatra can't find #{request.env['sinatra.error'].message}</h2>
        <img src='/__sinatra__/500.png'>
        <div id="c">
          Try this:<br />
          #{layout}
          <small>#{path}.#{format}.#{engine}</small>
          <pre>Hello World!</pre>
          <small>application.rb</small>
          <pre>#{request.request_method.downcase} '#{request.path_info}' do\n  respond_to do |wants|\n    wants.#{engine == 'builder' ? 'xml' : 'html'} { #{engine} :#{path}#{",\n#{' '*32}layout => :app" if layout} }\n  end\nend</pre>
        </div>
      </body>
      </html>
      HTML
    end

  end

  app.class_eval do
    private
      def accept_list
        @mime_types || Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT'] || '')
      end

      # Changes in 1.0 Sinatra reuse render for layout so we store
      # the original value to tell us if this is an automatic attempt
      # to do a layout call.  If it is, it might fail with Errno::ENOENT
      # and we want to pass that back to sinatra since it isn't a MissingTemplate
      # error
      def render_with_format(*args, &block)
        assumed_layout = args[1] == :layout
        args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
        render_without_format *args, &block
      rescue Errno::ENOENT => e
        raise MissingTemplate, "#{args[1]}.#{args[0]}" unless assumed_layout
        raise e
      end
      alias_method :render_without_format, :render
      alias_method :render, :render_with_format

      if ::Sinatra::VERSION =~ /^0\.9/
        def lookup_layout_with_format(*args)
          args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
          lookup_layout_without_format *args
        end
        alias_method :lookup_layout_without_format, :lookup_layout
        alias_method :lookup_layout, :lookup_layout_with_format
      end
  end
end