Class: HelloSign::Resource::BaseResource
- Inherits:
-
Object
- Object
- HelloSign::Resource::BaseResource
- 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
Direct Known Subclasses
Account, ApiApp, Embedded, SignatureRequest, Team, Template, TemplateDraft, UnclaimedDraft
Instance Method Summary collapse
-
#data ⇒ type
raw response data from the server in json.
-
#initialize(hash, key = nil) ⇒ HelloSign::Resource::BaseResource
constructor
recursively convert hash data into BaseResource.
-
#method_missing(method) ⇒ Object
Magic method, give class dynamic methods based on hash keys.
-
#warnings ⇒ Array<Hash>?
shows any warnings returned with the api response, if present.
Constructor Details
#initialize(hash, key = nil) ⇒ HelloSign::Resource::BaseResource
recursively convert hash data into BaseResource.
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
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
#data ⇒ type
raw response data from the server in json
78 79 80 |
# File 'lib/hello_sign/resource/base_resource.rb', line 78 def data @raw_data end |
#warnings ⇒ Array<Hash>?
shows any warnings returned with the api response, if present
86 87 88 |
# File 'lib/hello_sign/resource/base_resource.rb', line 86 def warnings @warnings end |