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.



85
86
87
88
89
90
91
92
93
94
# File 'lib/xapian/indexer/resource.rb', line 85

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.



99
100
101
# File 'lib/xapian/indexer/resource.rb', line 99

def body
  @body
end

#headerObject (readonly)

Returns the value of attribute header.



98
99
100
# File 'lib/xapian/indexer/resource.rb', line 98

def header
  @header
end

#metadataObject (readonly)

Returns the value of attribute metadata.



100
101
102
# File 'lib/xapian/indexer/resource.rb', line 100

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



96
97
98
# File 'lib/xapian/indexer/resource.rb', line 96

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



97
98
99
# File 'lib/xapian/indexer/resource.rb', line 97

def status
  @status
end

Instance Method Details

#contentObject

The data that will be indexed



114
115
116
# File 'lib/xapian/indexer/resource.rb', line 114

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

#content?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/xapian/indexer/resource.rb', line 155

def content?
	@body != nil
end

#fetch!Object



141
142
143
144
145
146
147
148
149
# File 'lib/xapian/indexer/resource.rb', line 141

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)


151
152
153
# File 'lib/xapian/indexer/resource.rb', line 151

def fetched?
	@fetched_on != nil
end

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

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/xapian/indexer/resource.rb', line 122

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


118
119
120
# File 'lib/xapian/indexer/resource.rb', line 118

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

#name_digestObject



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

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

#to_hashObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/xapian/indexer/resource.rb', line 102

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