Class: WebPay::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/webpay/data_types.rb

Instance Method Summary collapse

Instance Method Details

#normalize_hash(hash) ⇒ Object

Remove nil values and stringify keys



4
5
6
# File 'lib/webpay/data_types.rb', line 4

def normalize_hash(hash)
  hash.each_with_object({}) { |kv, obj| k,v = kv; obj[k.to_s] = v unless v == nil }
end

#to_hHash Also known as: to_hash

Convert attributes and its children to pure-Ruby hash

Returns:

  • (Hash)

    pure ruby hash including no user objects



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/webpay/data_types.rb', line 10

def to_h
  @attributes.each_with_object({}) do |kv, obj|
    k, v = kv
    next if v == nil
    obj[k] =
      case v
      when Entity
        v.to_h
      when Array
        v.map { |elem| elem.respond_to?(:to_h) ? elem.to_h : elem }
      else
        v
      end
  end
end

#to_sObject Also known as: inspect

Pretty print object’s data



29
30
31
32
33
34
35
# File 'lib/webpay/data_types.rb', line 29

def to_s
  rendered = "#<#{self.class}\n"
  self.class.fields.each do |k|
    rendered << "  #{k}: " << @attributes[k].inspect.gsub(/(\r?\n)/, '\1  ') << "\n"
  end
  rendered << ">"
end