Class: OvhSavoni::ResponseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ovh_savoni/response_builder.rb

Class Method Summary collapse

Class Method Details

.build(r, action) ⇒ Object

Build a Response class



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ovh_savoni/response_builder.rb', line 4

def self.build(r,action)
  if r.is_a?(Hash) # Build the class from the hash
    const = "OvhSavoniResponse_#{action}"
    klass=nil
    if Struct.const_defined?(const) 
      klass=Struct.const_get(const) 
    else # Create a new struct if not exist already
      klass=Struct.new(const,*r.keys)
      build_class(klass,action)
    end
    ret = klass.new(*r.values) # Instanciate the class
    if ret.members.include?(:item) && [:item].is_a?(Array)
      ret.item
    else
      ret
    end
  else # Return the value in case of basic type
    r
  end
end

.build_class(klass, action) ⇒ Object

Add recursive construction behavior to the class initializer



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ovh_savoni/response_builder.rb', line 26

def self.build_class(klass,action)
  klass.class_eval do 
    define_method(:initialize) do |*args|
      super(*args)
      each do |inst|
        each_pair do |k,v|
          if v.is_a?(Hash)
            self[k]=OvhSavoni::ResponseBuilder.build(v,"#{action}__#{k}")
            # replace response_info.item[] by response_info[]
            if self[k].respond_to?(:item) && self[k].item.is_a?(Array)
              self[k]=self[k].item
            end
          elsif v.is_a?(Array)
            self[k].map!{|i| OvhSavoni::ResponseBuilder.build(i,"#{action}__#{k}")}
          end
        end
      end
    end
  end
end