emptygif
语言: 简体中文

所有语言和框架

Hanami Ruby

一个用于 Hanami 的 Ruby 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Hanami Ruby

安装
gem install hanami && hanami new my_app
文件
show.rb
运行
bundle exec hanami server --port 8080
# app/actions/pixel/show.rb
module MyApp
  module Actions
    module Pixel
      class Show < MyApp::Action
        GIF = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==".unpack1("m0").freeze

        def handle(request, response)
          # Rack's content_type= sets the exact header; format= would append "; charset=utf-8".
          response.content_type = "image/gif"
          response.headers["Cache-Control"] = "no-store"
          response.body = GIF
        end
      end
    end
  end
end

# config/routes.rb
module MyApp
  class Routes < Hanami::Routes
    get "/pixel.gif", to: "pixel.show"
  end
end

我们运行了此代码片段,并比对了它返回的字节。

验证

curl -sI http://localhost:8080/pixel.gif

官方网站: Hanami

更多 Ruby 示例