Class: Mimic::StubbedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/mimic/stubbed_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, method, path) ⇒ StubbedRequest

Returns a new instance of StubbedRequest.



7
8
9
10
11
12
13
14
15
# File 'lib/mimic/stubbed_request.rb', line 7

def initialize(app, method, path)
  @method, @path = method, path
  @code = 200
  @headers = {}
  @params = {}
  @body = ""
  @app = app
  @received = false
end

Instance Attribute Details

#receivedObject

Returns the value of attribute received.



5
6
7
# File 'lib/mimic/stubbed_request.rb', line 5

def received
  @received
end

Instance Method Details

#buildObject



64
65
66
67
68
69
70
71
# File 'lib/mimic/stubbed_request.rb', line 64

def build
  stub = self

  @app.send(@method.downcase, @path) do
    stub.received = true
    stub.response_for_request(request)
  end
end

#echo_request!(format = :json) ⇒ Object



36
37
38
# File 'lib/mimic/stubbed_request.rb', line 36

def echo_request!(format = :json)
  @echo_request_format = format
end

#matched_responseObject



48
49
50
# File 'lib/mimic/stubbed_request.rb', line 48

def matched_response
  [@code, @headers, @body]
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/mimic/stubbed_request.rb', line 40

def matches?(request)
  if @params.any?
    request.params == @params
  else
    true
  end
end

#response_for_request(request) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/mimic/stubbed_request.rb', line 56

def response_for_request(request)
  if @echo_request_format
    @body = Helpers::RequestEcho.new(request).to_s(@echo_request_format)
  end

  matches?(request) ? matched_response : unmatched_response
end

#returning(body, code = 200, headers = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mimic/stubbed_request.rb', line 22

def returning(body, code = 200, headers = {})
  tap do
    @body = body
    @code = code
    @headers = headers
  end
end

#to_hashObject



17
18
19
20
# File 'lib/mimic/stubbed_request.rb', line 17

def to_hash
  token = "#{@method} #{@path}"
  Digest::MD5.hexdigest(token)
end

#unmatched_responseObject



52
53
54
# File 'lib/mimic/stubbed_request.rb', line 52

def unmatched_response
  [404, {}, ""]
end

#with_query_parameters(params) ⇒ Object



30
31
32
33
34
# File 'lib/mimic/stubbed_request.rb', line 30

def with_query_parameters(params)
  tap do
    @params = params
  end
end