Class: SpreePacketa::Services::Tracker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipment, operations = nil) ⇒ Tracker

Returns a new instance of Tracker.



8
9
10
11
# File 'lib/spree_packeta/services/tracker.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/tracker.rb', line 6

def operations
  @operations
end

#shipmentObject (readonly)

Returns the value of attribute shipment.



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

def shipment
  @shipment
end

Instance Method Details

#current_statusHash

Get current status of the packet

Returns:

  • (Hash)

    Current status details

Raises:



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

def current_status
  ensure_packet_exists!

  status = operations.packet_status(shipment.packeta_packet_id)
  update_shipment_status(status)
  status
end

#needs_sync?Boolean

Check if shipment needs status sync

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/spree_packeta/services/tracker.rb', line 53

def needs_sync?
  return true if shipment.packeta_last_sync_at.nil?

  sync_interval = SpreePacketa.configuration.tracking_sync_interval
  shipment.packeta_last_sync_at < sync_interval.seconds.ago
end

#sync_status!Hash

Sync status from Packeta and update shipment

Returns:

  • (Hash)

    Current status



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spree_packeta/services/tracker.rb', line 38

def sync_status!
  status = current_status

  # Update last sync timestamp
  shipment.update!(packeta_last_sync_at: Time.current)

  # Update Spree shipment state based on Packeta status
  update_spree_shipment_state(status)

  status
end

#tracking_historyArray<Hash>

Get full tracking history

Returns:

  • (Array<Hash>)

    Array of tracking records

Raises:



29
30
31
32
33
# File 'lib/spree_packeta/services/tracker.rb', line 29

def tracking_history
  ensure_packet_exists!

  operations.packet_tracking(shipment.packeta_packet_id)
end