Class: Rack::UsersAndHashtags

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/users-and-hashtags.rb,
lib/rack/users-and-hashtags/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ UsersAndHashtags

Returns a new instance of UsersAndHashtags.



7
8
9
10
# File 'lib/rack/users-and-hashtags.rb', line 7

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

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/users-and-hashtags.rb', line 12

def call(env)
  status, headers, body = @app.call(env)

  if body.class == ActionDispatch::Response
    headers = HeaderHash.new(headers)

    nbody = ""
    body.each do |b|
      doc = Nokogiri::HTML(b.to_s)
      doc.css(@options[:matcher]).each do |d|
        d.inner_html = d.inner_html.gsub(/(\W)@([\w-]+)?/, '\1<a href="' + "#{@options[:user_url]}" + '\2">@\2</a>')
        d.inner_html = d.inner_html.gsub(/(\W)#([\w-]+)?/, '\1<a href="' + "#{@options[:hashtag_url]}" + '\2">#\2</a>')
      end
      nbody = doc.to_s
    end
    body = [nbody]

    headers['Content-Length'] &&= bytesize(nbody).to_s
  end
  [status, headers, body]
end