Class: Stub::Reply
- Inherits:
-
Object
- Object
- Stub::Reply
- Defined in:
- lib/uaa/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
- #empty ⇒ Object
- #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.
90 |
# File 'lib/uaa/stub/server.rb', line 90 def initialize(status = 200) @status, @headers, @cookies, @body = status, {}, [], "" end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
89 90 91 |
# File 'lib/uaa/stub/server.rb', line 89 def body @body end |
#headers ⇒ Object
Returns the value of attribute headers.
89 90 91 |
# File 'lib/uaa/stub/server.rb', line 89 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
89 90 91 |
# File 'lib/uaa/stub/server.rb', line 89 def status @status end |
Instance Method Details
#empty ⇒ Object
120 121 122 123 124 |
# File 'lib/uaa/stub/server.rb', line 120 def empty() @status = 204 @body = '' nil end |
#html(status = nil, info) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/uaa/stub/server.rb', line 113 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
100 101 102 103 104 105 106 |
# File 'lib/uaa/stub/server.rb', line 100 def json(status = nil, info) info = {message: info} unless info.respond_to? :each @status = status if status headers["content-type"] = "application/json" @body = JSON.dump(info) nil end |
#set_cookie(name, value, options = {}) ⇒ Object
125 126 127 128 129 |
# File 'lib/uaa/stub/server.rb', line 125 def (name, value, = {}) @cookies << .each_with_object("#{name}=#{value}") { |(k, v), o| o << (v.nil? ? "; #{k}" : "; #{k}=#{v}") } end |
#text(status = nil, info) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/uaa/stub/server.rb', line 107 def text(status = nil, info) @status = status if status headers["content-type"] = "text/plain" @body = info.pretty_inspect nil end |
#to_s ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/uaa/stub/server.rb', line 91 def to_s = Rack::Utils::HTTP_STATUS_CODES[@status] reply = "HTTP/1.1 #{@status} #{.upcase if }\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 |