Module: Sinatra::RespondTo::Helpers
- Defined in:
- lib/sinatra/respond_to.rb
Class Method Summary collapse
-
.included(klass) ⇒ Object
Patch the content_type function to remember the set type This helps cut down on time in the format helper so it doesn’t have to do a reverse lookup on the header.
Instance Method Summary collapse
- #charset(val = nil) ⇒ Object
- #format(val = nil) ⇒ Object
- #respond_to {|wants| ... } ⇒ Object
-
#static_file?(path) ⇒ Boolean
This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory.
Class Method Details
.included(klass) ⇒ Object
Patch the content_type function to remember the set type This helps cut down on time in the format helper so it doesn’t have to do a reverse lookup on the header
148 149 150 151 152 153 154 155 156 |
# File 'lib/sinatra/respond_to.rb', line 148 def self.included(klass) klass.class_eval do alias :content_type_without_save :content_type def content_type(*args) @_format = (args.first ? args.first.to_sym : nil) content_type_without_save *args end end end |
Instance Method Details
#charset(val = nil) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/sinatra/respond_to.rb', line 179 def charset(val=nil) fail "Content-Type must be set in order to specify a charset" if response['Content-Type'].nil? if response['Content-Type'] =~ /charset=[^;]+/ response['Content-Type'].sub!(/charset=[^;]+/, (val == '' && '') || "charset=#{val}") else response['Content-Type'] += ";charset=#{val}" end unless val.nil? response['Content-Type'][/charset=([^;]+)/, 1] end |
#format(val = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sinatra/respond_to.rb', line 158 def format(val=nil) unless val.nil? mime_type = ::Sinatra::Base.mime_type(val) fail "Unknown media type #{val}\nTry registering the extension with a mime type" if mime_type.nil? @_format = val.to_sym response['Content-Type'] ? response['Content-Type'].sub!(/^[^;]+/, mime_type) : content_type(@_format) end @_format end |
#respond_to {|wants| ... } ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/sinatra/respond_to.rb', line 191 def respond_to(&block) wants = {} def wants.method_missing(type, *args, &handler) ::Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{type}\nTry registering the extension with a mime type") if ::Sinatra::Base.mime_type(type).nil? self[type] = handler end yield wants raise UnhandledFormat if wants[format].nil? wants[format].call end |
#static_file?(path) ⇒ Boolean
This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory
172 173 174 175 176 177 |
# File 'lib/sinatra/respond_to.rb', line 172 def static_file?(path) public_dir = File.(.public) path = File.(File.join(public_dir, unescape(path))) path[0, public_dir.length] == public_dir && File.file?(path) end |