Sha256: 921995499956686436c3733a16ce26f535bed4cf42c1f697e03814412a367c0b

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require "rack"
require "toycol"

Toycol::Protocol.use(:safe_ruby)

class App
  def call(env)
    case env["REQUEST_METHOD"]
    when "GET"
      case env["PATH_INFO"]
      when "/posts"
        return app_for_get_with_query if env["QUERY_STRING"] == "user_id=2"

        app_for_get
      end
    when "POST"
      input   = env["rack.input"].gets
      created = input.split("&").map { |str| str.split("=") }.to_h

      app_for_post(user_id: created["user_id"], body: created["body"])
    end
  end

  private

  def app_for_get_with_query
    [
      200,
      { "Content-Type" => "text/html" },
      ["User<2> I love RubyKaigi!\n"]
    ]
  end

  def app_for_get
    [
      200,
      { "Content-Type" => "text/html" },
      ["User<1> I love Ruby!\n", "User<2> I love RubyKaigi!\n"]
    ]
  end

  def app_for_post(user_id:, body:)
    [
      201,
      { "Content-Type" => "text/html", "Location" => "/posts" },
      ["User<1> I love Ruby!\n",
       "User<2> I love RubyKaigi!\n",
       "User<#{user_id}> #{body}\n"]
    ]
  end
end

run App.new

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
toycol-1.0.0 examples/safe_ruby/config_safe_ruby.ru
toycol-0.3.1 examples/safe_ruby/config_safe_ruby.ru
toycol-0.3.0 examples/safe_ruby/config_safe_ruby.ru
toycol-0.2.2 examples/safe_ruby/config_safe_ruby.ru
toycol-0.2.1 examples/safe_ruby/config_safe_ruby.ru
toycol-0.2.0 examples/safe_ruby/config_safe_ruby.ru
toycol-0.1.0 examples/safe_ruby/config_safe_ruby.ru