Class: Xapian::Rack::RelativeLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian/rack/search.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RelativeLoader.


27
28
29
30
31
# File 'lib/xapian/rack/search.rb', line 27

def initialize(app, options = {})
	@app = app
	
	@logger = options[:logger] || Logger.new($stderr)
end

Instance Method Details

#call(name, &block) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xapian/rack/search.rb', line 33

def call(name, &block)
	# Handle relative URIs
	if URI.parse(name).relative?
		env = {
			'HTTP_ACCEPT' => '*/*',
			'QUERY_STRING' => '',
			'REQUEST_METHOD' => 'GET',
			'PATH_INFO' => name,
			'rack.input' => StringIO.new,
		}
		
		begin
			status, header, content = @app.call(env)
		rescue
			@logger.error "Error requesting resource #{name}: #{$!}"
			$!.backtrace.each{|line| @logger.error(line)}
			
			yield 500, {}, nil
		end
	
		body = lambda do 
			buffer = ""
			content.each{|segment| buffer += segment}

			buffer
		end
		
		downcase_header = {}
		header.each{|k,v| downcase_header[k.downcase] = v}
		
		yield status, downcase_header, body
		
		return true
	end
	
	return false
end