Class: RankMirror::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/rankmirror/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Cache

Returns a new instance of Cache.



5
6
7
8
9
# File 'lib/rankmirror/cache.rb', line 5

def initialize(uri)
	@uri = uri
	@host = @uri.gsub(/^http(s):\/\//,"").gsub("/","_")
	@filename = File.join("/tmp",@host)
end

Instance Method Details

#fetchObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rankmirror/cache.rb', line 11

def fetch
	unless is_recent?
		buffer = open(@filename,'w')
		r = Curl::Easy.new(@uri)
		r.on_body do |b|
			buffer.write b
		end
		r.perform
		buffer.close
		to_xml
	end
	return @filename + ".xml"
end

#is_recent?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rankmirror/cache.rb', line 35

def is_recent?
	if File.exist?(@filename + ".xml")
		last_time = File.mtime(@filename + ".xml")
		# one week
		if Time.now - last_time < 60*60*24*7
			true
		else
			false
		end
	else
		false
	end
end

#to_xmlObject



25
26
27
28
29
30
31
32
33
# File 'lib/rankmirror/cache.rb', line 25

def to_xml
	buffer = open(@filename) {|f|
			f.read
		}
	doc = Nokogiri::XML(buffer)
	f = open(@filename + ".xml",'w')
	doc.write_xml_to(f)
	f.close
end