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.



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

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

Instance Attribute Details

#extractorsObject (readonly)

Returns the value of attribute extractors.



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

def extractors
  @extractors
end

#loadersObject (readonly)

Returns the value of attribute loaders.



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

def loaders
  @loaders
end

Instance Method Details

#create(name) ⇒ Object



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

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

#load(resource, &block) ⇒ Object



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

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



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

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

#save(resource) ⇒ Object



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

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