Class: Response

Inherits:
Object
  • Object
show all
Includes:
WebMock::API
Defined in:
lib/endpoint/response.rb

Defined Under Namespace

Classes: ParamIndices, Params

Instance Method Summary collapse

Constructor Details

#initialize(type, url, stub, &proc) ⇒ Response

Returns a new instance of Response.



19
20
21
22
23
24
25
26
27
28
# File 'lib/endpoint/response.rb', line 19

def initialize(type, url, stub, &proc)
  @param_indices = {}
  
  @url_regex = build_url_regex!(url)

  @type = type
  @stub = stub

  @response_stack = [proc]
end

Instance Method Details

#activate!Object

This should add the stubbed request to WebMock.



35
36
37
38
# File 'lib/endpoint/response.rb', line 35

def activate!
  @stubbed_request = stub_request(@type, @url_regex).
    to_return(&create_response_proc(@response_stack))
end

#activated?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/endpoint/response.rb', line 30

def activated?
  !@stubbed_request.nil?
end

#add_to_stack(&proc) ⇒ Object



58
59
60
61
62
# File 'lib/endpoint/response.rb', line 58

def add_to_stack(&proc)
  deactivate! do
    @response_stack << proc
  end
end

#deactivate!Object

This should remove the request stubbed by #activate! Passing a block will reactivate when done with block logic if previously activated.



43
44
45
46
47
48
49
50
51
# File 'lib/endpoint/response.rb', line 43

def deactivate!
  remove_request_stub @stubbed_request
  if block_given?
    yield
    activate! if @stubbed_request
  else
    @stubbed_request = nil
  end
end

#drop_overrides!Object

Return to the first stubbed response.



65
66
67
68
69
70
# File 'lib/endpoint/response.rb', line 65

def drop_overrides!
  deactivate! do
    remove_request_stub @stubbed_request
    @response_stack = @response_stack[0..1]
  end
end

#reset!Object



53
54
55
56
# File 'lib/endpoint/response.rb', line 53

def reset!
  deactivate!
  activate!
end