Class: Nomad::Response

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

Constant Summary collapse

BUILTIN_LOADERS =
{
  # Loads an array as an array of strings
  array_of_strings: ->(item) { Array(item).map(&:to_s) },

  # Parses an integer as a timestamp (18394289434).
  seconds_as_timestamp: ->(item) { Time.at(item || 0) },

  # Parses the given integer as a duration.
  nanoseconds_as_duration: ->(item) { Duration.new(item || 0) },

  # Parses the given integer as the number of nanoseconds since the unix
  # epoch.
  nanoseconds_as_timestamp: ->(item) { Time.at((item || 0) / 1e9) },

  # Parses the given integer as a "size".
  int_as_size_in_megabytes: ->(item) { Size.new(Float(item || 0) * Size::MEGABYTE) },

  # Parses the given integer as a "size".
  int_as_size_in_megabits: ->(item) { Size.new(Float(item || 0) * Size::MEGABIT) },

  # Returns an empty array if the given item is nil, otherwise returns the
  # item.
  nil_as_array: ->(item) { item || [] },

  # Parses the value as a string, converting "" to nil (go compat).
  string_as_nil: ->(item) {
    if item.nil? || item.strip.empty?
      nil
    else
      item
    end
  },

  stringify_keys: ->(item) { Stringify.stringify_keys(item) },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = {}) ⇒ Response

Returns a new instance of Response.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nomad/response.rb', line 80

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

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

      if (m = opts[:load])
        if m.is_a?(Symbol)
          v = BUILTIN_LOADERS[m].call(v)
        else
          v = m.call(v)
        end
      end

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

      instance_variable_set(:"@#{opts[:as]}", 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>)

Returns:

  • (Object, nil)


75
76
77
78
# File 'lib/nomad/response.rb', line 75

def self.decode(object)
  return nil if object.nil?
  self.new(object)
end

Instance Method Details

#==(other) ⇒ Object



125
126
127
# File 'lib/nomad/response.rb', line 125

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

#to_hHash

Create a hash-bashed representation of this response.

Returns:

  • (Hash)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nomad/response.rb', line 112

def to_h
  self.class.fields.inject({}) do |h, (n, opts)|
    result = self.public_send(opts[:as])

    if !result.nil? && !result.is_a?(Array) && result.respond_to?(:to_h)
      result = result.to_h
    end

    h[opts[:as]] = result
    h
  end
end