Class: Mimic::FakeHost::StubbedRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, method, path) ⇒ StubbedRequest

Returns a new instance of StubbedRequest.



149
150
151
152
153
154
155
156
157
# File 'lib/mimic/fake_host.rb', line 149

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.



147
148
149
# File 'lib/mimic/fake_host.rb', line 147

def received
  @received
end

Instance Method Details

#buildObject



206
207
208
209
210
211
212
213
# File 'lib/mimic/fake_host.rb', line 206

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



178
179
180
# File 'lib/mimic/fake_host.rb', line 178

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

#matched_responseObject



190
191
192
# File 'lib/mimic/fake_host.rb', line 190

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

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
188
# File 'lib/mimic/fake_host.rb', line 182

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

#response_for_request(request) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/mimic/fake_host.rb', line 198

def response_for_request(request)
  if @echo_request_format
    @body = RequestEcho.new(request).to_s(@echo_request_format)
  end
  
  matches?(request) ? matched_response : unmatched_response
end

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



164
165
166
167
168
169
170
# File 'lib/mimic/fake_host.rb', line 164

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

#to_hashObject



159
160
161
162
# File 'lib/mimic/fake_host.rb', line 159

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

#unmatched_responseObject



194
195
196
# File 'lib/mimic/fake_host.rb', line 194

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

#with_query_parameters(params) ⇒ Object



172
173
174
175
176
# File 'lib/mimic/fake_host.rb', line 172

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