Class: Convore::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/convore/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

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

#cursorObject

Returns the value of attribute cursor.



4
5
6
# File 'lib/convore/client.rb', line 4

def cursor
  @cursor
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/convore/client.rb', line 4

def password
  @password
end

#streamObject

Returns the value of attribute stream.



4
5
6
# File 'lib/convore/client.rb', line 4

def stream
  @stream
end

#threadObject

Returns the value of attribute thread.



4
5
6
# File 'lib/convore/client.rb', line 4

def thread
  @thread
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/convore/client.rb', line 4

def username
  @username
end

Instance Method Details

#listenObject

Raises:

  • (Exception)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

    http = EM::HttpRequest.new("https://convore.com/api/live.json?cursor=#{@cursor if @cursor}").get :head => {'authorization' => ["#{@username}", "#{@password}"]}

    http.callback {
      begin
        process_response(http.response)
      rescue Error => e
        puts "em-convore client: an error occured - #{e}"
      end
    }

    http.errback  {
    }

end

#process_response(response) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/convore/client.rb', line 29

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