Class: HelloSign::Resource::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/hello_sign/resource/base_resource.rb

Overview

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

Author:

  • hellosign

Instance Method Summary collapse

Constructor Details

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

recursively convert hash data 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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hello_sign/resource/base_resource.rb', line 19

def initialize(hash, key=nil)
  @raw_data = key ? hash[key] : hash
  @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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Magic method, give 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

Examples:

resource  = BaseResource.new :email_address => "[email protected]"
resource.email_address # =>  "[email protected]"
resource.not_in_hash_keys # => nil

Parameters:

  • method (Symbol)

    Method’s name



47
48
49
# File 'lib/hello_sign/resource/base_resource.rb', line 47

def method_missing(method)
  @data.key?(method.to_s) ? @data[method.to_s] : nil
end

Instance Method Details

#datatype

raw response data from the server in json

Returns:

  • (type)
    description


55
56
57
# File 'lib/hello_sign/resource/base_resource.rb', line 55

def data
  @raw_data
end