Class: Fitting::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/fitting/log.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, type) ⇒ Log

Returns a new instance of Log.



3
4
5
6
7
8
# File 'lib/fitting/log.rb', line 3

def initialize(log, type)
  @log = log
  @type = type
  @error = nil
  @skip = false
end

Class Method Details

.allObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fitting/log.rb', line 10

def self.all
  logs = []
  Dir["log/fitting*.log"].each do |file_path|
    testlog = File.read(file_path)
    testlog.split("\n").select { |f| f.include?('incoming request ') }.each do |test|
      logs.push(new(JSON.load(test.split('incoming request ')[1]), 'incoming'))
    end
    testlog.split("\n").select { |f| f.include?('outgoing request ') }.each do |test|
      logs.push(new(JSON.load(test.split('outgoing request ')[1]), 'outgoing'))
    end
  end
  logs.sort { |a, b| b.path <=> a.path }
end

.failure(logs) ⇒ Object



78
79
80
81
82
# File 'lib/fitting/log.rb', line 78

def self.failure(logs)
  logs.select do |log|
    log.failure?
  end
end

.pending(logs) ⇒ Object



88
89
90
91
92
# File 'lib/fitting/log.rb', line 88

def self.pending(logs)
  logs.select do |log|
    log.pending?
  end
end

.report(logs) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fitting/log.rb', line 94

def self.report(logs)
  puts "\n\n"
  Fitting::Log.failure(logs).each_with_index do |log, index|
    puts "\e[31m  #{index + 1}) #{log.error.class} #{log.error.message}\n\n\e[0m"
  end

  failure_count = Fitting::Log.failure(logs).size
  color_code = failure_count > 0 ? 31 : 32
  print "\e[#{color_code}m#{logs.size} examples, #{failure_count} failure, #{Fitting::Log.pending(logs).size} pending\e[0m\n"

  unless Fitting::Log.failure(logs).size <= 0
    exit 1
  end
end

Instance Method Details

#access!Object



56
57
58
# File 'lib/fitting/log.rb', line 56

def access!
  print "\e[32m.\e[0m"
end

#bodyObject



40
41
42
# File 'lib/fitting/log.rb', line 40

def body
  @log['response']['body']
end

#content_typeObject



44
45
46
# File 'lib/fitting/log.rb', line 44

def content_type
  @log['response']['content_type']
end

#errorObject



70
71
72
# File 'lib/fitting/log.rb', line 70

def error
  @error
end

#failure!(error) ⇒ Object



65
66
67
68
# File 'lib/fitting/log.rb', line 65

def failure!(error)
  @error = error
  print "\e[31mF\e[0m"
end

#failure?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/fitting/log.rb', line 74

def failure?
  @error.present?
end

#hostObject



48
49
50
# File 'lib/fitting/log.rb', line 48

def host
  @log['host'] || 'www.example.com'
end

#methodObject



32
33
34
# File 'lib/fitting/log.rb', line 32

def method
  @log['method']
end

#pathObject



28
29
30
# File 'lib/fitting/log.rb', line 28

def path
  @log['path']
end

#pending!Object



60
61
62
63
# File 'lib/fitting/log.rb', line 60

def pending!
  @skip = true
  print "\e[33m*\e[0m"
end

#pending?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/fitting/log.rb', line 84

def pending?
  @skip
end

#statusObject



36
37
38
# File 'lib/fitting/log.rb', line 36

def status
  @log['response']['status'].to_s
end

#typeObject



52
53
54
# File 'lib/fitting/log.rb', line 52

def type
  @type
end

#urlObject



24
25
26
# File 'lib/fitting/log.rb', line 24

def url
  "#{host}#{path}"
end