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.


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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.


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

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.


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

def url
  @url
end

Class Method Details

.cleanupObject


110
111
112
113
114
# File 'lib/repub/app/fetcher.rb', line 110

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

.for_url(url, &block) ⇒ Object


120
121
122
# File 'lib/repub/app/fetcher.rb', line 120

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

.rootObject


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

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

Instance Method Details

#assetsObject


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

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)

163
164
165
# File 'lib/repub/app/fetcher.rb', line 163

def cached?
  @cached == true
end

#empty?Boolean

Returns:

  • (Boolean)

159
160
161
# File 'lib/repub/app/fetcher.rb', line 159

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

#for_url(&block) ⇒ Object


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

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