Class: Xapian::Indexer::Resource

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

Overview

Represents a resource that will be indexed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, controller, values = {}) ⇒ Resource

Returns a new instance of Resource.



92
93
94
95
96
97
98
99
100
101
# File 'lib/xapian/indexer/resource.rb', line 92

def initialize(name, controller, values = {})
	@name = name
	@controller = controller
	
	@fetched_on = values[:fetched_on]
	@status = values[:status]
	@header = values[:header]
	@body = values[:body]
	@metadata = values[:metadata]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



106
107
108
# File 'lib/xapian/indexer/resource.rb', line 106

def body
  @body
end

#headerObject (readonly)

Returns the value of attribute header.



105
106
107
# File 'lib/xapian/indexer/resource.rb', line 105

def header
  @header
end

#metadataObject (readonly)

Returns the value of attribute metadata.



107
108
109
# File 'lib/xapian/indexer/resource.rb', line 107

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



103
104
105
# File 'lib/xapian/indexer/resource.rb', line 103

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



104
105
106
# File 'lib/xapian/indexer/resource.rb', line 104

def status
  @status
end

Instance Method Details

#contentObject

The data that will be indexed



121
122
123
# File 'lib/xapian/indexer/resource.rb', line 121

def content
	[@metadata[:content] || @body, @metadata[:title], @metadata[:description], @metadata[:keywords]].compact.join(" ")
end

#content?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/xapian/indexer/resource.rb', line 162

def content?
	@body != nil
end

#fetch!Object



148
149
150
151
152
153
154
155
156
# File 'lib/xapian/indexer/resource.rb', line 148

def fetch!
	@controller.load(self) do |status, header, body, |
		@fetched_on = Time.now
		@status = status
		@header = header
		@body = body
		@metadata = 
	end
end

#fetched?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/xapian/indexer/resource.rb', line 158

def fetched?
	@fetched_on != nil
end

#fresh?(at = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/xapian/indexer/resource.rb', line 129

def fresh?(at = Time.now)
	cache_control = @header['cache-control'] || ""
	fetched_age = @header['age'] || ""
	max_age = 3600
	
	if cache_control.match(/max-age=([0-9]+)/)
		max_age = $1.to_i
		
		if fetched_age.match(/([0-9]+)/)
			max_age -= $1.to_i
		end
	end
	
	age = at - @fetched_on
	
	# If the page is younger than the max_age the page can be considered fresh.
	return age < max_age
end


125
126
127
# File 'lib/xapian/indexer/resource.rb', line 125

def links
	@metadata[:links] if @metadata
end

#name_digestObject



166
167
168
# File 'lib/xapian/indexer/resource.rb', line 166

def name_digest
	"Q" + Digest::MD5.hexdigest(@name)
end

#to_hashObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/xapian/indexer/resource.rb', line 109

def to_hash
	{
		:fetched_on => @fetched_on,
		:name => @name,
		:status => @status,
		:header => @header,
		:body => @body,
		:metadata => @metadata
	}
end