lib/checkcheckit/console.rb in checkcheckit-0.0.4 vs lib/checkcheckit/console.rb in checkcheckit-0.0.5

- old
+ new

@@ -1,14 +1,15 @@ require 'ostruct' class CheckCheckIt::Console attr_accessor :list_dir - attr_accessor :out_stream, :in_stream + attr_accessor :out_stream, :in_stream, :web_socket def initialize(opts = {}) @out_stream = opts[:out_stream] || $stdout @in_stream = opts[:in_stream] || $stdin + @web_socket = opts[:web_socket] || SocketIO end def dir File.expand_path(@list_dir) end @@ -38,17 +39,25 @@ return end list_name = Dir[dir + '/*/*'].find{ |fname| fname.include? target } if list_name list = List.new(list_name) - if emails = @options['email'] - web_service_url = ENV['CHECKCHECKIT_URL'] - response = Excon.post(web_service_url, :body => { - emails: emails, - list: list.to_h - }.to_json, - :headers => { 'Content-Type' => 'application/json' }) + web_service_url = ENV['CHECKCHECKIT_URL'] + client = nil + if (emails = @options['email']) || @options['live'] + + @list_id = list_id = notify_server_of_start(emails, list) + + puts "Live at URL: #{web_service_url}/#{list_id}" + + if @options['ws'] + @client = web_socket.connect(web_service_url, sync: true) do + after_start do + emit('register', {list_id: list_id}) + end + end + end end step_through_list(list) else puts "Could not find checklist via: #{target}" @@ -93,10 +102,17 @@ rescue Interrupt => e puts "\nGoodbye!" return end + if @client + @client.emit 'check', { + list_id: @list_id, + step_id: i + } + end + results[i] = { step: i + 1, name: step.name, body: step.body, check: check, @@ -136,6 +152,20 @@ def print(text = '') @out_stream.print text end + def web_service_url + ENV['CHECKCHECKIT_URL'] + end + + # Returns id + def notify_server_of_start(emails, list) + Excon.post(web_service_url, :body => { + emails: emails, + list: list.to_h + }.to_json, + :headers => { + 'Content-Type' => 'application/json' + }).body.to_i + end end