Sha256: 9aacc11e54b9033c1cc5ca11bb0a3118b13b54d859487e26d59a7f7707a3b7b7

Contents?: true

Size: 1.62 KB

Versions: 17

Compression:

Stored size: 1.62 KB

Contents

module DiscoApp::Concerns::SynchroniseCarrierServiceJob
  extend ActiveSupport::Concern

  # Ensure that any carrier service required by our app is registered.
  def perform(shop)
    # Don't proceed unless we have a name and callback url.
    return unless carrier_service_name and callback_url

    # Registered the carrier service if it hasn't been registered yet.
    unless current_carrier_service_names.include?(carrier_service_name)
      ShopifyAPI::CarrierService.create(
        name: carrier_service_name,
        callback_url: callback_url,
        service_discovery: true,
        format: :json
      )
    end

    # Ensure any existing carrier services (with the correct name) are active
    # and have a current callback URL.
    current_carrier_services.each do |carrier_service|
      if carrier_service.name == carrier_service_name
        carrier_service.callback_url = callback_url
        carrier_service.active = true
        carrier_service.save
      end
    end
  end

  protected

    def carrier_service_name
      DiscoApp.configuration.app_name
    end

    def callback_url
      @callback_url ||= begin
        callback_url = DiscoApp.configuration.carrier_service_callback_url
        callback_url.respond_to?('call') ? callback_url.call : callback_url
      end
    end

  private

    # Return a list of currently registered carrier service names.
    def current_carrier_service_names
      current_carrier_services.map(&:name)
    end

    # Return a list of currently registered carrier services.
    def current_carrier_services
      @current_carrier_service ||= ShopifyAPI::CarrierService.find(:all)
    end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
disco_app-0.10.0 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.10.1 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.10.2 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.10.3 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.10.4 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.10.5 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.11.0 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.11.1 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.12.0 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.12.1 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.12.5 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.12.6 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.12.7 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.13.0 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.13.1 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.13.2 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb
disco_app-0.12.7.pre.puma.pre.3 app/jobs/disco_app/concerns/synchronise_carrier_service_job.rb