class RankMirror::Cache

Public Class Methods

new(uri) click to toggle source
# File lib/rankmirror/cache.rb, line 5
def initialize(uri)
        @uri = uri
        @host = @uri.gsub(/^http(s):\/\//,"").gsub("/","_")
        @filename = File.join("/tmp",@host)
end

Public Instance Methods

fetch() click to toggle source
# 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?() click to toggle source
# 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_xml() click to toggle source
# 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