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

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

Direct Known Subclasses

File, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



36
37
38
39
40
41
42
# File 'lib/http_stub/server/stub/response/base.rb', line 36

def initialize(args={})
  @original_args    = args
  resolved_args     = self.class.merge_defaults(args)
  @status           = resolved_args["status"]
  @headers          = HttpStub::Server::Stub::Response::Attribute::Headers.new(resolved_args["headers"])
  @delay_in_seconds = resolved_args["delay_in_seconds"]
end

Instance Attribute Details

#delay_in_secondsObject (readonly)

Returns the value of attribute delay_in_seconds.



34
35
36
# File 'lib/http_stub/server/stub/response/base.rb', line 34

def delay_in_seconds
  @delay_in_seconds
end

#headersObject (readonly)

Returns the value of attribute headers.



34
35
36
# File 'lib/http_stub/server/stub/response/base.rb', line 34

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



34
35
36
# File 'lib/http_stub/server/stub/response/base.rb', line 34

def status
  @status
end

Class Method Details

.add_default_headers(headers) ⇒ Object



14
15
16
# File 'lib/http_stub/server/stub/response/base.rb', line 14

def add_default_headers(headers)
  default_headers.merge!(headers)
end

.merge_defaults(args) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/http_stub/server/stub/response/base.rb', line 18

def merge_defaults(args)
  args.clone.tap do |result|
    headers = result["headers"] ||= {}
    DEFAULT_ARGS.each    { |key, value| result[key]  = value if !result[key]  || result[key]  == "" }
    default_headers.each { |key, value| headers[key] = value if !headers[key] || headers[key] == "" }
  end
end

Instance Method Details

#to_hashObject



48
49
50
# File 'lib/http_stub/server/stub/response/base.rb', line 48

def to_hash
  { status: @status, headers: @headers, delay_in_seconds: @delay_in_seconds }
end

#to_json(*args) ⇒ Object



52
53
54
# File 'lib/http_stub/server/stub/response/base.rb', line 52

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

#typeObject



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

def type
  self.class.name.demodulize.underscore
end