Class: Stub::Reply
- Inherits:
-
Object
- Object
- Stub::Reply
- Defined in:
- lib/stub/server.rb
Overview
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #html(status = nil, info) ⇒ Object
-
#initialize(status = 200) ⇒ Reply
constructor
A new instance of Reply.
- #json(status = nil, info) ⇒ Object
- #set_cookie(name, value, options = {}) ⇒ Object
- #text(status = nil, info) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(status = 200) ⇒ Reply
Returns a new instance of Reply.
89 |
# File 'lib/stub/server.rb', line 89 def initialize(status = 200) @status, @headers, @cookies, @body = status, {}, [], "" end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
88 89 90 |
# File 'lib/stub/server.rb', line 88 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
88 89 90 |
# File 'lib/stub/server.rb', line 88 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
88 89 90 |
# File 'lib/stub/server.rb', line 88 def status @status end |
Instance Method Details
#html(status = nil, info) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/stub/server.rb', line 111 def html(status = nil, info) @status = status if status headers["content-type"] = "text/html" info = ERB::Util.html_escape(info.pretty_inspect) unless info.is_a?(String) @body = "<html><body>#{info}</body></html>" nil end |
#json(status = nil, info) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/stub/server.rb', line 98 def json(status = nil, info) info = {message: info} unless info.respond_to? :each @status = status if status headers["content-type"] = "application/json" @body = MultiJson.dump(info) nil end |
#set_cookie(name, value, options = {}) ⇒ Object
118 119 120 121 122 |
# File 'lib/stub/server.rb', line 118 def (name, value, = {}) @cookies << .each_with_object("#{name}=#{value}") { |(k, v), o| o << (v.nil? ? "; #{k}" : "; #{k}=#{v}") } end |
#text(status = nil, info) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/stub/server.rb', line 105 def text(status = nil, info) @status = status if status headers["content-type"] = "text/plain" @body = info.pretty_inspect nil end |
#to_s ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/stub/server.rb', line 90 def to_s reply = "HTTP/1.1 #{@status} OK\r\n" headers["server"], headers["date"] = "stub server", DateTime.now.httpdate headers["content-length"] = body.bytesize headers.each { |k, v| reply << "#{k}: #{v}\r\n" } @cookies.each { |c| reply << "Set-Cookie: #{c}\r\n" } reply << "\r\n" << body end |