Class: Coarnotify::Core::Notify::NotifyPattern

Inherits:
NotifyBase
  • Object
show all
Defined in:
lib/coarnotify/core/notify.rb

Overview

Base class for all notification patterns

Instance Attribute Summary

Attributes inherited from NotifyBase

#properties_by_reference, #validate_properties, #validate_stream_on_construct, #validators

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NotifyBase

#doc, #get_property, #id, #id=, #optional_and_validate, #register_property_validation_error, #required, #required_and_validate, #set_property, #to_jsonld, #type, #type=, #validate_property

Constructor Details

#initialize(stream: nil, validate_stream_on_construct: true, validate_properties: true, validators: nil, validation_context: nil, properties_by_reference: true) ⇒ NotifyPattern

Constructor for the NotifyPattern

This constructor will ensure that the pattern has its mandated type in the type property

Parameters:

  • stream (ActivityStreams2::ActivityStream, Hash) (defaults to: nil)

    The activity stream object, or a hash from which one can be created

  • validate_stream_on_construct (Boolean) (defaults to: true)

    should the incoming stream be validated at construction-time

  • validate_properties (Boolean) (defaults to: true)

    should individual properties be validated as they are set

  • validators (Validate::Validator) (defaults to: nil)

    the validator object for this class and all nested elements

  • validation_context (String, Array) (defaults to: nil)

    the context in which this object is being validated

  • properties_by_reference (Boolean) (defaults to: true)

    should properties be get and set by reference (the default) or by value



367
368
369
370
371
372
373
# File 'lib/coarnotify/core/notify.rb', line 367

def initialize(stream: nil, validate_stream_on_construct: true, validate_properties: true,
               validators: nil, validation_context: nil, properties_by_reference: true)
  super(stream: stream, validate_stream_on_construct: validate_stream_on_construct,
        validate_properties: validate_properties, validators: validators,
        validation_context: validation_context, properties_by_reference: properties_by_reference)
  ensure_type_contains(self.class.type_constant)
end

Class Method Details

.type_constantObject

The type of the pattern. This should be overridden by subclasses, otherwise defaults to Object



353
354
355
# File 'lib/coarnotify/core/notify.rb', line 353

def self.type_constant
  ActivityStreams2::ActivityStreamsTypes::OBJECT
end

Instance Method Details

#actorNotifyActor?

Get the actor property of the notification

Returns:



470
471
472
473
474
475
476
477
478
# File 'lib/coarnotify/core/notify.rb', line 470

def actor
  a = get_property(ActivityStreams2::Properties::ACTOR)
  if a
    NotifyActor.new(stream: a, validate_stream_on_construct: false,
                    validate_properties: @validate_properties, validators: @validators,
                    validation_context: ActivityStreams2::Properties::ACTOR,
                    properties_by_reference: @properties_by_reference)
  end
end

#actor=(value) ⇒ Object

Set the actor property of the notification

Parameters:



483
484
485
# File 'lib/coarnotify/core/notify.rb', line 483

def actor=(value)
  set_property(ActivityStreams2::Properties::ACTOR, value.doc)
end

#contextNotifyObject?

Get the context property of the notification

Returns:



490
491
492
493
494
495
496
497
498
# File 'lib/coarnotify/core/notify.rb', line 490

def context
  c = get_property(ActivityStreams2::Properties::CONTEXT)
  if c
    NotifyObject.new(stream: c, validate_stream_on_construct: false,
                     validate_properties: @validate_properties, validators: @validators,
                     validation_context: ActivityStreams2::Properties::CONTEXT,
                     properties_by_reference: @properties_by_reference)
  end
end

#context=(value) ⇒ Object

Set the context property of the notification

Parameters:



503
504
505
# File 'lib/coarnotify/core/notify.rb', line 503

def context=(value)
  set_property(ActivityStreams2::Properties::CONTEXT, value.doc)
end

#ensure_type_contains(types) ⇒ Object

Ensure that the type field contains the given types

Parameters:

  • types (String, Array<String>)

    the types to ensure are present



378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/coarnotify/core/notify.rb', line 378

