Class: SolidusShipstation::Api::BatchSyncer

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_shipstation/api/batch_syncer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, shipment_matcher:) ⇒ BatchSyncer

Returns a new instance of BatchSyncer.



17
18
19
20
# File 'lib/solidus_shipstation/api/batch_syncer.rb', line 17

def initialize(client:, shipment_matcher:)
  @client = client
  @shipment_matcher = shipment_matcher
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/solidus_shipstation/api/batch_syncer.rb', line 15

def client
  @client
end

#shipment_matcherObject (readonly)

Returns the value of attribute shipment_matcher.



15
16
17
# File 'lib/solidus_shipstation/api/batch_syncer.rb', line 15

def shipment_matcher
  @shipment_matcher
end

Class Method Details

.from_configObject



7
8
9
10
11
12
# File 'lib/solidus_shipstation/api/batch_syncer.rb', line 7

def from_config
  new(
    client: SolidusShipstation::Api::Client.from_config,
    shipment_matcher: SolidusShipstation.config.api_shipment_matcher,
  )
end

Instance Method Details

#call(shipments) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solidus_shipstation/api/batch_syncer.rb', line 22

def call(shipments)
  begin
    response = client.bulk_create_orders(shipments)
  rescue RateLimitedError => e
    ::Spree::Event.fire(
      'solidus_shipstation.api.rate_limited',
      shipments: shipments,
      error: e,
    )

    raise e
  rescue RequestError => e
    ::Spree::Event.fire(
      'solidus_shipstation.api.sync_errored',
      shipments: shipments,
      error: e,
    )

    raise e
  end

  return unless response

  response['results'].each do |shipstation_order|
    shipment = shipment_matcher.call(shipstation_order, shipments)

    unless shipstation_order['success']
      ::Spree::Event.fire(
        'solidus_shipstation.api.sync_failed',
        shipment: shipment,
        payload: shipstation_order,
      )

      next
    end

    shipment.update_columns(
      shipstation_synced_at: Time.zone.now,
      shipstation_order_id: shipstation_order['orderId'],
    )

    ::Spree::Event.fire(
      'solidus_shipstation.api.sync_completed',
      shipment: shipment,
      payload: shipstation_order,
    )
  end
end