|
@@ -1,4 +1,6 @@
|
|
|
require "moonshine"
|
|
|
+require "redis"
|
|
|
+
|
|
|
include Moonshine
|
|
|
include Moonshine::Shortcuts
|
|
|
include Moonshine::Http
|
|
@@ -6,12 +8,14 @@ include Moonshine::Http
|
|
|
# Compose Objects (like Hash) to have a to_json method
|
|
|
require "json/to_json"
|
|
|
|
|
|
+redis = Redis.new
|
|
|
app = Moonshine::App.new
|
|
|
|
|
|
-is = {
|
|
|
- :JSON => "application/json; charset=UTF-8"
|
|
|
- :PLAIN => "text/plain"
|
|
|
-}
|
|
|
+class CONTENT
|
|
|
+ JSON = "application/json; charset=UTF-8"
|
|
|
+ PLAIN = "text/plain"
|
|
|
+end
|
|
|
+
|
|
|
|
|
|
app.response_middleware do |req, res|
|
|
|
res.headers["Server"] = "Moonshine"
|
|
@@ -21,15 +25,26 @@ end
|
|
|
|
|
|
app.define do
|
|
|
|
|
|
- route "/plaintext", do |request|
|
|
|
- res = ok("Hello, World!")
|
|
|
- res.headers["Content-type"] = is[:PLAIN]
|
|
|
+ # Test 1: JSON Serialization
|
|
|
+ get "/json", do |request|
|
|
|
+ res = ok({ :message => "Hello, World!" }.to_json)
|
|
|
+ res.headers["Content-type"] = CONTENT::JSON
|
|
|
res
|
|
|
end
|
|
|
|
|
|
- get "/json", do |request|
|
|
|
- res = ok({ :message => "Hello, World!" }.to_json)
|
|
|
- res.headers["Content-type"] = is[:JSON]
|
|
|
+ # Test 4: Fortunes
|
|
|
+ route "/fortunes", do |request|
|
|
|
+ res = ok({
|
|
|
+ :length => redis.llen("fortunes")
|
|
|
+ }.to_json)
|
|
|
+ res.headers["Content-type"] = CONTENT::JSON
|
|
|
+ res
|
|
|
+ end
|
|
|
+
|
|
|
+ # Test 6: Plaintext
|
|
|
+ route "/plaintext", do |request|
|
|
|
+ res = ok("Hello, World!")
|
|
|
+ res.headers["Content-type"] = CONTENT::PLAIN
|
|
|
res
|
|
|
end
|
|
|
|