Module: Sinatra::RespondTo::Helpers

Defined in:
lib/sinatra/respond_to.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

This code was copied from respond_to plugin github.com/cehoffman/sinatra-respond_to



191
192
193
194
195
196
197
198
199
200
# File 'lib/sinatra/respond_to.rb', line 191

def self.included(klass)
  klass.class_eval do
    alias :content_type_without_save :content_type
    def content_type(*args)
      content_type_without_save *args
      @_format = args.first.to_sym
      response['Content-Type']
    end
  end
end

Instance Method Details

#extension(val = nil) ⇒ Object

Extension holds trimmed extension. This is extra usefull when you want to build original URI (with extension) You can simply call “#'REQUEST_URI'.##extension



213
214
215
216
# File 'lib/sinatra/respond_to.rb', line 213

def extension(val=nil)
  @_extension ||= val
  @_extension
end

#format(val = nil) ⇒ Object

This helper will holds current format. Helper should be accesible from all places in Sinatra



220
221
222
223
# File 'lib/sinatra/respond_to.rb', line 220

def format(val=nil)
  @_format ||= val
  @_format
end

#respond_to {|wants| ... } ⇒ Object

Yields:

  • (wants)

Raises:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/sinatra/respond_to.rb', line 225

def respond_to(&block)
  wants = {}
  
  def wants.method_missing(type, *args, &handler)
    self[type] = handler
  end
  
  # Set proper content-type and encoding for
  # text based formats
  if [:xml, :gv, :html, :json].include?(format)
    content_type format, :charset => 'utf-8'
  end
  yield wants
  # Raise this error if requested format is not defined
  # in respond_to { } block.
  raise MissingTemplate if wants[format].nil?

  wants[format].call
end

#static_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
# File 'lib/sinatra/respond_to.rb', line 202

def static_file?(path)
  public_dir = File.expand_path(options.public)
  path = File.expand_path(File.join(public_dir, unescape(path)))

  path[0, public_dir.length] == public_dir && File.file?(path)
end