Class: Urbit::Groups

Inherits:
Set
  • Object
show all
Defined in:
lib/urbit/groups.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ship:) ⇒ Groups

Returns a new instance of Groups.



7
8
9
10
# File 'lib/urbit/groups.rb', line 7

def initialize(ship:)
  @ship   = ship
  @hash = {}
end

Instance Attribute Details

#shipObject

Returns the value of attribute ship.



5
6
7
# File 'lib/urbit/groups.rb', line 5

def ship
  @ship
end

Instance Method Details

#[](path: nil, title: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/urbit/groups.rb', line 12

def [](path: nil, title: nil)
  if title.nil?
    if (g = self.select {|g| g.path == path}.first)
      g.manager = self
      return g
    end
  else
    if (g = self.select {|g| g.title == title}.first)
      g.manager = self
      return g
    end
  end
  nil
end

#add(a_group) ⇒ Object

Adds a Group to this Manager’s groups collection



30
31
32
33
# File 'lib/urbit/groups.rb', line 30

def add(a_group)
  a_group.manager = self
  self << a_group
end

#add_members(group_path:, ships:) ⇒ Object



35
36
37
38
39
# File 'lib/urbit/groups.rb', line 35

def add_members(group_path:, ships:)
  if (group = self[path: group_path])
    group.members += ships
  end
end

#add_tag(group_path:, ships:, tag:) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/urbit/groups.rb', line 41

def add_tag(group_path:, ships:, tag:)
  if (group = self[path: group_path])
    if (group.tags.include? tag)
      group.tags[tag] += ships
    end
  end
end

#create(name:, title:, description:) ⇒ Object



49
50
51
# File 'lib/urbit/groups.rb', line 49

def create(name:, title:, description:)
  self.spider('group-create', %Q({"create": {"name": "#{name}", "title": "#{title}", "description": "#{description}", "policy": {"open": {"banRanks": [], "banned": []}}}}))
end

#empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/urbit/groups.rb', line 53

def empty?
  self.empty?
end

#join(host:, name:, share_contact: false, auto_join: false) ⇒ Object



57
58
59
60
61
# File 'lib/urbit/groups.rb', line 57

def join(host:, name:, share_contact: false, auto_join: false)
  data = {join: {resource: {ship: "#{host}", name: "#{name}"}, ship: "#{host}", shareContact: share_contact, app: "groups", autojoin: auto_join}}
  self.ship.poke(app: 'group-view', mark: 'group-view-action', message: data)
  nil
end

#listObject



63
64
65
# File 'lib/urbit/groups.rb', line 63

def list
  self.map {|g| g.to_list}.join("\n")
end

#loadObject



85
86
87
88
89
90
# File 'lib/urbit/groups.rb', line 85

def load
  if self.ship.logged_in?
    self.ship.subscribe(app: 'group-store', path: '/groups')
  end
  nil
end

#remove(group) ⇒ Object



67
68
69
# File 'lib/urbit/groups.rb', line 67

def remove(group)
  self.delete(group)
end

#remove_members(group_path:, ships:) ⇒ Object



71
72
73
74
75
# File 'lib/urbit/groups.rb', line 71

def remove_members(group_path:, ships:)
  if (group = self[path: group_path])
    group.members -= ships
  end
end

#remove_tag(group_path:, ships:, tag:) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/urbit/groups.rb', line 77

def remove_tag(group_path:, ships:, tag:)
  if (group = self[path: group_path])
    if (group.tags.include? tag)
      group.tags[tag] -= ships
    end
  end
end

#spider(thread, data) ⇒ Object



92
93
94
# File 'lib/urbit/groups.rb', line 92

def spider(thread, data)
  self.ship.spider(mark_in: 'group-view-action', mark_out: 'json', thread: thread, data: data)
end

#to_sObject



96
97
98
# File 'lib/urbit/groups.rb', line 96

def to_s
  self.sort.each {|g| puts g}
end