Class: ApiView::Engine
- Inherits:
-
Object
- Object
- ApiView::Engine
- Defined in:
- lib/api_view/engine.rb
Constant Summary collapse
- BASIC_TYPES =
Classes which require no further conversion
[ String, Integer, Fixnum, Bignum, Float, TrueClass, FalseClass, Time, Date, DateTime ]
- BASIC_TYPES_LOOKUP =
BASIC_TYPES.to_set
- DEFAULT_FORMAT =
'json'.freeze
Class Method Summary collapse
-
.convert(obj, options = {}) ⇒ Object
Convert the given object into a hash, array or other simple type (String, Fixnum, etc) that can be easily serialized into JSON or XML.
- .convert_custom_type(obj, options) ⇒ Object
- .convert_enumerable(obj, options) ⇒ Object
- .convert_hash(obj, options) ⇒ Object
- .converter_for(klazz, options) ⇒ Object
- .format_from_params(scope) ⇒ Object
- .format_from_request(scope) ⇒ Object
- .is_basic_type?(obj) ⇒ Boolean
-
.render(obj, scope = {}, options = {}) ⇒ String
Render the given object as JSON or XML.
-
.request_format(scope) ⇒ Object
Returns a guess at the format in this scope request_format => “xml”.
- .should_skip?(options) ⇒ Boolean
- .skip_serialization=(value) ⇒ Object
-
.to_json(obj) ⇒ Object
Returns a JSON representation of the data object.
-
.to_xml(obj) ⇒ Object
Returns an XML representation of the data object.
Class Method Details
.convert(obj, options = {}) ⇒ Object
Convert the given object into a hash, array or other simple type (String, Fixnum, etc) that can be easily serialized into JSON or XML.
50 51 52 53 54 55 |
# File 'lib/api_view/engine.rb', line 50 def convert(obj, ={}) return obj if is_basic_type?(obj) return convert_hash(obj, ) if obj.kind_of?(Hash) return convert_enumerable(obj, ) if obj.respond_to?(:map) return convert_custom_type(obj, ) end |
.convert_custom_type(obj, options) ⇒ Object
76 77 78 |
# File 'lib/api_view/engine.rb', line 76 def convert_custom_type(obj, ) converter_for(obj.class, ).new(obj).convert end |
.convert_enumerable(obj, options) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/api_view/engine.rb', line 67 def convert_enumerable(obj, ) if (.count == 0) then converter = converter_for(obj.first.class, ) return obj.map { |o| converter.new(o).convert } else return obj.map { |o| convert(o, ) } end end |
.convert_hash(obj, options) ⇒ Object
61 62 63 64 65 |
# File 'lib/api_view/engine.rb', line 61 def convert_hash(obj, ) ret = {} obj.each{ |k,v| ret[k] = convert(v, ) } ret end |
.converter_for(klazz, options) ⇒ Object
80 81 82 |
# File 'lib/api_view/engine.rb', line 80 def converter_for(klazz, ) ApiView::Registry.converter_for(klazz, ) end |
.format_from_params(scope) ⇒ Object
103 104 105 106 |
# 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) ⇒ Object
108 109 110 111 112 |
# 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) ⇒ Boolean
57 58 59 |
# File 'lib/api_view/engine.rb', line 57 def is_basic_type?(obj) BASIC_TYPES_LOOKUP.include?(obj.class) end |
.render(obj, scope = {}, options = {}) ⇒ String
Render the given object as JSON or XML
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/api_view/engine.rb', line 24 def render(obj, scope={}, ={}) ret = convert(obj, ) # skip the serialization, useful for extra-speed in unit-tests return ret if should_skip?() # already converted (by default converter, for ex) return ret if ret.kind_of? String # TODO cache_results { self.send("to_" + format.to_s) } format = [:format] || self.request_format(scope) self.send("to_" + format.to_s, ret) end |
.request_format(scope) ⇒ Object
Returns a guess at the format in this scope request_format => “xml”
96 97 98 99 100 101 |
# 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) ⇒ Boolean
37 38 39 |
# File 'lib/api_view/engine.rb', line 37 def should_skip?() .fetch(:skip_serialization) { @skip_serialization } end |
.skip_serialization=(value) ⇒ Object
41 42 43 |
# File 'lib/api_view/engine.rb', line 41 def skip_serialization=(value) @skip_serialization = value end |
.to_json(obj) ⇒ Object
Returns a JSON representation of the data object
85 86 87 |
# File 'lib/api_view/engine.rb', line 85 def to_json(obj) MultiJson.dump(obj) end |
.to_xml(obj) ⇒ Object
Returns an XML representation of the data object
90 91 92 |
# File 'lib/api_view/engine.rb', line 90 def to_xml(obj) obj.to_xml end |