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.



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

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>)


40
41
42
# File 'lib/vault/response.rb', line 40

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

Instance Method Details

#==(other) ⇒ Object



88
89
90
# File 'lib/vault/response.rb', line 88

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

#to_hHash

Create a hash-bashed representation of this response.

Returns:

  • (Hash)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vault/response.rb', line 72

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