Class: SpreePacketa::Services::PacketCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_packeta/services/packet_creator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipment, operations = nil) ⇒ PacketCreator

Returns a new instance of PacketCreator.



8
9
10
11
# File 'lib/spree_packeta/services/packet_creator.rb', line 8

def initialize(shipment, operations = nil)
  @shipment = shipment
  @operations = operations || Soap::Operations.new
end

Instance Attribute Details

#operationsObject (readonly)

Returns the value of attribute operations.



6
7
8
# File 'lib/spree_packeta/services/packet_creator.rb', line 6

def operations
  @operations
end

#shipmentObject (readonly)

Returns the value of attribute shipment.



6
7
8
# File 'lib/spree_packeta/services/packet_creator.rb', line 6

def shipment
  @shipment
end

Instance Method Details

#create_packetHash

Create a Packeta packet from a Spree shipment

Returns:

  • (Hash)

    Packet details with :id, :barcode, :barcode_text

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spree_packeta/services/packet_creator.rb', line 17

def create_packet
  raise ValidationError, 'Shipment already has a Packeta packet' if shipment.packeta_packet_id.present?

  # Extract attributes from shipment
  attributes = build_packet_attributes

  # Validate attributes first
  operations.packet_attributes_valid?(attributes)

  # Create the packet
  result = operations.create_packet(attributes)

  # Update shipment with packet details
  update_shipment_with_packet(result)

  result
rescue ValidationError => e
  Rails.logger.error("Failed to create Packeta packet for shipment #{shipment.id}: #{e.message}")
  raise
end