Class: FlowcommerceSpree::OrderSync

Inherits:
Object
  • Object
show all
Defined in:
app/services/flowcommerce_spree/order_sync.rb

Overview

represents flow.io order syncing service

Constant Summary collapse

FLOW_CENTER =
'default'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order:, flow_session_id:) ⇒ OrderSync

Returns a new instance of OrderSync.

Parameters:

  • order (Object)
  • flow_session_id (String)

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
# File 'app/services/flowcommerce_spree/order_sync.rb', line 12

def initialize(order:, flow_session_id:)
  raise(ArgumentError, 'Experience not defined or not active') unless order&.zone&.flow_io_active_experience?

  @experience = order.flow_io_experience_key
  @flow_session_id = flow_session_id
  @order = order
  @client = FlowcommerceSpree.client(default_headers: { "Authorization": "Session #{flow_session_id}" },
                                     authorization: nil)
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



8
9
10
# File 'app/services/flowcommerce_spree/order_sync.rb', line 8

def order
  @order
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'app/services/flowcommerce_spree/order_sync.rb', line 8

def response
  @response
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/services/flowcommerce_spree/order_sync.rb', line 33

def error?
  @response&.[]('code') && @response&.[]('messages') ? true : false
end

#synchronize!Object

helper method to send complete order from Spree to flow.io



23
24
25
26
27
28
29
30
31
# File 'app/services/flowcommerce_spree/order_sync.rb', line 23

def synchronize!
  return unless @order.state == 'cart' && @order.line_items.size > 0

  sync_body!
  write_response_to_order

  @order.update_columns(total: @order.total, meta: @order.meta.to_json)
  refresh_checkout_token
end