Class: EtherpadLite::Author

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

Overview

An Author of Pad content

Authors are used to create a Session in a Group. Authors may be created with a name and a mapper. A mapper is usually an identifier stored in your third-party system, like a foreign key or username.

Author Examples:

@ether = EtherpadLite.connect(9001, 'api key')

# Create a new author with both a name and a mapper
author1 = @ether.create_author(:name => 'Theodor Seuss Geisel', :mapper => 'author_1')

# Load (and create, if necessary) a mapped author with a name
author2 = @ether.author('author_2', :name => 'Richard Bachman')

# Load (and create, if necessary) a author by mapper
author3 = @ether.author('author_3')

# Load author1 by id
author4 = @ether.get_author(author1.id)

Session examples:

# Create two hour-long session for author 1 in two different groups
group1 = @ether.group('my awesome group')
group2 = @ether.group('my other awesome group')

session1 = author1.create_session(group1, 60)
session2 = author1.create_session(group2, 60)

Attribute examples:

author1.name #> "Theodor Seuss Geisel"

author1.mapper #> "author_1"

author2.sessions #> [#<EtherpadLite::Session>, #<EtherpadLite::Session>]

author2.session_ids.include? session1.id #> true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Instantiates an Author object (presumed it already exists)

Options:

mapper => the foreign author id it’s mapped to

name => the Author’s name



74
75
76
77
78
# File 'lib/etherpad-lite/models/author.rb', line 74

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

Instance Attribute Details

#idObject (readonly)

The author’s id



47
48
49
# File 'lib/etherpad-lite/models/author.rb', line 47

def id
  @id
end

#instanceObject (readonly)

The EtherpadLite::Instance object



45
46
47
# File 'lib/etherpad-lite/models/author.rb', line 45

def instance
  @instance
end

#mapperObject (readonly)

The author’s foreign mapper (if any)



49
50
51
# File 'lib/etherpad-lite/models/author.rb', line 49

def mapper
  @mapper
end

Class Method Details

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

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

Options:

mapper => uid of Author from another system

name => Author’s name



60
61
62
63
64
65
# File 'lib/etherpad-lite/models/author.rb', line 60

def self.create(instance, options={})
  result = options[:mapper] \
    ? instance.client.createAuthorIfNotExistsFor(authorMapper: options[:mapper], name: options[:name]) \
    : instance.client.createAuthor(options)
  new instance, result[:authorID], options
end

Instance Method Details

#create_session(group, length_in_min) ⇒ Object

Create a new session for group that will last length_in_minutes.



96
97
98
# File 'lib/etherpad-lite/models/author.rb', line 96

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

#nameObject

Returns the author’s name



81
82
83
# File 'lib/etherpad-lite/models/author.rb', line 81

def name
  @name ||= @instance.client.getAuthorName(authorID: @id)
end

#pad_idsObject

Returns an array of pad ids that this author has edited



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

def pad_ids
  @instance.client.listPadsOfAuthor(authorID: @id)[:padIDs] || []
end

#padsObject

Returns an array of Pads that this author has edited



91
92
93
# File 'lib/etherpad-lite/models/author.rb', line 91

def pads
  pad_ids.map { |id| Pad.new(@instance, id) }
end

#session_idsObject

Returns all session ids from this Author



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

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

#sessionsObject

Returns all sessions from this Author



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

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