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) ⇒ 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



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

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

.wrap(value) ⇒ Object

Wrap value in the API-internal type structure



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

def self.wrap(value)
  case value
  when String then value
  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



51
52
53
54
55
56
57
# File 'lib/usps/imis/properties.rb', line 51

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

#build(id = nil, ordinal = nil) {|_self| ... } ⇒ 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

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/usps/imis/properties.rb', line 33

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

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

  yield(self)

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