module LookOut
  module RSpec
    class LookOutFormatter < ::RSpec::Core::Formatters::JsonFormatter
      ::RSpec::Core::Formatters.register self, :close

      def close(_notification)
        output_hash[:examples].each do |example|
          %i(description full_description line_number pending_message file_path).each do |key|
            example.delete(key)
          end
        end

        LookOut::Cast.create(
          api_key: LookOut.config.api_key,
          user: LookOut.config.user,
          data: output_hash,
          repo: LookOut.config.repo,
          sha: `git log -1 --format=%H`.chomp,
          env: env
        )
      end

      private

      def env
        if ENV['JENKINS'] || ENV['CI']
          :integration
        else
          :development
        end
      end
    end
  end
end