Class: Rack::HatenaStar

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/hatena_star.rb,
lib/rack/hatena_star/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ HatenaStar

Returns a new instance of HatenaStar.



5
6
7
# File 'lib/rack/hatena_star.rb', line 5

def initialize(app, options)
  @app, @options = app, options
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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