Browse Source

Use Rails metal controller for plaintext and json (#7043)

Michael Spiz 3 years ago
parent
commit
d43ab12407

+ 7 - 0
frameworks/Ruby/rails/app/controllers/application_controller_metal.rb

@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+class ApplicationControllerMetal < ActionController::Metal
+  def add_headers
+    response.set_header('Server', 'rails')
+    response.set_header('Date', Time.now.httpdate)
+  end
+end

+ 7 - 0
frameworks/Ruby/rails/app/controllers/json_controller.rb

@@ -0,0 +1,7 @@
+class JsonController < ApplicationControllerMetal
+  def index
+    add_headers
+    self.content_type = 'application/json'
+    self.response_body = Oj.dump({ 'message' => 'Hello, World!' })
+  end
+end

+ 7 - 0
frameworks/Ruby/rails/app/controllers/plaintext_controller.rb

@@ -0,0 +1,7 @@
+class PlaintextController < ApplicationControllerMetal
+  def index
+    add_headers
+    self.content_type = 'text/plain'
+    self.response_body = 'Hello, World!'
+  end
+end

+ 2 - 2
frameworks/Ruby/rails/config/routes.rb

@@ -1,11 +1,11 @@
 Rails.application.routes.draw do
 Rails.application.routes.draw do
   # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
   # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
 
 
-  get "json", to: "hello_world#json"
+  get "json", to: JsonController.action(:index)
   get "db", to: "hello_world#db"
   get "db", to: "hello_world#db"
   get "queries", to: "hello_world#query"
   get "queries", to: "hello_world#query"
   get "fortunes", to: "hello_world#fortune"
   get "fortunes", to: "hello_world#fortune"
   get "updates", to: "hello_world#update"
   get "updates", to: "hello_world#update"
-  get "plaintext", to: "hello_world#plaintext"
+  get "plaintext", to: PlaintextController.action(:index)
   get "cached", to: "hello_world#cached_query"
   get "cached", to: "hello_world#cached_query"
 end
 end