Class: SpreePacketa::Soap::Operations

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_packeta/soap/operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ Operations

Returns a new instance of Operations.



8
9
10
# File 'lib/spree_packeta/soap/operations.rb', line 8

def initialize(client = nil)
  @client = client || Client.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/spree_packeta/soap/operations.rb', line 6

def client
  @client
end

Instance Method Details

#cancel_packet(packet_id) ⇒ Boolean

Cancel a packet (if allowed)

Parameters:

  • packet_id (Integer)

    Packeta packet ID

Returns:

  • (Boolean)

    True if cancelled successfully



98
99
100
101
# File 'lib/spree_packeta/soap/operations.rb', line 98

def cancel_packet(packet_id)
  client.call(:cancel_packet, packet_id: packet_id)
  true
end

#create_packet(attributes) ⇒ Hash

Create a new packet

Parameters:

  • attributes (Hash)

    Packet attributes

Options Hash (attributes):

  • :number (String)

    Order number

  • :name (String)

    Customer first name

  • :surname (String)

    Customer last name

  • :email (String)

    Customer email

  • :phone (String)

    Customer phone

  • :address_id (Integer)

    Packeta pickup point ID

  • :value (BigDecimal)

    Package value

  • :weight (BigDecimal)

    Package weight in kg

  • :cod (BigDecimal)

    Cash on delivery amount (optional)

  • :currency (String)

    Currency code (CZK, EUR, etc.)

Returns:

  • (Hash)

    Created packet details with :id, :barcode, :barcode_text



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spree_packeta/soap/operations.rb', line 39

def create_packet(attributes)
  response = client.call(:create_packet, attributes: packet_attributes_hash(attributes))

  result = response.body[:create_packet_response][:create_packet_result]

  {
    id: result[:id],
    barcode: result[:barcode],
    barcode_text: result[:barcode_text]
  }
end

#packet_attributes_valid?(attributes) ⇒ Boolean

Validate packet attributes before creating a packet

Parameters:

  • attributes (Hash)

    Packet attributes

Returns:

  • (Boolean)

    True if valid

Raises:



17
18
19
20
21
22
23
# File 'lib/spree_packeta/soap/operations.rb', line 17

def packet_attributes_valid?(attributes)
  response = client.call(:packet_attributes_valid, attributes: packet_attributes_hash(attributes))
  true
rescue ValidationError => e
  Rails.logger.error("Packet attributes validation failed: #{e.message}")
  raise
end

#packet_label_pdf(packet_id, format: 'A7 on A4', offset: 0) ⇒ String

Get packet label as PDF

Parameters:

  • packet_id (Integer)

    Packeta packet ID

  • format (String) (defaults to: 'A7 on A4')

    Label format ('A7 on A4', 'A6 on A4', etc.)

  • offset (Integer) (defaults to: 0)

    Offset for positioning on page (0-3)

Returns:

  • (String)

    Base64 encoded PDF



109
110
111
112
113
114
115
116
117
118
# File 'lib/spree_packeta/soap/operations.rb', line 109

def packet_label_pdf(packet_id, format: 'A7 on A4', offset: 0)
  response = client.call(
    :packet_label_pdf,
    packet_id: packet_id,
    format: format,
    offset: offset
  )

  response.body[:packet_label_pdf_response][:packet_label_pdf_result]
end

#packet_status(packet_id) ⇒ Hash

Get current status of a packet

Parameters:

  • packet_id (Integer)

    Packeta packet ID

Returns:

  • (Hash)

    Current status with :date_time, :status_code, :code_text, :status_text, :branch_id



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spree_packeta/soap/operations.rb', line 55

def packet_status(packet_id)
  response = client.call(:packet_status, packet_id: packet_id)

  result = response.body[:packet_status_response][:packet_status_result]

  {
    date_time: result[:date_time],
    status_code: result[:status_code],
    code_text: result[:code_text],
    status_text: result[:status_text],
    branch_id: result[:branch_id],
    destination_branch_id: result[:destination_branch_id],
    is_returning: result[:is_returning],
    stored_until: result[:stored_until]
  }
end

#packet_tracking(packet_id) ⇒ Array<Hash>

Get full tracking history for a packet

Parameters:

  • packet_id (Integer)

    Packeta packet ID

Returns:

  • (Array<Hash>)

    Array of tracking records



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spree_packeta/soap/operations.rb', line 76

def packet_tracking(packet_id)
  response = client.call(:packet_tracking, packet_id: packet_id)

  records = response.body[:packet_tracking_response][:packet_tracking_result][:record]
  records = [records] unless records.is_a?(Array) # Handle single record

  records.map do |record|
    {
      date_time: record[:date_time],
      status_code: record[:status_code],
      code_text: record[:code_text],
      status_text: record[:status_text],
      branch_id: record[:branch_id],
      destination_branch_id: record[:destination_branch_id]
    }
  end
end