Class: Repub::App::Fetcher::Cache

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/repub/app/fetcher.rb

Constant Summary

Constants included from Logger

Logger::LOGGER_NORMAL, Logger::LOGGER_QUIET, Logger::LOGGER_VERBOSE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



104
105
106
# File 'lib/repub/app/fetcher.rb', line 104

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



105
106
107
# File 'lib/repub/app/fetcher.rb', line 105

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.



103
104
105
# File 'lib/repub/app/fetcher.rb', line 103

def url
  @url
end

Class Method Details

.cleanupObject



97
98
99
100
101
# File 'lib/repub/app/fetcher.rb', line 97

def self.cleanup
  Dir.chdir(self.root) { FileUtils.rm_r(Dir.glob('*')) }
rescue
  # ignore exceptions
end

.for_url(url, &block) ⇒ Object



107
108
109
# File 'lib/repub/app/fetcher.rb', line 107

def self.for_url(url, &block)
  self.new(url).for_url(&block)
end

.rootObject



93
94
95
# File 'lib/repub/app/fetcher.rb', line 93

def self.root
  return File.join(App.data_path, 'cache')
end

Instance Method Details

#assetsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/repub/app/fetcher.rb', line 129

def assets
  unless @assets
    # Enumerate assets
    Dir.chdir(@path) do
      @assets = {}
      AssetTypes.each_pair do |asset_type, file_types|
        @assets[asset_type] ||= []
        file_types.each do |file_type|
          @assets[asset_type] << Dir.glob("*.#{file_type}")
        end
        @assets[asset_type].flatten!
      end
    end
  end
  @assets
end

#cached?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/repub/app/fetcher.rb', line 150

def cached?
  @cached == true
end

#empty?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/repub/app/fetcher.rb', line 146

def empty?
  Dir.glob(File.join(@path, '*')).empty?
end

#for_url(&block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/repub/app/fetcher.rb', line 111

def for_url(&block)
  # Download stuff if not yet cached
  @cached = File.exist?(@path)
  unless @cached
    FileUtils.mkdir_p(@path) 
    begin
      Dir.chdir(@path) { yield self }
    rescue
      FileUtils.rm_r(@path)
      raise
    end
  else
    log.info "Using cached assets"
    log.debug "-- Cache is #{@path}"
  end
  self
end