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.



115
116
117
# File 'lib/repub/app/fetcher.rb', line 115

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



116
117
118
# File 'lib/repub/app/fetcher.rb', line 116

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.



114
115
116
# File 'lib/repub/app/fetcher.rb', line 114

def url
  @url
end

Class Method Details

.cleanupObject



108
109
110
111
112
# File 'lib/repub/app/fetcher.rb', line 108

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

.for_url(url, &block) ⇒ Object



118
119
120
# File 'lib/repub/app/fetcher.rb', line 118

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

.rootObject



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

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

Instance Method Details

#assetsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/repub/app/fetcher.rb', line 140

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)


161
162
163
# File 'lib/repub/app/fetcher.rb', line 161

def cached?
  @cached == true
end

#empty?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/repub/app/fetcher.rb', line 157

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

#for_url(&block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/repub/app/fetcher.rb', line 122

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