Sha256: a817e33d559121781e7f47933bbe12db79fe82541f80c28f22de7ba1ad23876a

Contents?: true

Size: 1.97 KB

Versions: 8

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

# 一个示例 Rack 应用,运行方式:`ruby -Ilib <this-file>`

require 'zlib'

# Paste has a Pony, Rack has a Lobster!
class Lobster
  LobsterString = Zlib::Inflate.inflate("eJx9kEEOwyAMBO99xd7MAcytUhPlJyj2
    P6jy9i4k9EQyGAnBarEXeCBqSkntNXsi/ZCvC48zGQoZKikGrFMZvgS5ZHd+aGWVuWwhVF0
    t1drVmiR42HcWNz5w3QanT+2gIvTVCiE1lm1Y0eU4JGmIIbaKwextKn8rvW+p5PIwFl8ZWJ
    I8jyiTlhTcYXkekJAzTyYN6E08A+dk8voBkAVTJQ==".delete("\n ").unpack("m*")[0])

  LambdaLobster = lambda { |env|
    if env[Rack::QUERY_STRING].include?("flip")
      lobster = LobsterString.split("\n").
        map { |line| line.ljust(42).reverse }.
        join("\n")
      href = "?"
    else
      lobster = LobsterString
      href = "?flip"
    end

    content = ["<title>Lobstericious!</title>",
               "<pre>", lobster, "</pre>",
               "<a href='#{href}'>flip!</a>"]
    length = content.inject(0) { |a, e| a + e.size }.to_s
    [200, { Rack::CONTENT_TYPE => "text/html", Rack::CONTENT_LENGTH => length }, content]
  }

  def call(env)
    req = Rack::Request.new(env)
    if req.GET["flip"] == "left"
      lobster = LobsterString.split("\n").map do |line|
        line.ljust(42).reverse.
          gsub('\\', 'TEMP').
          gsub('/', '\\').
          gsub('TEMP', '/').
          gsub('{', '}').
          gsub('(', ')')
      end.join("\n")
      href = "?flip=right"
    elsif req.GET["flip"] == "crash"
      raise "Lobster crashed"
    else
      lobster = LobsterString
      href = "?flip=left"
    end

    res = Rack::Response.new
    res.write "<title>Lobstericious!</title>"
    res.write "<pre>"
    res.write lobster
    res.write "</pre>"
    res.write "<p><a href='#{href}'>flip!</a></p>"
    res.write "<p><a href='?flip=crash'>crash!</a></p>"
    res.finish
  end
end

if $0 == __FILE__
  # :nocov:
  require 'rack'

  app = Lobster.new # or Lobster::LambdaLobster
  Rack::Server.start(
    app: Rack::ShowExceptions.new(Rack::Lint.new(app)), Port: 9292
  )
  # :nocov:
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
meta-api-0.0.8 examples/lobster.rb
meta-api-0.0.7 examples/lobster.rb
meta-api-0.0.6 examples/lobster.rb
meta-api-0.0.5 examples/lobster.rb
meta-api-0.0.4 examples/lobster.rb
meta-api-0.0.3 examples/lobster.rb
meta-api-0.0.2 examples/lobster.rb
meta-api-0.0.1 examples/lobster.rb