Class: Usps::Imis::BaseData

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/imis/base_data.rb

Overview

Base class for API data response wrappers

Direct Known Subclasses

Data, PartyData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = {}) ⇒ BaseData

Returns a new instance of BaseData.



25
26
27
# File 'lib/usps/imis/base_data.rb', line 25

def initialize(raw = {})
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly) Also known as: to_h

The raw API response hash



13
14
15
# File 'lib/usps/imis/base_data.rb', line 13

def raw
  @raw
end

Class Method Details

.[](hash) ⇒ Object

Create an instance from a hash



17
# File 'lib/usps/imis/base_data.rb', line 17

def self.[](hash) = new(hash)

.from_json(json) ⇒ Object

Load raw API response JSON to access properties

Parameters:

  • json (String)

    Raw API response JSON



23
# File 'lib/usps/imis/base_data.rb', line 23

def self.from_json(json) = new(JSON.parse(json))

Instance Method Details

#==(other) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/usps/imis/base_data.rb', line 65

def ==(other)
  case other
  when BaseData then raw == other.raw
  when Hash then raw == other
  else super
  end
end

#[](_property_name) ⇒ Object

Access an individual property value by name

Raises:



48
49
50
# File 'lib/usps/imis/base_data.rb', line 48

def [](_property_name)
  raise Errors::ApiError, "#{self.class} must implement #[](property_name)"
end

#[]=Object

Raises:



58
59
60
61
62
63
# File 'lib/usps/imis/base_data.rb', line 58

def []=(...)
  raise(
    Errors::ApiError,
    "#{self.class} does not support setting values. If you need to modify it, call `.raw` on it."
  )
end

#imis_idObject Also known as: id

Access the iMIS ID property

Raises:



35
36
37
# File 'lib/usps/imis/base_data.rb', line 35

def imis_id
  raise Errors::ApiError, "#{self.class} must implement #imis_id"
end

#inspectObject



73
74
75
76
77
# File 'lib/usps/imis/base_data.rb', line 73

def inspect
  stringio = StringIO.new
  PP.pp(self, stringio)
  stringio.string.delete("\n")
end

#pretty_print(pp) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/usps/imis/base_data.rb', line 79

def pretty_print(pp)
  pp.group(1, "#<#{self.class}", '>') do
    pretty_print_data.each do |key, value|
      pp.breakable
      pp.text "#{key}="
      pp.pp value
    end
  end
end

#propertiesObject

Hash of all property names to values

Raises:



54
55
56
# File 'lib/usps/imis/base_data.rb', line 54

def properties(...)
  raise Errors::ApiError, "#{self.class} must implement #properties"
end

#record_idObject

Access the Record ID property

Raises:



42
43
44
# File 'lib/usps/imis/base_data.rb', line 42

def record_id
  raise Errors::ApiError, "#{self.class} must implement #record_id"
end