Method: HelloSign::Resource::BaseResource#initialize

Defined in:
lib/hello_sign/resource/base_resource.rb

#initialize(hash, key = nil) ⇒ HelloSign::Resource::BaseResource

Converts hash data recursively into BaseResource.

Parameters:

  • hash (Hash)

    Data of the resource

  • key (String) (defaults to: nil)

    (nil) Key of the hash, point to where resource data is. If nil, then the hash itself.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hello_sign/resource/base_resource.rb', line 39

def initialize(hash, key=nil)
  @headers = hash[:headers]
  @raw_data = key ? hash[:body][key] : hash
  if hash[:body]
    @warnings = hash[:body]['warnings'] ? hash[:body]['warnings'] : nil
  end

  @data = @raw_data.inject({}) do |data, (key, value)|
    data[key.to_s] = if value.is_a? Hash
      value = BaseResource.new(value)
    elsif ((value.is_a? Array) && (value[0].is_a? Hash))
      value = value.map {|v| BaseResource.new(v)}
    else
      value
    end
    data
  end
end