Class: ApiView::Engine

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • obj (Object)

Returns:

  • (Object)


50
51
52
53
54
55
# 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) ⇒ Object



76
77
78
# 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) ⇒ Object



67
68
69
70
71
72
73
74
# 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) ⇒ Object



61
62
63
64
65
# 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) ⇒ Object



80
81
82
# File 'lib/api_view/engine.rb', line 80

def converter_for(klazz, options)
  ApiView::Registry.converter_for(klazz, options)
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

Returns:

  • (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

Parameters:

  • obj (Object)
  • scope (ActionDispatch::Request) (defaults to: {})
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :format (String)

    Request a particular format (“json” or “xml”)

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ 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

Returns:

  • (Boolean)


37
38
39
# File 'lib/api_view/engine.rb', line 37

def should_skip?(options)
  options.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