def ensure_type_contains(types)
  existing = @stream.get_property(ActivityStreams2::Properties::TYPE)
  if existing.nil?
    set_property(ActivityStreams2::Properties::TYPE, types)
  else
    existing = [existing] unless existing.is_a?(Array)
    types = [types] unless types.is_a?(Array)
    types.each do |t|
      existing << t unless existing.include?(t)
    end
    existing = existing.length == 1 ? existing[0] : existing
    set_property(ActivityStreams2::Properties::TYPE, existing)
  end
end

#in_reply_toString

Get the inReplyTo property of the notification

Returns:

  • (String)

    the inReplyTo value



456
457
458
# File 'lib/coarnotify/core/notify.rb', line 456

def in_reply_to
  get_property(ActivityStreams2::Properties::IN_REPLY_TO)
end

#in_reply_to=(value) ⇒ Object

Set the inReplyTo property of the notification

Parameters:

  • value (String)

    the inReplyTo value to set



463
464
465
# File 'lib/coarnotify/core/notify.rb', line 463

def in_reply_to=(value)
  set_property(ActivityStreams2::Properties::IN_REPLY_TO, value)
end

#objectNotifyObject?

Get the object property of the notification

Returns:



436
437
438
439
440
441
442
443
444
# File 'lib/coarnotify/core/notify.rb', line 436

def object
  o = get_property(ActivityStreams2::Properties::OBJECT)
  if o
    NotifyObject.new(stream: o, validate_stream_on_construct: false,
                     validate_properties: @validate_properties, validators: @validators,
                     validation_context: ActivityStreams2::Properties::OBJECT,
                     properties_by_reference: @properties_by_reference)
  end
end

#object=(value) ⇒ Object

Set the object property of the notification

Parameters:



449
450
451
# File 'lib/coarnotify/core/notify.rb', line 449

def object=(value)
  set_property(ActivityStreams2::Properties::OBJECT, value.doc)
end

#originNotifyService?

Get the origin property of the notification

Returns:



396
397
398
399
400
401
402
403
404
# File 'lib/coarnotify/core/notify.rb', line 396

def origin
  o = get_property(ActivityStreams2::Properties::ORIGIN)
  if o
    NotifyService.new(stream: o, validate_stream_on_construct: false,
                      validate_properties: @validate_properties, validators: @validators,
                      validation_context: ActivityStreams2::Properties::ORIGIN,
                      properties_by_reference: @properties_by_reference)
  end
end

#origin=(value) ⇒ Object

Set the origin property of the notification

Parameters:



409
410
411
# File 'lib/coarnotify/core/notify.rb', line 409

def origin=(value)
  set_property(ActivityStreams2::Properties::ORIGIN, value.doc)
end

#targetNotifyService?

Get the target property of the notification

Returns:



416
417
418
419
420
421
422
423
424
# File 'lib/coarnotify/core/notify.rb', line 416

def target
  t = get_property(ActivityStreams2::Properties::TARGET)
  if t
    NotifyService.new(stream: t, validate_stream_on_construct: false,
                      validate_properties: @validate_properties, validators: @validators,
                      validation_context: ActivityStreams2::Properties::TARGET,
                      properties_by_reference: @properties_by_reference)
  end
end

#target=(value) ⇒ Object

Set the target property of the notification

Parameters:



429
430
431
# File 'lib/coarnotify/core/notify.rb', line 429

def target=(value)
  set_property(ActivityStreams2::Properties::TARGET, value.doc)
end

#validateBoolean

Base validator for all notification patterns. This extends the validate function on the superclass.

In addition to the base class’s constraints, this applies the following validation:

  • The origin, target and object properties are required and must be valid

  • The actor inReplyTo and context properties are optional, but if present must be valid

Returns:

  • (Boolean)

    true if valid, otherwise raises ValidationError



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/coarnotify/core/notify.rb', line 515

def validate
  ve = ValidationError.new
  begin
    super
  rescue ValidationError => superve
    ve = superve
  end

  required_and_validate(ve, ActivityStreams2::Properties::ORIGIN, origin)
  required_and_validate(ve, ActivityStreams2::Properties::TARGET, target)
  required_and_validate(ve, ActivityStreams2::Properties::OBJECT, object)
  optional_and_validate(ve, ActivityStreams2::Properties::ACTOR, actor)
  optional_and_validate(ve, ActivityStreams2::Properties::IN_REPLY_TO, in_reply_to)
  optional_and_validate(ve, ActivityStreams2::Properties::CONTEXT, context)

  raise ve if ve.has_errors?
  true
end