Browse Source

[ruby/hanami] Return json and plaintext from the routes. (#9687)

Petrik de Heus 5 months ago
parent
commit
46de0d47a8

+ 0 - 14
frameworks/Ruby/hanami/app/actions/json/index.rb

@@ -1,14 +0,0 @@
-# frozen_string_literal: true
-
-module HelloWorld
-  module Actions
-    module JSON
-      class Index < HelloWorld::Action
-        def handle(*, response)
-          response.format = :json
-          response.body = { 'message' => 'Hello, World!' }.to_json
-        end
-      end
-    end
-  end
-end

+ 0 - 13
frameworks/Ruby/hanami/app/actions/plaintext/index.rb

@@ -1,13 +0,0 @@
-# frozen_string_literal: true
-
-module HelloWorld
-  module Actions
-    module Plaintext
-      class Index < HelloWorld::Action
-        def handle(*, response)
-          response.body = 'Hello, World!'
-        end
-      end
-    end
-  end
-end

+ 18 - 2
frameworks/Ruby/hanami/config/routes.rb

@@ -2,11 +2,27 @@
 
 module HelloWorld
   class Routes < Hanami::Routes
-    get "/json", to: "json.index"
+    get "/json", to: ->(env) do
+      [200,
+       {
+         'Server' => 'Rails',
+         'Content-Type' => 'application/json',
+         'Date' => Time.now.httpdate,
+       },
+       [{ 'message' => 'Hello, World!' }.to_json]]
+    end
     get "/db", to: "db.index"
     get "/queries", to: "queries.index"
     get "/fortunes", to: "fortunes.index"
     get "/updates", to: "updates.index"
-    get "/plaintext", to: "plaintext.index"
+    get "/plaintext", to: ->(env) do
+      [200,
+       {
+         'Server' => 'Hanami',
+         'Content-Type' => 'text/plain',
+         'Date' => Time.now.httpdate
+       },
+       ['Hello, World!']]
+    end
   end
 end