Class: Usps::Imis::Properties

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

Overview

Constructor for the Properties field

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(id = nil, ordinal = nil, hash = {}) {|| ... } ⇒ Object

Build the data for a new Properties field

Parameters:

  • id (String) (defaults to: nil)

    iMIS ID to include in the properties before running the block

  • ordinal (String) (defaults to: nil)

    Ordinal to include in the properties before running the block

  • hash (Hash) (defaults to: {})

    Hash version of props to set (block takes priority)

Yields:

  • Block context to define additional properties with add

Yield Parameters:



16
# File 'lib/usps/imis/properties.rb', line 16

def self.build(id = nil, ordinal = nil, hash = {}, &) = new.build(id, ordinal, hash, &)

.wrap(value) ⇒ Object

Wrap value in the API-internal type structure



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/usps/imis/properties.rb', line 20

def self.wrap(value)
  case value
  when String then value
  when Symbol then value.to_s
  when Time, DateTime then value.strftime('%Y-%m-%dT%H:%M:%S')
  when Integer then { '$type' => 'System.Int32', '$value' => value }
  when true, false then { '$type' => 'System.Boolean', '$value' => value }
  else
    raise Errors::UnexpectedPropertyTypeError.from(value)
  end
end

Instance Method Details

#add(name, value) ⇒ Object

Add an individual property to the field

The last definition for a given property name will be used



62
63
64
65
66
67
68
69
70
71
# File 'lib/usps/imis/properties.rb', line 62

def add(name, value)
  existing = @properties.find { it['Name'] == name.to_s }
  @properties.delete(existing) if existing

  @properties << {
    '$type' => 'Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts',
    'Name' => name.to_s,
    'Value' => self.class.wrap(value)
  }
end

#build(id = nil, ordinal = nil, hash = {}) {|| ... } ⇒ Object

Build the data for the Properties field

Parameters:

  • id (String) (defaults to: nil)

    iMIS ID to include in the properties before running the block

  • ordinal (String) (defaults to: nil)

    Ordinal to include in the properties before running the block

  • hash (Hash) (defaults to: {})

    Hash version of props to set (block takes priority)

Yields:

  • Block context to define additional properties with add

Yield Parameters:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/usps/imis/properties.rb', line 40

def build(id = nil, ordinal = nil, hash = {})
  @properties ||= []

  add('ID', id) if id
  add('Ordinal', ordinal) if ordinal

  hash.each { |key, value| add(key, value) }

  yield(self) if block_given?

  {
    'Properties' => {
      '$type' => 'Asi.Soa.Core.DataContracts.GenericPropertyDataCollection, Asi.Contracts',
      '$values' => @properties
    }
  }
end