Class: Swisspost::Model::BarcodeItem
- Inherits:
-
Object
- Object
- Swisspost::Model::BarcodeItem
- Defined in:
- lib/swisspost/model/barcode_item.rb
Constant Summary collapse
- VALIDATOR =
Dry::Validation.Contract do params do required(:weight).value(:integer) required(:service_code).value(:string) end rule(:weight) do key.failure('must be greater than 0') if value <= 0 end rule(:service_code) do allowed_services = %w[ECO PRI APOST BPOST] key.failure("must be one of: #{allowed_services.join(', ')}") unless allowed_services.include?(value) end end
Instance Attribute Summary collapse
-
#service_code ⇒ Object
readonly
Returns the value of attribute service_code.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Instance Method Summary collapse
-
#initialize(attributes) ⇒ BarcodeItem
constructor
A new instance of BarcodeItem.
Constructor Details
#initialize(attributes) ⇒ BarcodeItem
Returns a new instance of BarcodeItem.
24 25 26 27 28 29 30 31 |
# File 'lib/swisspost/model/barcode_item.rb', line 24 def initialize(attributes) result = VALIDATOR.call(attributes) raise ValidationError, result.errors.to_h.inspect if result.failure? validated_data = result.to_h @weight = validated_data[:weight] @service_code = validated_data[:service_code] end |
Instance Attribute Details
#service_code ⇒ Object (readonly)
Returns the value of attribute service_code.
6 7 8 |
# File 'lib/swisspost/model/barcode_item.rb', line 6 def service_code @service_code end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
6 7 8 |
# File 'lib/swisspost/model/barcode_item.rb', line 6 def weight @weight end |