Module: Urbit::Fact

Defined in:
lib/urbit/fact.rb,
lib/urbit/fact/base_fact.rb,
lib/urbit/fact/graph_fact.rb,
lib/urbit/fact/group_fact.rb,
lib/urbit/fact/metadata_fact.rb,
lib/urbit/fact/settings_fact.rb

Defined Under Namespace

Classes: AddGraphFact, AddGroupFact, AddGroupMemberFact, AddNodesFact, AddTagFact, BaseFact, EmptyFact, ErrorFact, GraphUpdateFact, GroupUpdateFact, InitialGroupFact, InitialGroupGroupFact, MetadataUpdateFact, RemoveGraphFact, RemoveGroupMemberFact, RemoveTagFact, SettingsEventDelBucketFact, SettingsEventDelEntryFact, SettingsEventFact, SettingsEventPutBucketFact, SettingsEventPutEntryFact, SuccessFact

Class Method Summary collapse

Class Method Details

.collect(channel:, event:) ⇒ Object

This is a Factory method to make the proper Fact subclass from a Channel Event.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/urbit/fact.rb', line 15

def collect(channel:, event:)
  contents = JSON.parse(event.data)

  if contents["json"].nil?
    return SuccessFact.new(channel: channel, event: event) if contents["ok"]
    return ErrorFact.new(channel: channel, event: event)   if contents["err"]
    return EmptyFact.new(channel: channel, event: event)
  end

  if contents["json"]["graph-update"]
    return AddGraphFact.new(channel: channel, event: event)      if contents["json"]["graph-update"]["add-graph"]
    return AddNodesFact.new(channel: channel, event: event)      if contents["json"]["graph-update"]["add-nodes"]
    return RemoveGraphFact.new(channel: channel, event: event)   if contents["json"]["graph-update"]["remove-graph"]
  end

  if (c = contents["json"]["groupUpdate"])
    return AddGroupFact.new(channel: channel, event: event)          if c["addGroup"]
    return AddGroupMemberFact.new(channel: channel, event: event)    if c["addMembers"]
    return AddTagFact.new(channel: channel, event: event)            if c["addTag"]
    return InitialGroupFact.new(channel: channel, event: event)      if c["initial"]
    return InitialGroupGroupFact.new(channel: channel, event: event) if c["initialGroup"]
    return RemoveGroupMemberFact.new(channel: channel, event: event) if c["removeMembers"]
    return RemoveTagFact.new(channel: channel, event: event)         if c["removeTag"]
  end

  if (c = contents["json"]["metadata-update"])
    return MetadataUpdateFact.new(channel: channel, event: event) if contents["json"]["metadata-update"]
  end

  if (c = contents["json"]["settings-event"])
    return SettingsEventDelBucketFact.new(channel: channel, event: event) if c["del-bucket"]
    return SettingsEventDelEntryFact.new(channel: channel, event: event)  if c["del-entry"]
    return SettingsEventPutBucketFact.new(channel: channel, event: event) if c["put-bucket"]
    return SettingsEventPutEntryFact.new(channel: channel, event: event)  if c["put-entry"]
  end

  return BaseFact.new(channel: channel, event: event)
end