Class: Usps::Imis::BaseData
- Inherits:
-
Object
- Object
- Usps::Imis::BaseData
- Defined in:
- lib/usps/imis/base_data.rb
Overview
Base class for API data response wrappers
Instance Attribute Summary collapse
-
#raw ⇒ Object
(also: #to_h)
readonly
The raw API response hash.
Class Method Summary collapse
-
.[](hash) ⇒ Object
Create an instance from a hash.
-
.from_json(json) ⇒ Object
Load raw API response JSON to access properties.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](_property_name) ⇒ Object
Access an individual property value by name.
- #[]= ⇒ Object
-
#imis_id ⇒ Object
(also: #id)
Access the iMIS ID property.
-
#initialize(raw = {}) ⇒ BaseData
constructor
A new instance of BaseData.
- #inspect ⇒ Object
- #pretty_print(pp) ⇒ Object
-
#properties ⇒ Object
Hash of all property names to values.
-
#record_id ⇒ Object
Access the Record ID property.
- #to_json ⇒ Object
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
#raw ⇒ Object (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
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
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
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_id ⇒ Object Also known as: id
Access the iMIS ID property
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 |
#inspect ⇒ Object
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 |
#properties ⇒ Object
Hash of all property names to values
54 55 56 |
# File 'lib/usps/imis/base_data.rb', line 54 def properties(...) raise Errors::ApiError, "#{self.class} must implement #properties" end |
#record_id ⇒ Object
Access the Record ID property
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 |
#to_json ⇒ Object
31 |
# File 'lib/usps/imis/base_data.rb', line 31 def to_json(*) = raw.to_json(*) |