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 範例