Class: UspsApi::PickupAddress

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/usps_api/models/pickup_address.rb

Overview

This is the point of contact information for a potential pickup.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#check_for_conflict, #process_additional_properties, #process_array, #process_basic_value, #process_hash, #to_hash, #to_json

Constructor Details

#initialize(first_name:, last_name:, address:, contact:, firm: SKIP) ⇒ PickupAddress

Returns a new instance of PickupAddress.



59
60
61
62
63
64
65
# File 'lib/usps_api/models/pickup_address.rb', line 59

def initialize(first_name:, last_name:, address:, contact:, firm: SKIP)
  @first_name = first_name
  @last_name = last_name
  @firm = firm unless firm == SKIP
  @address = address
  @contact = contact
end

Instance Attribute Details

#addressPickupDomesticAddress

Address fields for US locations. Combinations of streetAddress with ZIPCode, City/State, or City/State/ZIPCode must be used for a valid pickup response.



30
31
32
# File 'lib/usps_api/models/pickup_address.rb', line 30

def address
  @address
end

#contactArray[Object]

One or more contact methods used to facilitate package pickup.

Returns:

  • (Array[Object])


34
35
36
# File 'lib/usps_api/models/pickup_address.rb', line 34

def contact
  @contact
end

#firmString

This is the firm corresponding to the address. Firm is required for all domestic addresses in international label requests due to customs form requirements.

Returns:

  • (String)


24
25
26
# File 'lib/usps_api/models/pickup_address.rb', line 24

def firm
  @firm
end

#first_nameString

This is the first name corresponding to the address.

Returns:

  • (String)


14
15
16
# File 'lib/usps_api/models/pickup_address.rb', line 14

def first_name
  @first_name
end

#last_nameString

This is the last name corresponding to the address.

Returns:

  • (String)


18
19
20
# File 'lib/usps_api/models/pickup_address.rb', line 18

def last_name
  @last_name
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/usps_api/models/pickup_address.rb', line 68

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  first_name = hash.key?('firstName') ? hash['firstName'] : nil
  last_name = hash.key?('lastName') ? hash['lastName'] : nil
  address = PickupDomesticAddress.from_hash(hash['address']) if hash['address']
  contact = hash.key?('contact') ? APIHelper.deserialize_union_type(
    UnionTypeLookUp.get(:PickupAddressContact), hash['contact']
  ) : nil
  firm = hash.key?('firm') ? hash['firm'] : SKIP

  # Create object from extracted values.
  PickupAddress.new(first_name: first_name,
                    last_name: last_name,
                    address: address,
                    contact: contact,
                    firm: firm)
end

.namesObject

A mapping from model property names to API property names.



37
38
39
40
41
42
43
44
45
# File 'lib/usps_api/models/pickup_address.rb', line 37

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['first_name'] = 'firstName'
  @_hash['last_name'] = 'lastName'
  @_hash['firm'] = 'firm'
  @_hash['address'] = 'address'
  @_hash['contact'] = 'contact'
  @_hash
end

.nullablesObject

An array for nullable fields



55
56
57
# File 'lib/usps_api/models/pickup_address.rb', line 55

def self.nullables
  []
end

.optionalsObject

An array for optional fields



48
49
50
51
52
# File 'lib/usps_api/models/pickup_address.rb', line 48

def self.optionals
  %w[
    firm
  ]
end

.validate(value) ⇒ Object

Validates an instance of the object from a given value.

Parameters:

  • The (PickupAddress | Hash)

    value against the validation is performed.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/usps_api/models/pickup_address.rb', line 90

def self.validate(value)
  if value.instance_of? self
    return (
      APIHelper.valid_type?(value.first_name,
                            ->(val) { val.instance_of? String }) and
        APIHelper.valid_type?(value.last_name,
                              ->(val) { val.instance_of? String }) and
        APIHelper.valid_type?(value.address,
                              ->(val) { PickupDomesticAddress.validate(val) },
                              is_model_hash: true) and
        UnionTypeLookUp.get(:PickupAddressContact)
                       .validate(value.contact)
    )
  end

  return false unless value.instance_of? Hash

  (
    APIHelper.valid_type?(value['firstName'],
                          ->(val) { val.instance_of? String }) and
      APIHelper.valid_type?(value['lastName'],
                            ->(val) { val.instance_of? String }) and
      APIHelper.valid_type?(value['address'],
                            ->(val) { PickupDomesticAddress.validate(val) },
                            is_model_hash: true) and
      UnionTypeLookUp.get(:PickupAddressContact)
                     .validate(value['contact'])
  )
end

Instance Method Details

#inspectObject

Provides a debugging-friendly string with detailed object information.



128
129
130
131
132
# File 'lib/usps_api/models/pickup_address.rb', line 128

def inspect
  class_name = self.class.name.split('::').last
  "<#{class_name} first_name: #{@first_name.inspect}, last_name: #{@last_name.inspect}, firm:"\
  " #{@firm.inspect}, address: #{@address.inspect}, contact: #{@contact.inspect}>"
end

#to_sObject

Provides a human-readable string representation of the object.



121
122
123
124
125
# File 'lib/usps_api/models/pickup_address.rb', line 121

def to_s
  class_name = self.class.name.split('::').last
  "<#{class_name} first_name: #{@first_name}, last_name: #{@last_name}, firm: #{@firm},"\
  " address: #{@address}, contact: #{@contact}>"
end