<commit_before>require 'eventmachine'
require 'em-eventsource'
require 'flowdock'
require 'lita/adapters/flowdock/message_handler'
require 'lita/adapters/flowdock/users_creator'

class Flowdock::Client
  def private_message(params)
    raise InvalidParameterError, "Message must have :content" if blank?(params[:content])
    raise InvalidParameterError, "Message must have :user_id" if blank?(params[:user_id])

    user_id = params.delete(:user_id)

    params = params.clone
    event = "message"

    post("private/#{user_id}/messages", params.merge(event: event))
  end
end

module Lita
  module Adapters
    class Flowdock < Adapter
      class Connector

        def initialize(robot, api_token, organization, flows, flowdock_client=nil, query_params=Hash.new)
          @robot = robot
          @api_token = api_token
          @organization = organization
          @flows = flows
          @client =
            flowdock_client || ::Flowdock::Client.new(api_token: api_token)
          @query_params = query_params
          @stream_url =
            "https://#{api_token}@stream.flowdock.com/flows?user=1&active=true&filter=#{request_flows}"

          UsersCreator.create_users(client.get('/users'))
        end

        def run(url=stream_url, queue=nil)
          EM.run do
            @source = EventMachine::EventSource.new(
              url,
              @query_params,
              {'Accept' => 'text/event-stream'}
            )

            source.open do
              log.info('Connected to flowdock streaming API')
              robot.trigger(:connected)
            end

            source.message do |message|
              event = MultiJson.load(message)
              receive_message(event)
            end

            source.error do |error|
              log.error(error)
              robot.trigger(:disconnected)
              log.info('Disconnected')
            end

            source.start
            queue << source if queue
          end
        end

        def send_messages(target, messages, thread)
          params = {
            flow: target.room
          }
          params.merge!(message_id: target.message_id) if thread
          messages.each do |message|<commit_msg>shutdown connection after an error<commit_after>require 'eventmachine'
require 'em-eventsource'
require 'flowdock'
require 'lita/adapters/flowdock/message_handler'
require 'lita/adapters/flowdock/users_creator'

class Flowdock::Client
  def private_message(params)
    raise InvalidParameterError, "Message must have :content" if blank?(params[:content])
    raise InvalidParameterError, "Message must have :user_id" if blank?(params[:user_id])

    user_id = params.delete(:user_id)

    params = params.clone
    event = "message"

    post("private/#{user_id}/messages", params.merge(event: event))
  end
end

module Lita
  module Adapters
    class Flowdock < Adapter
      class Connector

        def initialize(robot, api_token, organization, flows, flowdock_client=nil, query_params=Hash.new)
          @robot = robot
          @api_token = api_token
          @organization = organization
          @flows = flows
          @client =
            flowdock_client || ::Flowdock::Client.new(api_token: api_token)
          @query_params = query_params
          @stream_url =
            "https://#{api_token}@stream.flowdock.com/flows?user=1&active=true&filter=#{request_flows}"

          UsersCreator.create_users(client.get('/users'))
        end

        def run(url=stream_url, queue=nil)
          EM.run do
            @source = EventMachine::EventSource.new(
              url,
              @query_params,
              {'Accept' => 'text/event-stream'}
            )

            source.open do
              log.info('Connected to flowdock streaming API')
              robot.trigger(:connected)
            end

            source.message do |message|
              event = MultiJson.load(message)
              receive_message(event)
            end

            source.error do |error|
              log.error(error)
              robot.trigger(:disconnected)
              #log.info('Disconnected')
              log.info('Shutting down')              
              shut_down
            end

            source.start
            queue << source if queue
          end
        end

        def send_messages(target, messages, thread)
          params = {
            flow: target.room
          }
          params.merge!(message_id: target.message_id) if thread
          messages.each do |message|
{'fraction_non_alphanumeric': 0.09555261999119331, 'fraction_numerical': 0.00044033465433729633, 'mean_word_length': 2.0551445864156017, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4138511', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1221, 'n_words': 266}