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

#_resolverObject

Returns the value of attribute _resolver.



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

def _resolver
  @_resolver
end

Class Method Details

.ap_attr(name, type = nil) ⇒ Object

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



66
67
68
69
70
71
72
# File 'lib/activitypub/base.rb', line 66

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

.ap_attributesObject



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

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

.ap_typesObject



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

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

Instance Method Details

#_contextObject



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

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

#_typeObject



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

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

#to_hObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/activitypub/base.rb', line 84

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



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

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