Class: Coarnotify::Core::ActivityStreams2::ActivityStream
- Inherits:
-
Object
- Object
- Coarnotify::Core::ActivityStreams2::ActivityStream
- 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
-
#context ⇒ Object
Returns the value of attribute context.
-
#doc ⇒ Object
Returns the value of attribute doc.
Instance Method Summary collapse
-
#get_property(property) ⇒ Object
Get an arbitrary property on the object.
-
#initialize(raw = nil) ⇒ ActivityStream
constructor
Construct a new ActivityStream object.
-
#register_namespace(namespace) ⇒ Object
Register a namespace in the context of the ActivityStream.
-
#set_property(property, value) ⇒ Object
Set an arbitrary property on the object.
-
#to_jsonld ⇒ Hash
Get the activity stream as a JSON-LD object.
Constructor Details
#initialize(raw = nil) ⇒ ActivityStream
Construct a new ActivityStream object
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
#context ⇒ Object
Returns the value of attribute context.
140 141 142 |
# File 'lib/coarnotify/core/activity_streams2.rb', line 140 def context @context end |
#doc ⇒ Object
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”]]
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
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”]]
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_jsonld ⇒ Hash
Get the activity stream as a JSON-LD object
225 226 227 228 229 230 |
# File 'lib/coarnotify/core/activity_streams2.rb', line 225 def to_jsonld { "@context" => @context, **@doc } end |