Class: UCEngine
- Inherits:
-
Object
- Object
- UCEngine
- Defined in:
- lib/ucengine.rb
Overview
This class is the main and only class in ucengine.rb, it handles connections and request to the UCEngine server.
uce = UCEngine.new("localhost", 4567)
uce.connect("bibi", :password => 'abcd') do |uce|
uce.subscribe(["af83"], :type => 'chat.message.new', :search => 'HTML5') do |event|
uce.push(:location => [event['org'], event['meeting']]
:from => 'bot',
:type => 'chat.message.new',
:metadata => {"text" => "Hey, you were talking about HTML5"})
end
end
Constant Summary collapse
- DEBUG =
Print every request and everything above.
0
- WARNING =
Print everything that seems fishy.
1
- ERROR =
Print regular errors, usually HTTP errors.
2
- CRITICAL =
Only print critical errors (bad hostname or port, etc).
3
- QUIET =
Don’t print anything (default).
4
Instance Method Summary collapse
-
#connect(uid, credential) {|_self| ... } ⇒ Object
Connect to the UCEngine server with the User ID ‘uid’ and the its credential.
-
#initialize(host, port, debug = UCEngine::QUIET) ⇒ UCEngine
constructor
Create a new UCEngine object.
-
#publish(event) ⇒ Object
Publish an event.
-
#subscribe(location, params = {}) ⇒ Object
Subscribe to an event stream.
-
#time ⇒ Object
Return the current timestamp from the server.
Constructor Details
#initialize(host, port, debug = UCEngine::QUIET) ⇒ UCEngine
Create a new UCEngine object. ‘host’ is the hostname of the UCEngine server and ‘port’ is to TCP port to connect to. Note that this method doesn’t create a new connection, see the #connect method. An additional ‘debug’ parameter set the debug level of the library, all the debug information are written in the error output.
44 45 46 47 48 49 50 51 |
# File 'lib/ucengine.rb', line 44 def initialize(host, port, debug = UCEngine::QUIET) @host = host @port = port @http = Net::HTTP.new(host, port) @threads = [] @debug = debug debug(UCEngine::DEBUG, "Initialisation complete for #{host}:#{port}.") end |
Instance Method Details
#connect(uid, credential) {|_self| ... } ⇒ Object
Connect to the UCEngine server with the User ID ‘uid’ and the its credential.
uce = UCEngine.new("localhost", 4567)
uce.connect("bibi", :password => 'abcd') do |uce|
... your code goes here
end
It is currently possible to use :token or :password as authentification method.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ucengine.rb', line 62 def connect(uid, credential) @uid = uid response = put("/presence/#{@uid}", {:auth => 'token', :credential => credential[:token]}) @sid = response['result'] debug(UCEngine::DEBUG, "Authentification complete for #{@uid}/#{@sid}.") yield self @threads.each do |thread| thread.join end end |
#publish(event) ⇒ Object
Publish an event. Publishing an event require a few mandatories parameters:
- :location
-
As described in the subscribe method: [“organisation”, “meeting”] publish the event in a specific meeting, [“organisation”] publish the event in the organisation and []: publish the event in the server root.
- :type
-
The type of event to send, the format of this type is usually ‘namespace.object.action’, for example: ‘chat.message.new’, ‘twitter.tweet.new’, ‘internal.user.update’
- :parent
-
The id of the parent, this parameter is useful to build event hierarchy.
- :metadata
-
A hash of freely defined values to append to the event.
uce.publish(:location => ["af83", "WebWorkersCamp"],
:type => 'presentation.slide.add'
:metadata => {:url => 'http://myserver/slides/03.png',
:index => 3})
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ucengine.rb', line 125 def publish(event) debug(UCEngine::DEBUG, "Publish to #{event[:location]}, type: #{event[:type]}, parent: #{event[:parent]}, metadata: #{event[:metadata]}") params = Hash.new params[:type] = event[:type] params[:parent] = event[:parent] if event[:parent] if event[:metadata] event[:metadata].each_key do |key| params["metadata[#{key}]"] = event[:metadata][key] end end put("/event/#{event[:location].join("/")}", params) end |
#subscribe(location, params = {}) ⇒ Object
Subscribe to an event stream. The ‘location’ parameter is where you’re expecting the events to come:
-
[“organisation”, “meeting”]: events from a specific meeting.
-
[“organisation”]: events from all meetings of the organisation and for the organisation itself.
-
[]: all events.
The function takes extra parameters: :type => the type of event (ex. ‘chat.message.new’, ‘internal.user.add’, etc). :from => the origin of the message, the value is an uid. :parent => the id of the the parent event. :search => list of keywords that match the metadata of the returned events
uce.subscribe(["af83"], :type => 'internal.meeting.add', :search => 'HTML5') do |event|
puts "A new meeting about HTML5 was created"
end
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ucengine.rb', line 89 def subscribe(location, params = {}) debug(UCEngine::DEBUG, "Subscribe to #{location} with #{params}.") @threads << Thread.new do Net::HTTP.start(@host, @port) do |http| params[:_async] = "lp" params[:start] = 0 if !params[:start] while true begin events = get("/event/#{location.join("/")}", params, http)['result'] rescue Timeout::Error debug(UCEngine::WARNING, "Subscribe timeout ... retry") retry rescue EOFError debug(UCEngine::WARNING, "Subscribe closed ... retry") sleep 10 retry end events.each do |event| yield event end params[:start] = events[-1]['datetime'] + 1 end end end end |
#time ⇒ Object
Return the current timestamp from the server. The timestamp is expressed in milliseconds from Epoch (january 1st 1970). This function can be useful if you need to search for events from now.
uce.time -> 1240394032
144 145 146 147 148 |
# File 'lib/ucengine.rb', line 144 def time time = get("/time", Hash.new)['result'].to_i debug(UCEngine::DEBUG, "Fecth timestamp from UCEngine: #{time}") return time end |