Class: Xapian::Indexer::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/xapian/indexer/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Controller

Returns a new instance of Controller.



23
24
25
26
27
28
# File 'lib/xapian/indexer/resource.rb', line 23

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

Instance Attribute Details

#extractorsObject (readonly)

Returns the value of attribute extractors.



31
32
33
# File 'lib/xapian/indexer/resource.rb', line 31

def extractors
  @extractors
end

#loadersObject (readonly)

Returns the value of attribute loaders.



30
31
32
# File 'lib/xapian/indexer/resource.rb', line 30

def loaders
  @loaders
end

Instance Method Details

#create(name) ⇒ Object



33
34
35
# File 'lib/xapian/indexer/resource.rb', line 33

def create(name)
	Resource.new(name, self)
end

#load(resource, &block) ⇒ Object



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
70
71
72
# File 'lib/xapian/indexer/resource.rb', line 37

def load(resource, &block)
	@loaders.each do |loader|
		loader.call(resource.name) do |status, header, load_body|
			if status >= 200 && status < 300
				# Process the page content
				mime_type = header['content-type'].split(";").first
				extractor = @extractors[mime_type]

				if extractor
					body = load_body.call
					 = extractor.call(resource, status, header, body)

					# Load the data into the resource
					yield status, header, body, 

					return true
				else
					@logger.warn "Ignoring resource #{resource.name} because content-type #{mime_type} is not supported."
					return false
				end
			elsif status >= 300 && status < 400
				# Process the redirect
				location = URI.parse(resource.name) + header['location']
				
				 = {
					:links => [location.to_s]
				}
				
				# This resource is not indexable, using nil for body
				yield status, header, nil, 
			end
		end
	end
	
	return false
end

#recreate(data) ⇒ Object



78
79
80
81
82
# File 'lib/xapian/indexer/resource.rb', line 78

def recreate(data)
	values = YAML::load(data)
	
	Resource.new(values[:name], self, values)
end

#save(resource) ⇒ Object



74
75
76
# File 'lib/xapian/indexer/resource.rb', line 74

def save(resource)
	YAML::dump(resource.to_hash)
end