Module: Spree::Zones::FlowIoProductZoneDecorator

Defined in:
app/models/spree/zones/flow_io_product_zone_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 6

def self.prepended(base)
  base.after_update :update_on_flow, if: -> { flow_data&.[]('key').present? }
  base.before_destroy :remove_on_flow_io, if: -> { flow_data&.[]('key').present? }
end

Instance Method Details

#available_currenciesObject



11
12
13
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 11

def available_currencies
  ((currencies || []) + [flow_data&.[]('currency')]).compact.uniq.reject(&:empty?)
end

#flow_io_active_experience?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 27

def flow_io_active_experience?
  flow_data&.[]('key').present? && flow_data['status'] == 'active'
end

#flow_io_active_or_archiving_experience?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 31

def flow_io_active_or_archiving_experience?
  flow_data&.[]('key').present? && %w[active archiving].include?(flow_data['status'])
end

#flow_io_experienceObject



15
16
17
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 15

def flow_io_experience
  flow_data&.[]('key')
end

#flow_io_experience_countryObject



19
20
21
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 19

def flow_io_experience_country
  flow_data&.[]('country')
end

#flow_io_experience_currencyObject



23
24
25
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 23

def flow_io_experience_currency
  flow_data&.[]('currency')
end

#remove_on_flow_ioObject



37
38
39
40
41
42
43
44
45
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 37

def remove_on_flow_io
  client = FlowcommerceSpree.client
  client.experiences.delete_by_key(FlowcommerceSpree::ORGANIZATION, flow_data['key'])

  # Flowcommerce `delete_by_key` methods are always returning `nil`, that's why this hack of fetching
  # @http_handler from client. This handler is a LoggingHttpHandler, which got the http_client attr_reader
  # implemented specifically for this purpose.
  false if client.instance_variable_get(:@http_handler).http_client.error
end

#store_flow_io_data(received_experience, logger: FlowcommerceSpree.logger) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 47

def store_flow_io_data(received_experience, logger: FlowcommerceSpree.logger)
  self.flow_data = received_experience.is_a?(Hash) ? received_experience : received_experience.to_hash
  self.status = flow_data['status']

  if new_record? && update_attributes(meta: meta, status: status, kind: 'country')
    logger.info "\nNew flow.io experience imported as product zone: #{name}"
  elsif update_columns(meta: meta.to_json, status: status, kind: 'country')
    logger.info "\nProduct zone `#{name}` has been updated from flow.io"
  end

  self
end

#update_on_flowObject



35
# File 'app/models/spree/zones/flow_io_product_zone_decorator.rb', line 35

def update_on_flow; end