etherpad-lite 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ ** RELEASE 0.2.3 (2013-04-11) **
2
+
3
+ * Add support for Chat API
4
+ * Default to the latest supported API version on the server
5
+
1
6
  ** RELEASE 0.2.2 (2013-01-20) **
2
7
 
3
8
  * Support for Etherpad Lite API v1.2.1
@@ -27,7 +27,7 @@ module EtherpadLite
27
27
  # client = EtherpadLite.client('https://etherpad.yoursite.com', 'your api key', '1.1')
28
28
  #
29
29
  # client = EtherpadLite.client(9001, 'your api key', '1.1') # Alias to http://localhost:9001
30
- def self.client(url_or_port, api_key_or_file, api_version=1)
30
+ def self.client(url_or_port, api_key_or_file, api_version=nil)
31
31
  Client.new(url_or_port, api_key_or_file, api_version)
32
32
  end
33
33
 
@@ -41,31 +41,37 @@ module EtherpadLite
41
41
  attr_reader :api_version
42
42
 
43
43
  # Instantiate a new Etherpad Lite Client. You may pass a full url or just a port number. The api key may be a string
44
- # or a File object. If you do not specify an API version, it will default to the latest version that is supported.
45
- def initialize(url_or_port, api_key_or_file, api_version=1)
44
+ # or a File object. If you do not specify an API version, it will default to the latest version.
45
+ def initialize(url_or_port, api_key_or_file, api_version=nil)
46
46
  url_or_port = "http://localhost:#{url_or_port}" if url_or_port.is_a? Integer
47
47
  @uri = URI.parse(url_or_port)
48
48
  @api_key = api_key_or_file.is_a?(IO) ? api_key_or_file.read : api_key_or_file
49
- @api_version = api_version.to_s
49
+ @api_version = api_version ? api_version.to_s : current_api_version.to_s
50
50
  end
51
51
 
52
52
  # Call an API method
53
53
  def method_missing(method, params={})
54
- request = method =~ /^(set|create|delete)/ \
55
- ? ->(url, params) { RestClient.post(url, params) } \
56
- : ->(url, params) { RestClient.get(url, :params => params) }
57
- call(method, params, &request)
54
+ call(method, params)
58
55
  end
59
56
 
60
57
  private
61
58
 
59
+ # Returns the latest api version. Defaults to "1" if anything goes wrong.
60
+ def current_api_version
61
+ JSON.parse(get('/api').to_s)['currentVersion'] rescue 1
62
+ end
63
+
62
64
  # Calls the EtherpadLite API and returns the :data portion of the response Hash.
63
65
  # If the API response contains an error code, an exception is raised.
64
66
  def call(api_method, params={}, &request)
65
- params[:apikey] = @api_key
66
- url = [@uri.to_s, 'api', self.api_version, api_method].compact.join('/')
67
- json = request.(url, params).to_s
68
- response = JSON.parse(json, :symbolize_names => true)
67
+ path = "/api/#{api_version}/#{api_method}"
68
+
69
+ begin
70
+ result = api_method =~ /^(set|create|delete)/ ? post(path, params) : get(path, params)
71
+ response = JSON.parse(result.to_s, :symbolize_names => true)
72
+ rescue JSON::ParserError => e
73
+ raise Error, "Unable to parse JSON response: #{json}"
74
+ end
69
75
 
70
76
  case response[:code]
71
77
  when 0 then response[:data]
@@ -73,5 +79,17 @@ module EtherpadLite
73
79
  else raise Error, "An unknown error ocurrced while handling the API response: #{response.to_s}"
74
80
  end
75
81
  end
82
+
83
+ # Makes a GET request
84
+ def get(path, params={})
85
+ params[:apikey] = self.api_key
86
+ RestClient.get("#{self.uri}#{path}", :params => params)
87
+ end
88
+
89
+ # Makes a POST request
90
+ def post(path, params={})
91
+ params[:apikey] = self.api_key
92
+ RestClient.post("#{self.uri}#{path}", params)
93
+ end
76
94
  end
77
95
  end
@@ -1,6 +1,7 @@
1
1
  require 'etherpad-lite/models/padded'
2
2
  require 'etherpad-lite/models/instance'
3
3
  require 'etherpad-lite/models/pad'
4
+ require 'etherpad-lite/models/chat_message'
4
5
  require 'etherpad-lite/models/group'
5
6
  require 'etherpad-lite/models/author'
6
7
  require 'etherpad-lite/models/session'
