Class: HttpStub::Server::Stub::Response::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/http_stub/server/stub/response/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Response

Returns a new instance of Response.



14
15
16
17
18
19
20
21
22
# File 'lib/http_stub/server/stub/response/response.rb', line 14

def initialize(args)
  @original_args    = args
  resolved_args     = DEFAULT_ARGS.merge(args)
  @status           = resolved_args[:status]
  @body             = HttpStub::Server::Stub::Response::Body.create(resolved_args)
  @headers          = HttpStub::Server::Stub::Response::Headers.create(resolved_args[:headers], @body)
  @delay_in_seconds = resolved_args[:delay_in_seconds]
  @blocks           = HttpStub::Server::Stub::Response::Blocks.new(resolved_args[:blocks])
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



12
13
14
# File 'lib/http_stub/server/stub/response/response.rb', line 12

def blocks
  @blocks
end

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/http_stub/server/stub/response/response.rb', line 12

def body
  @body
end

#delay_in_secondsObject (readonly)

Returns the value of attribute delay_in_seconds.



12
13
14
# File 'lib/http_stub/server/stub/response/response.rb', line 12

def delay_in_seconds
  @delay_in_seconds
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/http_stub/server/stub/response/response.rb', line 12

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/http_stub/server/stub/response/response.rb', line 12

def status
  @status
end

Instance Method Details

#serve_on(application) ⇒ Object



28
29
30
31
# File 'lib/http_stub/server/stub/response/response.rb', line 28

def serve_on(application)
  sleep @delay_in_seconds
  @body.serve(application, self)
end

#to_hashObject



33
34
35
36
37
38
39
40
41
# File 'lib/http_stub/server/stub/response/response.rb', line 33

def to_hash
  {
    status:           @status,
    headers:          @headers,
    body:             @body,
    delay_in_seconds: @delay_in_seconds,
    blocks:           @blocks.to_array
  }
end

#to_json(*args) ⇒ Object



43
44
45
# File 'lib/http_stub/server/stub/response/response.rb', line 43

def to_json(*args)
  self.to_hash.to_json(*args)
end

#with_values_from(request) ⇒ Object



24
25
26
# File 'lib/http_stub/server/stub/response/response.rb', line 24

def with_values_from(request)
  self.class.new(@original_args.merge(blocks: []).deep_merge(@blocks.evaluate_with(request)))
end