Class: LinkMiddleware::Annotator

Inherits:
Object
  • Object
show all
Defined in:
lib/link-middleware/annotator.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Annotator

Returns a new instance of Annotator.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/link-middleware/annotator.rb', line 5

def initialize(app, options={})
  @app = app       
  @options = options
  if options[:store]
    @store = options[:store]
  elsif options[:store_impl]
    @store = options[:store].new(options)
  else
    @store = LinkMiddleware::MemoryStore.new(options)
  end
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/link-middleware/annotator.rb', line 17

def call(env)      
  status, headers, response = @app.call(env)            
  request = Rack::Request.new( env ) 
  stored_links = @store.read( request.url )
  if !stored_links.links.empty?
    link_header = stored_links.to_s
    if headers["Link"]         
      link_header = env["Link"] + ";" + link_header
    end
    headers["Link"] = link_header
  end          
  [status, headers, response]
end