class HelloSign::Resource::BaseResource

Stores the value of a hash. Use missing_method to create method to access it like an object

@author [hellosign]

Attributes

data[R]
headers[R]
raw_data[R]
warnings[R]

Public Class Methods

new(hash, key=nil) click to toggle source

Converts hash data recursively into BaseResource.

@param hash [Hash] Data of the resource @param key [String] (nil) Key of the hash, point to where resource data is. If nil, then the hash itself.

@return [HelloSign::Resource::BaseResource] a new BaseResource

# 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

Public Instance Methods

method_missing(method) click to toggle source

Magic method, gives class dynamic methods based on hash keys. If initialized hash has a key which matches the method name, return value of that key. Otherwise, return nil.

@param method [Symbol] Method's name

@example

resource = BaseResource.new email_address: "me@example.com"
resource.email_address => "me@example.com"
resource.not_in_hash_keys => nil
# File lib/hello_sign/resource/base_resource.rb, line 68
def method_missing(method)
  @data.key?(method.to_s) ? @data[method.to_s] : nil
end