Class: Coarnotify::Core::ActivityStreams2::ActivityStream

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

Overview

A simple wrapper around an ActivityStreams hash object

Construct it with a ruby hash that represents an ActivityStreams object, or without to create a fresh, blank object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = nil) ⇒ ActivityStream

Construct a new ActivityStream object

Parameters:

  • raw (Hash) (defaults to: nil)

    the raw ActivityStreams object, as a hash



145
146
147
148
149
150
151
152
153
# File 'lib/coarnotify/core/activity_streams2.rb', line 145

def initialize(raw = nil)
  @doc = raw || {}
  @context = []
  if @doc.key?("@context")
    @context = @doc["@context"]
    @context = [@context] unless @context.is_a?(Array)
    @doc.delete("@context")
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



140
141
142
# File 'lib/coarnotify/core/activity_streams2.rb', line 140

def context
  @context
end

#docObject

Returns the value of attribute doc.



140
141
142
# File 'lib/coarnotify/core/activity_streams2.rb', line 140

def doc
  @doc
end

Instance Method Details

#get_property(property) ⇒ Object

Get an arbitrary property on the object. The property name can be one of:

  • A simple string with the property name

  • An array of the property name and the full namespace [“name”, “example.com/ns”]

  • An array containing the property name and another array of the short name and the full namespace [“name”, [“as”, “example.com/ns”]]

Parameters:

  • property (String, Array)

    the property name

Returns:

  • (Object)

    the value of the property, or nil if it does not exist



211
212
213
214
215
216
217
218
219
220
# File 'lib/coarnotify/core/activity_streams2.rb', line 211

def get_property(property)
  prop_name = property
  namespace = nil
  if property.is_a?(Array)
    prop_name = property[0]
    namespace = property[1]
  end

  @doc[prop_name]
end

#register_namespace(namespace) ⇒ Object

Register a namespace in the context of the ActivityStream

Parameters:

  • namespace (String, Array)

    the namespace to register



172
173
174
175
176
177
178
179
180
181
# File 'lib/coarnotify/core/activity_streams2.rb', line 172

def register_namespace(namespace)
  entry = namespace
  if namespace.is_a?(Array)
    url = namespace[1]
    short = namespace[0]
    entry = { short => url }
  end

  @context << entry unless @context.include?(entry)
end

#set_property(property, value) ⇒ Object

Set an arbitrary property on the object. The property name can be one of:

  • A simple string with the property name

  • An array of the property name and the full namespace [“name”, “example.com/ns”]

  • An array containing the property name and another array of the short name and the full namespace [“name”, [“as”, “example.com/ns”]]

Parameters:

  • property (String, Array)

    the property name

  • value (Object)

    the value to set



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/coarnotify/core/activity_streams2.rb', line 191

def set_property(property, value)
  prop_name = property
  namespace = nil
  if property.is_a?(Array)
    prop_name = property[0]
    namespace = property[1]
  end

  @doc[prop_name] = value
  register_namespace(namespace) if namespace
end

#to_jsonldHash

Get the activity stream as a JSON-LD object

Returns:

  • (Hash)

    the JSON-LD representation



225
226
227
228
229
230
# File 'lib/coarnotify/core/activity_streams2.rb', line 225

def to_jsonld
  {
    "@context" => @context,
    **@doc
  }
end