class Rack::HatenaStar

Constants

VERSION

Public Class Methods

new(app, options) click to toggle source
# File lib/rack/hatena_star.rb, line 5
def initialize(app, options)
  @app, @options = app, options
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/hatena_star.rb, line 9
    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 Instance Methods

html?(headers) click to toggle source
# File lib/rack/hatena_star.rb, line 34
def html?(headers)
  headers['Content-Type'] =~ /text\/html/
end