Sha256: d2324000f30b260d493df952344457d7a5f1c07259c9a06b4b7d30ddc7a06caf

Contents?: true

Size: 1.85 KB

Versions: 7

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    # TODO: Extract base functionality into `lt-google-api` gem
    module Google
      class ScriptService
        include Lcms::Engine::GoogleCredentials

        SCRIPT_ID = ENV.fetch('GOOGLE_APPLICATION_SCRIPT_ID', 'PLEASE SET UP SCRIPT ID')
        SCRIPT_FUNCTION = ENV.fetch('GOOGLE_APPLICATION_SCRIPT_FUNCTION', 'postProcessingUB')

        def initialize(document)
          @document = document
        end

        def execute(id)
          # Create an execution request object.
          request = ::Google::Apis::ScriptV1::ExecutionRequest.new(
            function: SCRIPT_FUNCTION,
            parameters: [id, gdoc_template_id, *Array.wrap(parameters)]
          )
          response = service.run_script(SCRIPT_ID, request)
          return unless response.error

          # The API executed, but the script returned an error.
          error = response.error.details[0]
          msg = +"Script error message: #{error['errorMessage']}\n"
          if error['scriptStackTraceElements']
            msg << 'Script error stacktrace:'
            error['scriptStackTraceElements'].each do |trace|
              msg << "\t#{trace['function']}: #{trace['lineNumber']}"
            end
          end
          raise ::Google::Apis::Error, msg
        end

        private

        attr_reader :document

        def gdoc_template_id
          ENV.fetch("GOOGLE_APPLICATION_TEMPLATE_#{document&.orientation&.upcase || 'PORTRAIT'}")
        end

        def parameters
          [document.cc_attribution, document.full_breadcrumb, document.short_url]
        end

        def service
          @service ||=
            begin
              service = ::Google::Apis::ScriptV1::ScriptService.new
              service.authorization = google_credentials
              service
            end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lcms-engine-0.3.1 app/services/lcms/engine/google/script_service.rb
lcms-engine-0.1.4 app/services/lcms/engine/google/script_service.rb
lcms-engine-0.3.0 app/services/lcms/engine/google/script_service.rb
lcms-engine-0.1.3 app/services/lcms/engine/google/script_service.rb
lcms-engine-0.2.0 app/services/lcms/engine/google/script_service.rb
lcms-engine-0.1.2 app/services/lcms/engine/google/script_service.rb
lcms-engine-0.1.0 app/services/lcms/engine/google/script_service.rb