Class: Opticon::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/opticon/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, notifiers = Opticon.default_notifiers) ⇒ Service

Returns a new instance of Service.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/opticon/service.rb', line 8

def initialize(uri, notifiers = Opticon.default_notifiers)
  raise ArgumentError, "You must either provide a valid Notifier or set Opticon.default_notifiers." unless
    notifiers and (notifiers.respond_to? :notify or notifiers.first.respond_to? :notify)
  @uri = URI.parse(uri)
  
  if notifiers.kind_of? Array
    @notifiers = notifiers
  else
    @notifiers = [notifiers]
  end
end

Instance Attribute Details

#notifiersObject

Returns the value of attribute notifiers.



6
7
8
# File 'lib/opticon/service.rb', line 6

def notifiers
  @notifiers
end

Instance Method Details

#failures?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
# File 'lib/opticon/service.rb', line 82

def failures?
  if @failures.nil?
    raise "Test has not yet been run."
  else
    not @failures.empty?
  end
end

#send_batch_notificationObject



78
79
80
# File 'lib/opticon/service.rb', line 78

def send_batch_notification
  notifiers.each {|n| n.notify(@failures)} unless @failures.empty?
end

#test(*args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/opticon/service.rb', line 24

def test(*args, &block)
  if block
    @prev_batch_notify = @batch_notify
    @batch_notify = true
    instance_eval(&block)
    send_batch_notification
    @batch_notify = @prev_batch_notify
  else
    tester_type = args[0]
    condition = args[1]
    options = args[2]
  
    case tester_type
    when :responds_with_code, 'responds_with_code'
      tester = Opticon::Tester::ResponseCodeTester.new
    when :response_body_contains, 'response_body_contains'
      tester = Opticon::Tester::ContentTester.new
    when Class
      tester = tester.new
    when Tester::Base
      tester = tester
    else
      begin
        tester_class = "Opitcon::Tester::#{tester.to_s}".constantize
        tester = tester_class.new
      rescue NameError
        bad_tester = true
      end
      
      if bad_tester or !tester.respond_to? :test
        raise ArgumentError, "'#{tester}' is not a valid tester. The parameter must be: 'responds_with_code', 'response_body_contains', or"+
          " an Object or Class that responds to a 'test' method."
      end
    end
    
    tester.uri = @uri
    
    @failures ||= []
    
    debug = "#{@uri} #{tester_type} #{condition.inspect}?"
    
    if tester.run(condition)
      puts "#{debug} ==> PASSED!" if ARGV.include?("-v")
    else
      puts "#{debug} ==> FAILED!" if ARGV.include?("-v")
      notifiers.each {|n| n.notify(tester.failure)} unless @batch_notify
      @failures << tester.failure
    end
    
    # return self to allow for chaining test calls
    return self
  end
end

#to_sObject



20
21
22
# File 'lib/opticon/service.rb', line 20

def to_s
  @uri.to_s
end