Sha256: 2300f416c41fd2ebd6f8c1228b5e188c27dacb18c88bae4df2b1940da0ea77aa

Contents?: true

Size: 921 Bytes

Versions: 1

Compression:

Stored size: 921 Bytes

Contents

require "rack/hatena_star/version"

module Rack
  class HatenaStar
    def initialize(app, options)
      @app, @options = app, options
    end

    def call(env)
      status, headers, body = @app.call(env)
      return [status, headers, body] unless html?(headers)

      res = Rack::Response.new([], status, headers)

      body.each do |b|
        res.write(b.sub('</head>', <<-EOS))
          <script type="text/javascript" src="//s.hatena.ne.jp/js/HatenaStar.js"></script>
          <script type="text/javascript">
            Hatena.Star.Token = "#{@options[:token]}";
            Hatena.Star.SiteConfig = {
              entryNodes: #{@options[:entry_nodes].to_json}
            };
          </script>
          </head>
        EOS
      end
      body.close if body.respond_to? :close

      res.finish
    end

    private

    def html?(headers)
      headers['Content-Type'] =~ /text\/html/
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-hatena_star-0.1.0 lib/rack/hatena_star.rb