Sha256: f92da81c2796e9e24c767267c852b693a947968b76180f9b2ceebae13ad54daa

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

# override the classes' read_page method and replace with one
# that will cache pages in spec/samples/{url}

module CacheExtensions
  # == Synopsis
  # Attach the read_page and cache_file methods to the given
  # class (cls) and use the given directory for the cache files
  def self.attach_to(cls, directory='/tmp')

    # define the read_page(page) method on the given class: cls
    cls.send('define_method', "read_page") do |page|
      data = nil
      filespec = page.gsub(/^http:\//, directory).gsub(/\/$/, '.html')
      if File.exist?(filespec)
        data = open(filespec).read
      else
        data = open(page).read
        cache_file(page, data)
      end
      data
    end

    # define the cache_file(page, data) method on the given class: cls
    cls.send('define_method', "cache_file") do |page, data|
      begin
        filespec = page.gsub(/^http:\//, directory).gsub(/\/$/, '.html')
        unless File.exist?(filespec)
          puts "caching #{filespec}"
          File.mkdirs(File.dirname(filespec))
          File.open(filespec, 'w') { |f| f.puts data }
        end
      rescue Exception => eMsg
        puts eMsg.to_s
      end
    end
  end

  # == Synopsis
  # Find all classes that have a read_page instance method and
  # then overwrite that read_page method with one that handles
  # the caching.  Use the given directory for the cache files.
  def self.attach_to_read_page_classes(directory='/tmp')
    ObjectSpace.each_object(Class) do |cls|
      if(cls.public_instance_methods(false).include?("read_page") ||
        cls.protected_instance_methods(false).include?("read_page") ||
        cls.private_instance_methods(false).include?("read_page"))
        CacheExtensions.attach_to(cls, directory)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 4 rubygems

Version Path
royw-dvdprofiler2xbmc-0.0.19 spec/cache_extensions.rb
royw-dvdprofiler_collection-0.1.2 spec/cache_extensions.rb
royw-imdb-0.1.2 spec/cache_extensions.rb
royw-tmdb-0.1.5 spec/cache_extensions.rb