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)



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

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

.wrap(value) ⇒ Object

Wrap value in the API-internal type structure



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

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:%I:%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



59
60
61
62
63
64
65
66
67
68
# File 'lib/usps/imis/properties.rb', line 59

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



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

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