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



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

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



70
71
72
# File 'lib/hello_sign/resource/base_resource.rb', line 70

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


78
79
80
# File 'lib/hello_sign/resource/base_resource.rb', line 78

def data
  @raw_data
end

#warningsArray<Hash>?

shows any warnings returned with the api response, if present

Returns:

  • (Array<Hash>, nil)

    Array of warning hashes in format => val, ‘warning_name’ => val or nil



86
87
88
# File 'lib/hello_sign/resource/base_resource.rb', line 86

def warnings
  @warnings
end