Sha256: 28f3c111c3312177617f6b4e60d66040a7ebcdc8f16c5ee247990bd784fbb20c

Contents?: true

Size: 1.96 KB

Versions: 34

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Dor
  module Services
    class Client
      # API calls that are about retrieving metadata
      class Events < VersionedService
        Event = Struct.new(:event_type, :data, :timestamp, keyword_init: true)

        # @param object_identifier [String] the pid for the object
        def initialize(connection:, version:, object_identifier:)
          super(connection: connection, version: version)
          @object_identifier = object_identifier
        end

        # @param type [String] a type for the event, e.g., publish, shelve
        # @param data [Hash] an unstructured hash of event data
        # @return [Boolean] true if successful
        # @raise [NotFoundResponse] when the response is a 404 (object not found)
        # @raise [UnexpectedResponse] if the request is unsuccessful.
        def create(type:, data:)
          resp = connection.post do |req|
            req.url "#{api_version}/objects/#{object_identifier}/events"
            req.headers['Content-Type'] = 'application/json'
            req.body = { event_type: type, data: data }.to_json
          end

          raise_exception_based_on_response!(resp, object_identifier) unless resp.success?

          true
        end

        # @return [Array<Event>,NilClass] The events for an object or nil if 404
        # @raise [UnexpectedResponse] on an unsuccessful response from the server
        def list
          resp = connection.get do |req|
            req.url "#{api_version}/objects/#{object_identifier}/events"
          end
          return response_to_models(resp) if resp.success?
          return if resp.status == 404

          raise_exception_based_on_response!(resp, object_identifier)
        end

        private

        attr_reader :object_identifier

        def response_to_models(resp)
          JSON.parse(resp.body).map { |data| Event.new(event_type: data['event_type'], data: data['data'], timestamp: data['created_at']) }
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
dor-services-client-15.5.0 lib/dor/services/client/events.rb
dor-services-client-15.4.0 lib/dor/services/client/events.rb
dor-services-client-15.3.0 lib/dor/services/client/events.rb
dor-services-client-15.2.1 lib/dor/services/client/events.rb
dor-services-client-15.2.0 lib/dor/services/client/events.rb
dor-services-client-15.1.0 lib/dor/services/client/events.rb
dor-services-client-15.0.0 lib/dor/services/client/events.rb
dor-services-client-14.21.0 lib/dor/services/client/events.rb
dor-services-client-14.20.0 lib/dor/services/client/events.rb
dor-services-client-14.19.0 lib/dor/services/client/events.rb
dor-services-client-14.18.0 lib/dor/services/client/events.rb
dor-services-client-14.17.0 lib/dor/services/client/events.rb
dor-services-client-14.16.0 lib/dor/services/client/events.rb
dor-services-client-14.15.0 lib/dor/services/client/events.rb
dor-services-client-14.14.0 lib/dor/services/client/events.rb
dor-services-client-14.13.0 lib/dor/services/client/events.rb
dor-services-client-14.12.0 lib/dor/services/client/events.rb
dor-services-client-14.11.0 lib/dor/services/client/events.rb
dor-services-client-14.7.0 lib/dor/services/client/events.rb
dor-services-client-14.6.1 lib/dor/services/client/events.rb