Class: TopNotify::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/top_notify/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(consumer = Consumer.new) ⇒ Server

Returns a new instance of Server.



9
10
11
12
# File 'lib/top_notify/server.rb', line 9

def initialize(consumer = Consumer.new)
  @conn = Connection.new
  @consumer = consumer
end

Instance Method Details

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/top_notify/server.rb', line 14

def start
  Net::HTTP.start(@conn.uri.host, @conn.uri.port) do |http|
    http.request @conn.request do |response|
      if response.code.to_i == 400
        raise ConfigurationError.new('Your configuration is not correct, please check it.')
      end
      response.read_body do |chunk|
        data = MultiJson.load(chunk.strip)['packet']
        result = TYPE[data['code'].to_i] || TYPE[0]
        logger.log_stream(data, result[:msg])
        @consumer.send(result[:method], data) if @consumer.respond_to?(result[:method])
      end
    end
  end
end