Class: Flowpi::Server
- Inherits:
-
Object
- Object
- Flowpi::Server
- Defined in:
- lib/flowpi.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #handle_line(line) ⇒ Object
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Server
Returns a new instance of Server.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/flowpi.rb', line 9 def initialize( = {}) = if [:log_file].nil? || [:log_file].empty? @logger = ::Logger.new(STDOUT) else @logger = ::Logger.new([:log_file]) end @logger.level = ::Logger.const_get(.fetch(:log_level, 'WARN')) @logger.datetime_format = .fetch(:log_date_format, '%Y-%m-%d %H:%M:%S') trap "SIGINT" do exit 130 end end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/flowpi.rb', line 7 def logger @logger end |
Instance Method Details
#handle_line(line) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/flowpi.rb', line 42 def handle_line(line) = Flowpi::Message.new.parse(line) if .has_content? if .content.match([:message_matcher]) logger.info { "Speaking message: #{message.content}" } %x(espeak "#{message.content}" --stdout 2>/dev/null | aplay -D 'default') else logger.debug { "Ignoring message: #{message.content}" } end end end |
#run ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/flowpi.rb', line 24 def run logger.info "Starting Flowpi..." http = EM::HttpRequest.new( "https://stream.flowdock.com/flows?filter=#{@options[:filter]}", :keepalive => true, :connect_timeout => 0, :inactivity_timeout => 0) EventMachine.run do s = http.get(:head => { 'Authorization' => [[:token], ''], 'accept' => 'application/json'}) buffer = "" s.stream do |chunk| buffer << chunk while line = buffer.slice!(/.+\r\n/) handle_line(line) end end end end |