Class: Usps::Imis::Data

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

Overview

Convenience wrapper for accessing specific properties within an API data response

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_json(json) ⇒ Object

Load raw API response JSON to access properties

Parameters:

  • json (String)

    Raw API response JSON



15
# File 'lib/usps/imis/data.rb', line 15

def self.from_json(json) = self[JSON.parse(json)]

Instance Method Details

#[](property_name) ⇒ Object

Access an individual property value by name



30
31
32
33
34
35
36
# File 'lib/usps/imis/data.rb', line 30

def [](property_name)
  property = raw['Properties']['$values'].find { it['Name'] == property_name }
  return if property.nil?

  value = property['Value']
  value.is_a?(String) ? value : value['$value']
end

#imis_idObject Also known as: id

Access the iMIS ID property



21
# File 'lib/usps/imis/data.rb', line 21

def imis_id = self['ID'].to_i

#inspectObject



38
39
40
41
42
# File 'lib/usps/imis/data.rb', line 38

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

#ordinalObject

Access the Ordinal identifier property (if present)



26
# File 'lib/usps/imis/data.rb', line 26

def ordinal = self['Ordinal']&.to_i

#pretty_print(pp) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/usps/imis/data.rb', line 44

def pretty_print(pp)
  data = {
    entity_type_name: raw['EntityTypeName'],
    imis_id:,
    ordinal:
  }.compact

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