Sha256: 2b688bb7701dbce1a9ec55c55ac9ba482dbea7924c7f383f3f0beb3241789bc2

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# Flow.io (2017)
# communicates with flow api, responds to webhook events

class Flow::Webhook
  attr_accessor :product
  attr_accessor :variant

  class << self
    def process data, opts={}
      web_hook = new data, opts
      web_hook.process
    end
  end

  ###

  def initialize(data, opts={})
    @data = data
    @opts = opts
  end

  def process
    @discriminator = @data['discriminator']

    m = 'hook_%s' % @discriminator

    return 'Error: No hook for %s' % @discriminator unless respond_to?(m)
    raise ArgumentError, 'Organization name mismatch for %s' % @data['organization'] if @data['organization'] != Flow.organization

    send(m)
  end

  # hooks

  def hook_localized_item_upserted
    raise ArgumentError, 'number not found' unless @data['number']
    raise ArgumentError, 'local not found' unless @data['local']

    number  = @data['number']
    exp_key = @data['local']['experience']['key']

    # for testing we need ability to inject dependency for variant class
    variant_class = @opts[:variant_class] || Spree::Variant

    @variant      = variant_class.find number
    @product      = @variant.product
    is_included   = @data['local']['status'] == 'included'

    @product.flow_data['%s.excluded' % exp_key] = is_included ? 0 : 1

    @product.save!

    message = is_included ? 'included in' : 'excluded from'

    'Product id:%s - "%s" (from variant %s) %s experience "%s"' % [@product.id, @product.name, @variant.id, message, exp_key]
  end

  # we should consume only localized_item_upserted
  def hook_subcatalog_item_upserted
    'not used'
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flowcommerce-solidus-0.1.13 ./static/app/flow/flow/webhook.rb
flowcommerce-solidus-0.1.12 ./static/app/flow/flow/webhook.rb
flowcommerce-solidus-0.1.11 ./static/app/flow/flow/webhook.rb