Sha256: eb590b2a38e53784f9cf463a825b8cbc7d4aa74247b093e2c54b4d872331e3d3

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

require 'rspectacles/config'
require 'uri'
require 'json'
require 'httparty'

module RSpectacles
  module Adapter
    class Logger
      attr_reader :test_run_key

      def initialize(test_run_key: nil)
        @test_run_key = test_run_key || config.last_run_primary_key
      end

      def config
        RSpectacles.config
      end

      def uri
        @uri ||= config.rspectacles_url
      end

      def stop
      end

      def start
      end

      def log(example)
        message = format_example(example)
        queue message
      end

      private

      def queue(message)
        return unless active?
        post_results Array.wrap(message)
      end

      def post_results(messages)
        HTTParty.post(full_uri, timeout: config.timeout,
                                body: { examples: messages }.to_json,
                                headers: { 'Content-Type' => 'application/json' })
      rescue Net::ReadTimeout
        puts "RSpectacles Timeout! Failed to send #{messages.size} messages"
      end

      def active?
        !!uri
      end

      def full_uri
        "#{uri}/examples"
      end

      def format_example(example)
        {
          rspec_run: test_run_key,
          description: example.description,
          full_description: example.full_description,
          status: example.execution_result.status,
          duration: example.execution_result.run_time,
          file_path: example.metadata[:file_path],
          line_number: example.metadata[:line_number]
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rspectacles-0.5.0 lib/rspectacles/adapter/logger.rb
rspectacles-0.4.2 lib/rspectacles/adapter/logger.rb
rspectacles-0.4.1 lib/rspectacles/adapter/logger.rb
rspectacles-0.4.0 lib/rspectacles/adapter/logger.rb