0
# Copyright (c) 2008, John Mettraux, OpenWFE.org
0
-# Redistribution and use in source and binary forms, with or without
0
+# Redistribution and use in source and binary forms, with or without
0
# modification, are permitted provided that the following conditions are met:
0
# . Redistributions of source code must retain the above copyright notice, this
0
-# list of conditions and the following disclaimer.
0
-# . Redistributions in binary form must reproduce the above copyright notice,
0
-# this list of conditions and the following disclaimer in the documentation
0
+# list of conditions and the following disclaimer.
0
+# . Redistributions in binary form must reproduce the above copyright notice,
0
+# this list of conditions and the following disclaimer in the documentation
0
# and/or other materials provided with the distribution.
0
# . Neither the name of the "OpenWFE" nor the names of its contributors may be
0
# used to endorse or promote products derived from this software without
0
# specific prior written permission.
0
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0
# POSSIBILITY OF SUCH DAMAGE.
0
-
representation = request.env["rack.input"].read
0
+
representation = request.env["rack.input"].read
0
-
format = determine_in_format
0
+
format = determine_in_format
0
-
send "parse_#{type}_#{format}", representation
0
+
send "parse_#{type}_#{format}", representation
0
def rrender (type, object, options={})
0
-
format, ctype = determine_out_format options
0
+
format, ctype = determine_out_format options
0
- ctype = "text/plain" if params[:plain]
0
- # useful for debugging
0
+ ctype = "text/plain" if params[:plain]
0
+ # useful for debugging
0
-
response.status = options.delete(:status) || 200
0
+
response.status = options.delete(:status) || 200
0
- header 'Content-Type' => ctype
0
- options.each { |k, v| header(k => v) }
0
+ header 'Content-Type' => ctype
0
+ options.each { |k, v| header(k => v) }
0
-
method_name = "render_#{type}_#{format}"
0
+
method_name = "render_#{type}_#{format}"
0
- send method_name, object
0
- # send method_name, object
0
- #rescue Exception => e
0
- # header 'Content-Type' => 'application/xml'
0
- # send "render_#{type}_xml", object
0
+ send method_name, object
0
+ # send method_name, object
0
+ #rescue Exception => e
0
+ # header 'Content-Type' => 'application/xml'
0
+ # send "render_#{type}_xml", object
0
def determine_in_format
0
-
ct = request.env['CONTENT_TYPE']
0
+
ct = request.env['CONTENT_TYPE']
0
- return "form" if ct.index("form-")
0
- return "json" if ct.index("application/json")
0
- return "yaml" if ct.index("application/yaml")
0
+ return "form" if ct.index("form-")
0
+ return "json" if ct.index("application/json")
0
+ return "yaml" if ct.index("application/yaml")
0
@@ -100,10 +100,10 @@ end
0
- :xml => [ "xml", "application/xml" ],
0
- :html => [ "html", "text/html" ],
0
- :json => [ "json", "application/json" ],
0
- :yaml => [ "yaml", "application/yaml" ]
0
+ :xml => [ "xml", "application/xml" ],
0
+ :html => [ "html", "text/html" ],
0
+ :json => [ "json", "application/json" ],
0
+ :yaml => [ "yaml", "application/yaml" ]
0
} unless defined?(FORMATS)
0
@@ -113,18 +113,18 @@ FORMATS = {
0
def determine_out_format (options)
0
-
f = options[:format] || params["format"]
0
+
f = options[:format] || params["format"]
0
- return FORMATS[:xml] if f == "xml"
0
- return FORMATS[:json] if f == "json"
0
- return FORMATS[:yaml] if f == "yaml"
0
+ return FORMATS[:xml] if f == "xml"
0
+ return FORMATS[:json] if f == "json"
0
+ return FORMATS[:yaml] if f == "yaml"
0
-
accept = request.env['HTTP_ACCEPT'] || ""
0
+
accept = request.env['HTTP_ACCEPT'] || ""
0
- return FORMATS[:html] if accept.index("text/html")
0
- return FORMATS[:yaml] if accept.index("yaml")
0
- return FORMATS[:json] if accept.index("json")
0
+ return FORMATS[:html] if accept.index("text/html")
0
+ return FORMATS[:yaml] if accept.index("yaml")
0
+ return FORMATS[:json] if accept.index("json")