Class: Vault::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/vault/response.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Response

Returns a new instance of Response.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vault/response.rb', line 41

def initialize(opts = {})
  # Initialize all fields as nil to start
  self.class.fields.each do |k, _|
    instance_variable_set(:"@#{k}", nil)
  end

  # For each supplied option, set the instance variable if it was defined
  # as a field.
  opts.each do |k, v|
    if self.class.fields.key?(k)
      opts = self.class.fields[k]

      if (m = opts[:load]) && !v.nil?
        v = m.call(v)
      end

      if opts[:freeze]
        v = v.freeze
      end

      instance_variable_set(:"@#{k}", v)
    end
  end
end

Class Method Details

.decode(object) ⇒ Object

Decodes the given object (usually a Hash) into an instance of this class.

Parameters:

  • object (Hash<Symbol, Object>)


37
38
39
# File 'lib/vault/response.rb', line 37

def self.decode(object)
  self.new(object)
end

Instance Method Details

#==(other) ⇒ Object



85
86
87
# File 'lib/vault/response.rb', line 85

def ==(other)
  self.to_h == other.to_h
end

#to_hHash

Create a hash-bashed representation of this response.

Returns:

  • (Hash)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vault/response.rb', line 69

def to_h
  self.class.fields.inject({}) do |h, (k, opts)|
    if opts[:as].nil?
      h[k] = self.public_send(k)
    else
      h[k] = self.public_send(opts[:as])
    end

    if !h[k].nil? && !h[k].is_a?(Array) && h[k].respond_to?(:to_h)
      h[k] = h[k].to_h
    end

    h
  end
end