Sha256: f5e204541183b02c6bf7238e05bf01ce5506fe0f2a6fe7a37085af21cf80cfc6
Contents?: true
Size: 1.05 KB
Versions: 3
Compression:
Stored size: 1.05 KB
Contents
module Merb # Thanks to Chris Wanstrath # use this in your controllers to switch output based on # the HTTP_ACCEPT header. like so: # respond_to do |type| # type.js { render_js } # type.html { render } # type.xml { @foo.to_xml } # type.yaml { @foo.to_yaml } # end module ResponderMixin def respond_to yield response = Response.new(@env['HTTP_ACCEPT']) @headers['Content-Type'] = response.content_type response.body end class Response attr_reader :body, :content_type def initialize(accept) @accept = accept end TYPES = { :yaml => %w[application/yaml text/yaml], :text => %w[text/plain], :html => %w[text/html */* application/html], :xml => %w[application/xml], :js => %w[application/json text/x-json] } def method_missing(method, *args) if TYPES[method] && @accept =~ Regexp.union(*TYPES[method]) @content_type = TYPES[method].first @body = yield if block_given? end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
merb-0.0.8 | lib/merb/mixins/responder_mixin.rb |
merb-0.0.9 | lib/merb/mixins/responder_mixin.rb |
merb-0.1.0 | lib/merb/mixins/responder_mixin.rb |