Class: Stub::Reply

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

Overview


Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



89
90
91
# File 'lib/uaa/stub/server.rb', line 89

def body
  @body
end

#headersObject

Returns the value of attribute headers.



89
90
91
# File 'lib/uaa/stub/server.rb', line 89

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



89
90
91
# File 'lib/uaa/stub/server.rb', line 89

def status
  @status
end

Instance Method Details

#emptyObject



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 = MultiJson.dump(info)
  nil
end


125
126
127
128
129
# File 'lib/uaa/stub/server.rb', line 125

def set_cookie(name, value, options = {})
  @cookies << options.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_sObject



91
92
93
94
95
96
97
98
99
# File 'lib/uaa/stub/server.rb', line 91

def to_s
  message = Rack::Utils::HTTP_STATUS_CODES[@status]
  reply = "HTTP/1.1 #{@status} #{message.upcase if message}\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