Module: PYR::Parser

Defined in:
lib/pyr/parser.rb

Overview

Parses the response body and returns an array of response_objects.

Class Method Summary collapse

Class Method Details

.convert_to_relation(resource, memo, value) ⇒ Object



25
26
27
28
29
# File 'lib/pyr/parser.rb', line 25

def self.convert_to_relation(resource, memo, value)
  klass = "PYR::#{resource.classify}".constantize
  LazyRecord::Relation.new model: klass,
                           array: memo + value.map { |val| klass.new(val) }
end

.parse(body, controller) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/pyr/parser.rb', line 6

def self.parse(body, controller)
  if body.keys.first == 'self'
    klass = "PYR::#{controller.to_s.classify}".constantize
    LazyRecord::Relation.new model: klass, array: [klass.new(body)]
  else
    reduce_body(body)
  end
end

.reduce_body(body) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/pyr/parser.rb', line 15

def self.reduce_body(body)
  body.reduce([]) do |memo, (key, value)|
    if PYR_RESOURCES.include?(key.to_sym)
      convert_to_relation(key, memo, value)
    else
      memo
    end
  end
end