Class: ActivityPub::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/activitypub/base.rb

Overview

This is not part of ActivityPub, but provides basic mechanisms for serialization and dezerialisation.

Direct Known Subclasses

Link, Object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_rawObject

Returns the value of attribute _raw.



68
69
70
# File 'lib/activitypub/base.rb', line 68

def _raw
  @_raw
end

#_resolverObject

Returns the value of attribute _resolver.



67
68
69
# File 'lib/activitypub/base.rb', line 67

def _resolver
  @_resolver
end

Class Method Details

.ap_attr(name, type = nil) ⇒ Object

FIXME: Allow specifying a type (e.g. URI)



72
73
74
75
76
77
78
# File 'lib/activitypub/base.rb', line 72

def self.ap_attr(name, type=nil)
  @ap_attributes ||= []
  @ap_attributes << name
  @ap_types ||= {}
  @ap_types[name] = type
  attr_accessor name
end

.ap_attributesObject



85
86
87
88
# File 'lib/activitypub/base.rb', line 85

def self.ap_attributes
  parent = superclass.respond_to?(:ap_attributes) ? superclass.ap_attributes : []
  parent.concat(@ap_attributes||[])
end

.ap_typesObject



80
81
82
83
# File 'lib/activitypub/base.rb', line 80

def self.ap_types
  parent = superclass.respond_to?(:ap_types) ? superclass.ap_types : {}
  parent.merge(@ap_types || {})
end

Instance Method Details

#_contextObject



64
# File 'lib/activitypub/base.rb', line 64

def _context = @_context || "https://www.w3.org/ns/activitystreams"

#_typeObject



65
# File 'lib/activitypub/base.rb', line 65

def _type = self.class.name.split("::").last

#to_hObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/activitypub/base.rb', line 90

def to_h
  h = {
    "@context" => _context,
    "type" => _type,
  }

  self.class.ap_attributes.each do |attr|
    val = instance_variable_get("@#{attr}")
    h[attr.to_s] = val if val
  end

  h
end

#to_jsonObject



104
# File 'lib/activitypub/base.rb', line 104

def to_json(...) = to_h.to_json(...)