Sha256: 5ba6e1dc14d7bd66de46c12de657fd330d9a43b57856692975f47a1d288fb0af

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 KB

Contents

module HelloSign
  module Resource

    #
    # Store the value of a hash. Use missing_method to create method to access it like an object
    #
    # @author [hellosign]
    #
    class BaseResource

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

      #
      # 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
      #
      # @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
      def method_missing(method)
        @data.key?(method.to_s) ? @data[method.to_s] : nil
      end

      #
      # raw response data from the server in json
      #
      # @return [type] [description]
      def data
        @raw_data
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hellosign-ruby-sdk-3.0.11 lib/hello_sign/resource/base_resource.rb
hellosign-ruby-sdk-3.0.10 lib/hello_sign/resource/base_resource.rb
hellosign-ruby-sdk-3.0.9 lib/hello_sign/resource/base_resource.rb
hellosign-ruby-sdk-3.0.7 lib/hello_sign/resource/base_resource.rb
hellosign-ruby-sdk-3.0.6 lib/hello_sign/resource/base_resource.rb
hellosign-ruby-sdk-3.0.5 lib/hello_sign/resource/base_resource.rb
hellosign-ruby-sdk-3.0.4 lib/hello_sign/resource/base_resource.rb