Class: Ipregistry::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ipregistry/models/base.rb

Overview

Base class for all response models. Wraps the decoded JSON hash and exposes convenient readers declared with a small class-level DSL.

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • data (Hash, nil) (defaults to: {})

    the decoded JSON payload



14
15
16
# File 'lib/ipregistry/models/base.rb', line 14

def initialize(data = {})
  @data = data.is_a?(Hash) ? data : {}
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



30
31
32
# File 'lib/ipregistry/models/base.rb', line 30

def ==(other)
  other.instance_of?(self.class) && other.to_h == @data
end

#[](key) ⇒ Object

Raw access to any field of the payload, including ones without a dedicated reader.



26
27
28
# File 'lib/ipregistry/models/base.rb', line 26

def [](key)
  @data[key.to_s]
end

#hashObject



35
36
37
# File 'lib/ipregistry/models/base.rb', line 35

def hash
  [self.class, @data].hash
end

#inspectObject



39
40
41
# File 'lib/ipregistry/models/base.rb', line 39

def inspect
  "#<#{self.class.name} #{@data.inspect}>"
end

#to_hHash

The raw decoded payload backing this model.

Returns:

  • (Hash)


20
21
22
# File 'lib/ipregistry/models/base.rb', line 20

def to_h
  @data
end