@@ -0,0 +1,61 @@
1
+ module EtherpadLite
2
+ # An Etherpad Lite Chat message
3
+ class ChatMessage
4
+ # The EtherpadLite::Instance object
5
+ attr_reader :instance
6
+ # The pad id
7
+ attr_reader :pad_id
8
+ # The message text
9
+ attr_reader :text
10
+ # User/Author id
11
+ attr_reader :user_id
12
+ # Unix timestamp
13
+ attr_reader :timestamp
14
+ # User/Author name
15
+ attr_reader :user_name
16
+
17
+ # Instantiate a ChatMessage
18
+ def initialize(instance, attrs)
19
+ @instance = instance
20
+ @pad_id = attrs[:padID]
21
+ @text = attrs[:text]
22
+ @user_id = attrs[:userId]
23
+ @timestamp = attrs[:time] / 1000 if attrs[:time]
24
+ @user_name = attrs[:userName]
25
+ end
26
+
27
+ # Returns this message's Pad
28
+ def pad
29
+ if pad_id
30
+ @pad ||= Pad.new(instance, pad_id)
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ # Returns the Author that sent this message
37
+ def author
38
+ if user_id
39
+ @author ||= Author.new(instance, user_id)
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
45
+ alias_method :user, :author
46
+
47
+ # Time object
48
+ def time
49
+ if timestamp
50
+ @time ||= Time.at(timestamp)
51
+ else
52
+ nil
53
+ end
54
+ end
55
+
56
+ # Returns the message text
57
+ def to_s
58
+ text.to_s
59
+ end
60
+ end
61
+ end
@@ -4,7 +4,7 @@ module EtherpadLite
4
4
  # ether = EtherpadLite.client('https://etherpad.yoursite.com', 'your api key', '1.1')
5
5
  #
6
6
  # ether = EtherpadLite.client(9001, 'your api key', '1.1') # Alias to http://localhost:9001
7
- def self.connect(url_or_port, api_key_or_file, api_version=1)
7
+ def self.connect(url_or_port, api_key_or_file, api_version=nil)
8
8
  Instance.new(url_or_port, api_key_or_file, api_version)
9
9
  end
10
10
 
@@ -16,7 +16,7 @@ module EtherpadLite
16
16
 
17
17
  # Instantiate a new Etherpad Lite Instance. You may pass a full url or just a port number. The api key may be a string
18
18
  # or a File object.
19
- def initialize(url_or_port, api_key_or_file, api_version=1)
19
+ def initialize(url_or_port, api_key_or_file, api_version=nil)
20
20
  @client = Client.new(url_or_port, api_key_or_file, api_version)
21
21
  end
22
22
 
@@ -152,6 +152,20 @@ module EtherpadLite
152
152
  author_ids.map { |id| Author.new(@instance, id) }
153
153
  end
154
154
 
155
+ # Returns an array of chat message Hashes
156
+ def chat_messages(start_index=nil, end_index=nil)
157
+ messages = @instance.client.getChatHistory(:padID => @id, :start => start_index, :end => end_index)[:messages]
158
+ messages.map do |msg|
159
+ attrs = {padID: @id}.merge(msg)
160
+ ChatMessage.new(@instance, attrs)
161
+ end
162
+ end
163
+
164
+ # Returns the number of chat messages
165
+ def chat_size
166
+ @instance.client.getChatHead(padID: @id)[:chatHead] + 1
167
+ end
168
+
155
169
  # Returns true if this is a public Pad (opposite of private?).
156
170
  # This only applies to Pads belonging to a Group.
157
171
  def public?
@@ -1,4 +1,4 @@
1
1
  module EtherpadLite
2
2
  # The library version
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etherpad-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-20 00:00:00.000000000 Z
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -34,16 +34,17 @@ extensions: []
34
34
  extra_rdoc_files:
35
35
  - README.rdoc
36
36
  files:
37
- - lib/etherpad-lite.rb
38
- - lib/etherpad-lite/models/padded.rb
37
+ - lib/etherpad-lite/models/author.rb
38
+ - lib/etherpad-lite/models/session.rb
39
39
  - lib/etherpad-lite/models/group.rb
40
40
  - lib/etherpad-lite/models/pad.rb
41
- - lib/etherpad-lite/models/author.rb
41
+ - lib/etherpad-lite/models/chat_message.rb
42
42
  - lib/etherpad-lite/models/instance.rb
43
- - lib/etherpad-lite/models/session.rb
43
+ - lib/etherpad-lite/models/padded.rb
44
+ - lib/etherpad-lite/models.rb
44
45
  - lib/etherpad-lite/client.rb
45
46
  - lib/etherpad-lite/version.rb
46
- - lib/etherpad-lite/models.rb
47
+ - lib/etherpad-lite.rb
47
48
  - README.rdoc
48
49
  - CHANGELOG
49
50
  - LICENSE