Class: EtherpadLite::Pad

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

Overview

An Etherpad Lite Pad

This class allows you to interact with pads stored in an Etherpad Lite server. The README has some basic examples.

Note that some functions are restricted to Group pads.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Instantiate a Pad. It is presumed to already exist (via Pad.create).

Options:

groupID => a group id

group => an EtherpadLite::Group object

rev => a pad revision number



49
50
51
52
53
54
55
56
57
58
# File 'lib/etherpad-lite/models/pad.rb', line 49

def initialize(instance, id, options={})
  @instance = instance
  @id = id.to_s
  if options[:groupID]
    @group_id = options[:groupID]
    @id = "#{@group_id}$#{@id}" unless @id =~ Group::GROUP_ID_REGEX
  end
  @group = options[:group]
  @rev = options[:rev]
end

Instance Attribute Details

#idObject (readonly)

The pad id



13
14
15
# File 'lib/etherpad-lite/models/pad.rb', line 13

def id
  @id
end

#instanceObject (readonly)

The EtherpadLite::Instance object



11
12
13
# File 'lib/etherpad-lite/models/pad.rb', line 11

def instance
  @instance
end

#revObject (readonly)

An optional pad revision number



15
16
17
# File 'lib/etherpad-lite/models/pad.rb', line 15

def rev
  @rev
end

Class Method Details

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

Creates and returns a new Pad.

Options:

text => ‘initial Pad text’

groupID => group id of Group new Pad should belong to



24
25
26
27
28
29
30
31
32
33
# File 'lib/etherpad-lite/models/pad.rb', line 24

def self.create(instance, id, options={})
  if options[:groupID]
    group = Group.new instance, options[:groupID]
    instance.client.createGroupPad(groupID: group.id, padName: degroupify_pad_id(id), text: options[:text])
  else
    group = nil
    instance.client.createPad(padID: id, text: options[:text])
  end
  new instance, id, groupID: options[:groupID], group: group
end

.degroupify_pad_id(pad_id) ⇒ Object

Remove the group id portion of a Group Pad’s id



36
37
38
# File 'lib/etherpad-lite/models/pad.rb', line 36

def self.degroupify_pad_id(pad_id)
  pad_id.to_s.sub(Group::GROUP_ID_REGEX, '').sub(/^\$/, '')
end

Instance Method Details

#author_idsObject

Returns an array of ids of authors who’ve edited this pad



156
157
158
# File 'lib/etherpad-lite/models/pad.rb', line 156

def author_ids
  @instance.client.listAuthorsOfPad(padID: @id)[:authorIDs] || []
end

#authorsObject

Returns an array of Authors who’ve edited this pad



161
162
163
# File 'lib/etherpad-lite/models/pad.rb', line 161

def authors
  author_ids.map { |id| Author.new(@instance, id) }
end

#changeset(rev) ⇒ Object

Returns the changeset for the given revision



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

def changeset(rev)
  @instance.client.getRevisionChangeset(padID: @id, rev: rev)
end

#chat_messages(start_index = nil, end_index = nil) ⇒ Object

Returns an array of chat message Hashes



166
167
168
169
170
171
172
# File 'lib/etherpad-lite/models/pad.rb', line 166

def chat_messages(start_index=nil, end_index=nil)
  messages = @instance.client.getChatHistory(:padID => @id, :start => start_index, :end => end_index)[:messages]
  messages.map do |msg|
    attrs = {padID: @id}.merge(msg)
    ChatMessage.new(@instance, attrs)
  end
end

#chat_sizeObject

Returns the number of chat messages



175
176
177
# File 'lib/etherpad-lite/models/pad.rb', line 175

def chat_size
  @instance.client.getChatHead(padID: @id)[:chatHead] + 1
end

#deleteObject

Deletes the Pad



221
222
223
# File 'lib/etherpad-lite/models/pad.rb', line 221

def delete
  @instance.client.deletePad(padID: @id)
end

#diff(start_rev, end_rev = nil) ⇒ Object

Returns a Diff. If end_rev is not specified, the latest revision will be used



125
126
127
# File 'lib/etherpad-lite/models/pad.rb', line 125

def diff(start_rev, end_rev=nil)
  Diff.new(self, start_rev, end_rev)
end

#groupObject

Returns this Pad’s group, if any



76
77
78
79
# File 'lib/etherpad-lite/models/pad.rb', line 76

def group
  return nil unless group_id
  @group ||= Group.new(@instance, group_id)
end

#group_idObject

Returns the group_id of this Pad, if any



67
68
69
70
71
72
73
# File 'lib/etherpad-lite/models/pad.rb', line 67

def group_id
  unless @group_id
    match = Group::GROUP_ID_REGEX.match(@id)
    @group_id = match ? match[0] : nil
  end
  @group_id
end

#html(options = {}) ⇒ Object

Returns the Pad’s text as HTML. Unless you specified a :rev when instantiating the Pad, or specify one here, this will return the latest revision.

