💾 Archived View for gem.graypegg.com › gor › controllers captured on 2023-04-26 at 12:55:29. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
are actually views and controllers in one. I decided for such a simple protocol, it's better to just inline styles rather than adding a whole other template system. We already have string interpolation in Ruby! (You are of course free to use File.read to load any files you wish to send to clients)
Controllers should be direct children in the file tree under /app and have a file name that ends with _controller. You cannot nest controllers. Controllers manage their route with any number of actions. Actions are methods in side your controller.
A simple example would be a simple static document, here we extend Controller, and define our own methods, HelloController#get, HelloController#greet_get and HelloController#greet_answer. We'll assume we assigned this controller to the route "hello" in /config/routes.rb.
╭────────────────────────┤ app/hello_controller.rb ├─╮ class HelloController < Controller # /hello def get respond_with <<~TEXT # 🎍 Gray's space Giving Gemini a try! It feels cozy. <3 => /hello/greet Get a personalized hello TEXT end # /hello/greet def greet_get ask_for "What's your name?" end # /hello/greet?name_will_be_here def greet_answer(name) respond_with <<~TEXT Hello #{name}! TEXT end end ╰────────────────────────────────────────────────────╯
You can include as many actions as you want! Private methods will not be accessible from the Gemini service, so you can keep things DRY by keeping common logic in private methods.
Note: A controller is always instantiated fresh for every connection. You cannot keep data between requests in this class. Use a persistent for that.
You assign controllers to routes using the /config/routes.rb file.
respond_with "Hello"
Respond to client with a text/gemini response. The gemini status code will be 20. You may want to use multiline comments like <<~TEXT to make things a little cleaner.
This MUST be the return statement in your action for it to work.
ask_for "What is your age?"
Sends back a request to the user to provide a string. You can include a question to be shown on the client. The gemini status will be 10.
This MUST be the return statement in your action for it to work.
redirect_to "gemini://gem.graypegg.com"
Sends back a redirect to the client. This will normally ask the user if they want to be redirect.
This MUST be the return statement in your action for it to work.
use_persistent(:GuestbookPersistent).all_messages
Gets the current instance of a persistent.
def get; end
def ailurus_get; end
Controller action for requests with no user input data. (No ? in the URI)
def answer(input); end
def fulgens_answer(input); end
Controller action for requests with user input data. (that have a ? in the URI)