Ruby on Rails Ruby
適用於 Ruby on Rails 的 Ruby 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。
Ruby on Rails Ruby
# app/controllers/pixels_controller.rb
# ActionController::API: no session, cookies or browser-version checks for a beacon.
class PixelsController < ActionController::API
GIF = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==".unpack1("m0").freeze
def show
no_store
send_data GIF, type: "image/gif", disposition: "inline"
end
end
# config/routes.rb
Rails.application.routes.draw do
get "/pixel.gif", to: "pixels#show", format: false
end
我們執行了這段程式碼,並比對了它回傳的位元組。
驗證
curl -sI http://localhost:8080/pixel.gif