Class: EtherpadLite::Group

Inherits:
Object
  • Object
show all
Includes:
Padded
Defined in:
lib/etherpad-lite/models/group.rb

Overview

A Group serves as a container for related pads. Only an Author with a Session can access a group Pad.

Group examples:

# Create a new group
group1 = @ether.create_group
# Etherpad Lite will assign it an internal id
group.id #> 'g.asdflsadf7w9823kjlasdf' 

# Create a new group with a mapper, so it can be easily found again
group2 = @ether.create_group :mapper => 'Blurg'

# Load (or create, if it doesn't exist) a group mapped to "Flarb"
group3 = @ether.group('Flarb')

# Load an existing group based on its internal id
group4 = @ether.get_group('g.823lasdlfj98asdfj')

Group pad examples:

# Create a new pad in this group, optionally specifying its initial text
pad1 = group1.create_pad('group 1 pad', :text => 'Words!')

# Load (or create, if it doesn't exist) a pad in this group
pad2 = group2.pad('group 2 pad')

# Load an existing pad from group 2
pad3 = group2.get_pad('important pad')

Session examples:

# Create two hour-long session for an author in group 1
author = @ether.author('author_1')
session = group1.create_session(author, 60)

Understand how ids work. A group pad’s id is the group_id + ‘$’ + pad_name:

pad2.group_id == group2.id #> true

pad2.id == group2.id + '$' + pad2.name #> true

pad2 == group2.pad('group 2 pad') == @ether.get_pad("#{group2.id}$group 2 pad") == @ether.get_pad('group 2 pad', :groupID => group2.id) #> true

group2.mapper #> "Blurg"

Constant Summary collapse

GROUP_ID_REGEX =
/^g\.[^\$]+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, id, options = {}) ⇒ Group

Instantiates a Group object (presumed it already exists)

Options:

mapper => the foreign id it’s mapped to



77
78
79
80
81
# File 'lib/etherpad-lite/models/group.rb', line 77

def initialize(instance, id, options={})
  @instance = instance
  @id = id
  @mapper = options[:mapper]
end

Instance Attribute Details

#idObject (readonly)

The group id



54
55
56
# File 'lib/etherpad-lite/models/group.rb', line 54

def id
  @id
end

#instanceObject (readonly)

The EtherpadLite::Instance object



52
53
54
# File 'lib/etherpad-lite/models/group.rb', line 52

def instance
  @instance
end

#mapperObject (readonly)

An optional identifier used to map the group to something outside Etherpad Lite



56
57
58
# File 'lib/etherpad-lite/models/group.rb', line 56

def mapper
  @mapper
end

Class Method Details

.create(instance, options = {}) ⇒ Object

Creates a new Group. Optionally, you may pass the :mapper option your third party system’s group id. This will allow you to find your Group again later using the same identifier as your foreign system. If you pass the mapper option, the method behaves like “create group for <mapper> if it doesn’t already exist”.

Options:

mapper => your foreign group id



65
66
67
68
69
70
# File 'lib/etherpad-lite/models/group.rb', line 65

def self.create(instance, options={})
  result = options[:mapper] \
    ? instance.client.createGroupIfNotExistsFor(groupMapper: options[:mapper]) \
    : instance.client.createGroup
  new instance, result[:groupID], options
end

Instance Method Details

#create_pad(id, options = {}) ⇒ Object

Creates and returns a Pad with the given id.

Options:

text => ‘initial Pad text’



102
103
104
105
# File 'lib/etherpad-lite/models/group.rb', line 102

def create_pad(id, options={})
  options[:groupID] = @id
  super groupify_pad_id(id), options
end

#create_session(author, length_in_min) ⇒ Object

Create a new session for author that will last length_in_minutes.



118
119
120
# File 'lib/etherpad-lite/models/group.rb', line 118

def create_session(author, length_in_min)
  Session.create(@instance, @id, author.id, length_in_min)
end

#deleteObject

Deletes the Group



135
136
137
# File 'lib/etherpad-lite/models/group.rb', line 135

def delete
  @instance.client.deleteGroup(groupID: @id)
end

#get_pad(id, options = {}) ⇒ Object

Returns the Pad with the given id (presumed to already exist). Use this instead of Group#pad when you know the Pad already exists; it will save an HTTP request.



92
93
94
95
# File 'lib/etherpad-lite/models/group.rb', line 92

def get_pad(id, options={})
  options[:group] = self
  super groupify_pad_id(id), options
end

#pad(id, options = {}) ⇒ Object

Returns the Pad with the given id, creating it if it doesn’t already exist. This requires an HTTP request, so if you know the Pad already exists, use Group#get_pad instead.



85
86
87
88
# File 'lib/etherpad-lite/models/group.rb', line 85

def pad(id, options={})
  options[:groupID] = @id
  super groupify_pad_id(id), options
end

#pad_idsObject

Returns an array of all the Pad ids in this Group.



113
114
115
# File 'lib/etherpad-lite/models/group.rb', line 113

def pad_ids
  @instance.client.listPads(groupID: @id)[:padIDs]
end

#padsObject

Returns an array of all the Pads in this Group.



108
109
110
# File 'lib/etherpad-lite/models/group.rb', line 108

def pads
  pad_ids.map { |id| Pad.new(@instance, id, :group => self) }
end

#session_idsObject

Returns all session ids in this Group



123
124
125
126
# File 'lib/etherpad-lite/models/group.rb', line 123

def session_ids
  s = @instance.client.listSessionsOfGroup(groupID: @id) || {}
  s.keys
end

#sessionsObject

Returns all sessions in this Group



129
130
131
132
# File 'lib/etherpad-lite/models/group.rb', line 129

def sessions
  s = @instance.client.listSessionsOfGroup(groupID: @id) || {}
  s.map { |id,info| Session.new(@instance, id, info) }
end