Options:

rev => revision_number



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

def html(options={})
  options[:padID] = @id
  options[:rev] ||= @rev unless @rev.nil?
  @instance.client.getHTML(options)[:html]
end

#html=(html) ⇒ Object

Writes HTML to the Pad. There is no ‘save’ method; it is written immediately.



109
110
111
# File 'lib/etherpad-lite/models/pad.rb', line 109

def html=(html)
  @instance.client.setHTML(padID: @id, html: html)
end

#last_editedObject

Returns the time the pad was last edited as a Unix timestamp



151
152
153
# File 'lib/etherpad-lite/models/pad.rb', line 151

def last_edited
  @instance.client.getLastEdited(padID: @id)[:lastEdited]
end

#message(msg) ⇒ Object

Sends a custom message of type msg to the pad.



216
217
218
# File 'lib/etherpad-lite/models/pad.rb', line 216

def message(msg)
  @instance.client.sendClientsMessage(padID: @id, msg: msg)
end

#nameObject

Returns the name of the Pad. For a normal pad, this is the same as it’s id. But for a Group Pad, this strips away the group id part of the pad id.



62
63
64
# File 'lib/etherpad-lite/models/pad.rb', line 62

def name
  @name ||= self.class.degroupify_pad_id(@id)
end

#password=(new_password) ⇒ Object

Sets the Pad’s password. This only applies to Pads belonging to a Group.



211
212
213
# File 'lib/etherpad-lite/models/pad.rb', line 211

def password=(new_password)
  @instance.client.setPassword(padID: @id, password: new_password)
end

#password?Boolean

Returns true if this Pad has a password, false if not. This only applies to Pads belonging to a Group.

Returns:

  • (Boolean)


205
206
207
# File 'lib/etherpad-lite/models/pad.rb', line 205

def password?
  @instance.client.isPasswordProtected(padID: @id)[:isPasswordProtected]
end

#private=(status) ⇒ Object

Set the pad’s private status to true or false (opposite of public=) This only applies to Pads belonging to a Group.



199
200
201
# File 'lib/etherpad-lite/models/pad.rb', line 199

def private=(status)
  self.public = !status
end

#private?Boolean

Returns true if this is a private Pad (opposite of public?) This only applies to Pads belonging to a Group.

Returns:

  • (Boolean)


193
194
195
# File 'lib/etherpad-lite/models/pad.rb', line 193

def private?
  not self.public?
end

#public=(status) ⇒ Object

Set the pad’s public status to true or false (opposite of private=) This only applies to Pads belonging to a Group.



187
188
189
# File 'lib/etherpad-lite/models/pad.rb', line 187

def public=(status)
  @instance.client.setPublicStatus(padID: @id, publicStatus: status)
end

#public?Boolean

Returns true if this is a public Pad (opposite of private?). This only applies to Pads belonging to a Group.

Returns:

  • (Boolean)


181
182
183
# File 'lib/etherpad-lite/models/pad.rb', line 181

def public?
  @instance.client.getPublicStatus(padID: @id)[:publicStatus]
end

#read_only_idObject

Returns the Pad’s read-only id. This is cached.



146
147
148
# File 'lib/etherpad-lite/models/pad.rb', line 146

def read_only_id
  @read_only_id ||= @instance.client.getReadOnlyID(padID: @id)[:readOnlyID]
end

#revision_numbersObject

Returns an Array of all this Pad’s revision numbers



114
115
116
117
# File 'lib/etherpad-lite/models/pad.rb', line 114

def revision_numbers
  max = @instance.client.getRevisionsCount(padID: @id)[:revisions]
  (0..max).to_a
end

#revisionsObject

Returns an array of Pad objects, each with an increasing revision of the text.



120
121
122
# File 'lib/etherpad-lite/models/pad.rb', line 120

def revisions
  revision_numbers.map { |n| Pad.new(@instance, @id, :rev => n) }
end

#text(options = {}) ⇒ Object

Returns the Pad’s text. Unless you specified a :rev when instantiating the Pad, or specify one here, this will return the latest revision.

Options:

rev => revision_number



86
87
88
89
90
# File 'lib/etherpad-lite/models/pad.rb', line 86

def text(options={})
  options[:padID] = @id
  options[:rev] ||= @rev unless @rev.nil?
  @instance.client.getText(options)[:text]
end

#text=(txt) ⇒ Object

Writes txt to the Pad. There is no ‘save’ method; it is written immediately.



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

def text=(txt)
  @instance.client.setText(padID: @id, text: txt)
end

#user_countObject Also known as: users_count

Returns the number of users currently editing a pad



140
141
142
# File 'lib/etherpad-lite/models/pad.rb', line 140

def user_count
  @instance.client.padUsersCount(padID: @id)[:padUsersCount]
end

#usersObject

Returns an array of users hashes, representing users currently using the pad



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

def users
  @instance.client.padUsers(padID: @id)[:padUsers]
end