require 'java' require 'jruby/core_ext' require 'bundler/setup' Bundler.require java_import 'ratpack.server.RatpackServer' RatpackServer.start do |server| server.handlers do |chain| chain.get do |ctx| ctx.render 'Hello World from Ratpack / jRuby' end end end # github.com/klappradla/jruby_ratpack_examples/hello_world
# server.rb RatpackServer.start do |server| server.handlers do |chain| chain.get do |ctx| ctx.render 'Hello World from Ratpack / jRuby' end chain.all(Handler::Default) end end # handler/default.rb module Handler class Default def self.handle(ctx) ctx.render 'Nothing here, sorry ¯\_(ツ)_/¯' end end end # https:/ /ratpack.io/manual/current/api/ratpack/handling/Handler.html
java_import 'ratpack.server.RatpackServer' java_import 'ratpack.exec.Blocking' require 'json' RatpackServer.start do |server| server.handlers do |chain| chain.get('music') do |ctx| Blocking.get do DB[:albums].all end.then do |albums| ctx.render(JSON.dump(albums)) end end end end # https:/ /ratpack.io/manual/current/async.html#performing_blocking_operations_eg_io
# server RatpackServer.start do |server| server.handlers do |chain| chain.get('music', Handler::Music) end end # github.com/klappradla/jruby_ratpack_examples/db_example
module Handler class Music class << self def handle(ctx) get_data.then do |data| render(ctx, data) end end private def get_data Blocking.get { DB[:albums].all } end def render(ctx, data) ctx.render(Jackson.json(data)) end end end end # github.com/klappradla/jruby_ratpack_examples/db_example
class Server def self.run RatpackServer.start do |server| server.handlers do |chain| chain.all(RequestLogger.ncsa) chain.get('music', Handler::Music) chain.get('planets', Handler::Planets) chain.all(Handler::NotFound) end end end end # github.com/klappradla/jruby_ratpack_examples/http_example
module Handler class Base def self.handle(args) new(*args).handle end def initialize(ctx) @ctx = ctx end def handle raise NotImplementedError end private attr_reader :ctx def response @response ||= ctx.get_response end end end # github.com/klappradla/jruby_ratpack_examples/http_example
module Handler class Planets < Base URL = java.net.URI.new('http://swapi.co/api/planets') def handle call_api.then do |resp| render(resp) end end private def call_api client.get(URL) end def client ctx.get(HttpClient.java_class) end def render(resp) response.send(‘application/json;charset=UTF-8', resp.get_body.get_text) end end end # github.com/klappradla/jruby_ratpack_examples/http_example