Class: HttpMonkey::Configuration::Behaviours

Inherits:
Object
  • Object
show all
Defined in:
lib/http_monkey/configuration/behaviours.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBehaviours

Returns a new instance of Behaviours.



7
8
9
# File 'lib/http_monkey/configuration/behaviours.rb', line 7

def initialize
  self.clear!
end

Instance Attribute Details

#error_behaviourObject (readonly)

Returns the value of attribute error_behaviour.



5
6
7
# File 'lib/http_monkey/configuration/behaviours.rb', line 5

def error_behaviour
  @error_behaviour
end

#unknown_behaviourObject (readonly)

Returns the value of attribute unknown_behaviour.



5
6
7
# File 'lib/http_monkey/configuration/behaviours.rb', line 5

def unknown_behaviour
  @unknown_behaviour
end

Instance Method Details

#clear!Object



17
18
19
20
21
22
23
# File 'lib/http_monkey/configuration/behaviours.rb', line 17

def clear!
  @behaviours = {}
  @behaviours_range = {}
  @unknown_behaviour = nil
  @error_behaviour = nil
  nil
end

#find(code) ⇒ Object

Public: Look for behaviours defined by code.

code - Integer. Generally response.code

Returns Proc defined by code or unknown_behaviour proc.



47
48
49
50
51
52
53
54
55
# File 'lib/http_monkey/configuration/behaviours.rb', line 47

def find(code)
  behaviour = @behaviours[code]
  if behaviour.nil?
    _, behaviour = @behaviours_range.detect do |range, proc|
      range.include?(code)
    end
  end
  behaviour || self.unknown_behaviour
end

#initialize_copy(source) ⇒ Object



11
12
13
14
15
# File 'lib/http_monkey/configuration/behaviours.rb', line 11

def initialize_copy(source)
  super
  @behaviours = @behaviours.clone
  @behaviours_range = @behaviours_range.clone
end

#on(code, &block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/http_monkey/configuration/behaviours.rb', line 25

def on(code, &block)
  if code.is_a?(Integer)
    @behaviours[code] = block
  elsif code.respond_to?(:include?)
    @behaviours_range[code] = block
  end
  nil
end

#on_error(&block) ⇒ Object



38
39
40
# File 'lib/http_monkey/configuration/behaviours.rb', line 38

def on_error(&block)
  @error_behaviour = block
end

#on_unknown(&block) ⇒ Object



34
35
36
# File 'lib/http_monkey/configuration/behaviours.rb', line 34

def on_unknown(&block)
  @unknown_behaviour = block
end