Sha256: 49ea4b48d41b989c86856854337056bb3b0fcb70c8f94f9ef27929dd85085811

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Shark
  module DoubleOptIn
    class Execution < Base
      extend DoubleOptIn::Resource

      attr_accessor :payload, :request_type, :recipient

      def self.verify(verification_token)
        response = connection.run(:post, "/executions/#{verification_token}/verify")
        new(response.body['data'])
      rescue UnprocessableEntity => e
        if caused_by_error_code?(e.errors, 'exceeded_number_of_verification_requests')
          raise ExceededNumberOfVerificationRequestsError
        end

        raise VerificationExpiredError if caused_by_error_code?(e.errors, 'verification_expired')

        raise e
      end

      def self.find(verification_token)
        response = connection.run(:get, "/executions/#{verification_token}")
        new(response.body['data'])
      rescue UnprocessableEntity => e
        if caused_by_error_code?(e.errors, 'requested_unverified_execution')
          raise RequestedUnverifiedExecutionError
        end

        raise e
      end

      def self.terminate(verification_token)
        response = connection.run(:delete, "/executions/#{verification_token}")
        new(response.body['data'])
      end

      def initialize(data)
        %w[payload request_type recipient].each do |key|
          public_send("#{key}=", data['attributes'][key])
        end
      end

      def self.caused_by_error_code?(errors, error_code)
        errors.detect { |error| error['code'] == error_code }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bima-shark-sdk-3.1.1 lib/shark/double_opt_in/execution.rb
bima-shark-sdk-2.5.0 lib/shark/double_opt_in/execution.rb
bima-shark-sdk-3.1.0 lib/shark/double_opt_in/execution.rb
bima-shark-sdk-3.0.0 lib/shark/double_opt_in/execution.rb
bima-shark-sdk-2.4.4 lib/shark/double_opt_in/execution.rb