class ApiView::Engine
Constants
- BASIC_TYPES
Classes which require no further conversion
- BASIC_TYPES_LOOKUP
- DEFAULT_FORMAT
Public Class Methods
convert(obj, options={})
click to toggle source
Convert the given object into a hash, array or other simple type (String, Fixnum, etc) that can be easily serialized into JSON or XML.
@param [Object] obj @return [Object]
# File lib/api_view/engine.rb, line 50 def convert(obj, options={}) return obj if is_basic_type?(obj) return convert_hash(obj, options) if obj.kind_of?(Hash) return convert_enumerable(obj, options) if obj.respond_to?(:map) return convert_custom_type(obj, options) end
convert_custom_type(obj, options)
click to toggle source
# File lib/api_view/engine.rb, line 76 def convert_custom_type(obj, options) converter_for(obj.class, options).new(obj).convert end
convert_enumerable(obj, options)
click to toggle source
# File lib/api_view/engine.rb, line 67 def convert_enumerable(obj, options) if (options.count == 0) then converter = converter_for(obj.first.class, options) return obj.map { |o| converter.new(o).convert } else return obj.map { |o| convert(o, options) } end end
convert_hash(obj, options)
click to toggle source
# File lib/api_view/engine.rb, line 61 def convert_hash(obj, options) ret = {} obj.each{ |k,v| ret[k] = convert(v, options) } ret end
converter_for(klazz, options)
click to toggle source
# File lib/api_view/engine.rb, line 80 def converter_for(klazz, options) ApiView::Registry.converter_for(klazz, options) end
format_from_params(scope)
click to toggle source
# File lib/api_view/engine.rb, line 103 def format_from_params(scope) params = scope.respond_to?(:params) ? scope.params : {} params[:format] end
format_from_request(scope)
click to toggle source
# File lib/api_view/engine.rb, line 108 def format_from_request(scope) request = scope.respond_to?(:request) && scope.request return unless request request.format.to_sym.to_s if request.respond_to?(:format) end
is_basic_type?(obj)
click to toggle source
# File lib/api_view/engine.rb, line 57 def is_basic_type?(obj) BASIC_TYPES_LOOKUP.include?(obj.class) end
render(obj, scope={}, options={})
click to toggle source
Render the given object as JSON or XML
@param [Object] obj @param [ActionDispatch::Request] scope @param [Hash] options @option options [String] :format Request a particular format (“json” or “xml”)
@return [String]
# File lib/api_view/engine.rb, line 24 def render(obj, scope={}, options={}) ret = convert(obj, options) # skip the serialization, useful for extra-speed in unit-tests return ret if should_skip?(options) # already converted (by default converter, for ex) return ret if ret.kind_of? String # TODO cache_results { self.send("to_" + format.to_s) } format = options[:format] || self.request_format(scope) self.send("to_" + format.to_s, ret) end
request_format(scope)
click to toggle source
Returns a guess at the format in this scope request_format
=> “xml”
# File lib/api_view/engine.rb, line 96 def request_format(scope) format = format_from_params(scope) format ||= format_from_request(scope) return format if (format && self.respond_to?("to_#{format}")) DEFAULT_FORMAT end
should_skip?(options)
click to toggle source
# File lib/api_view/engine.rb, line 37 def should_skip?(options) options.fetch(:skip_serialization) { @skip_serialization } end
skip_serialization=(value)
click to toggle source
# File lib/api_view/engine.rb, line 41 def skip_serialization=(value) @skip_serialization = value end
to_json(obj)
click to toggle source
Returns a JSON representation of the data object
# File lib/api_view/engine.rb, line 85 def to_json(obj) MultiJson.dump(obj) end
to_xml(obj)
click to toggle source
Returns an XML representation of the data object
# File lib/api_view/engine.rb, line 90 def to_xml(obj) obj.to_xml end