Class: Convore::Client
- Inherits:
-
Object
- Object
- Convore::Client
- Defined in:
- lib/convore/client.rb
Instance Attribute Summary collapse
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#password ⇒ Object
Returns the value of attribute password.
-
#stream ⇒ Object
Returns the value of attribute stream.
-
#thread ⇒ Object
Returns the value of attribute thread.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #listen ⇒ Object
- #poll(wait = 0.05) ⇒ Object
- #process_response(response) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
6 7 8 9 |
# File 'lib/convore/client.rb', line 6 def initialize @stream ||= [] @ts ||= Time.now.to_f end |
Instance Attribute Details
#cursor ⇒ Object
Returns the value of attribute cursor.
4 5 6 |
# File 'lib/convore/client.rb', line 4 def cursor @cursor end |
#password ⇒ Object
Returns the value of attribute password.
4 5 6 |
# File 'lib/convore/client.rb', line 4 def password @password end |
#stream ⇒ Object
Returns the value of attribute stream.
4 5 6 |
# File 'lib/convore/client.rb', line 4 def stream @stream end |
#thread ⇒ Object
Returns the value of attribute thread.
4 5 6 |
# File 'lib/convore/client.rb', line 4 def thread @thread end |
#username ⇒ Object
Returns the value of attribute username.
4 5 6 |
# File 'lib/convore/client.rb', line 4 def username @username end |
Instance Method Details
#listen ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/convore/client.rb', line 11 def listen raise Exception.new("username and password need to be set to listen to /live") if !username || !password @thread = Thread.fork { Net::HTTP.start('convore.com', 443, nil, nil, nil, nil, {:use_ssl => true}) {|http| req = Net::HTTP::Get.new("/api/live.json?cursor=#{@cursor if @cursor}") req.basic_auth(username, password) req.set_content_type('application/json') response = http.request(req) Thread.current[:response] = response.body } } end |
#poll(wait = 0.05) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/convore/client.rb', line 47 def poll(wait = 0.05) if t = @thread.join(wait) listen unless @thread.alive? process_response(t[:response]) end end |
#process_response(response) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/convore/client.rb', line 26 def process_response(response) json = JSON.parse(response) if json['messages'] json['messages'].each {|msg| if msg['_ts'] && @ts < msg['_ts'] case msg['kind'] when 'message' then stream.unshift(Message.from_json(msg)) when 'topic' then stream.unshift(Topic.from_json(msg)) when 'star', 'unstar' then stream.unshift(Star.from_json(msg)) end @ts = msg['_ts'] if msg['_ts'] @cursor = msg['_id'] if msg['_id'] end } end end |