Module: Restify::Processors::Base::Parsing

Defined in:
lib/restify/processors/base/parsing.rb

Overview

Parses generic data structures into resources

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
# File 'lib/restify/processors/base/parsing.rb', line 12

def self.included(base)
  base.extend ClassMethods
  base.indifferent_access = true
end

Instance Method Details

#loadObject



17
18
19
# File 'lib/restify/processors/base/parsing.rb', line 17

def load
  parse deserialized_body, root: true
end

#parse(object, root: false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/restify/processors/base/parsing.rb', line 21

def parse(object, root: false)
  case object
    when Hash
      data      = object.each_with_object({}, &method(:parse_data))
      relations = object.each_with_object({}, &method(:parse_rels))

      data = with_indifferent_access(data) if self.class.indifferent_access?

      Resource.new context,
        data: data,
        response: root ? response : nil,
        relations: relations

    when Array
      object.map(&method(:parse))
    else
      object
  end
end