emptygif
언어: 한국어

모든 언어 및 프레임워크

Hanami Ruby

/pixel.gif 요청에 빈 GIF로 응답하는 Hanami용 Ruby 스니펫입니다. 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의 다른 예제