Class: Unbreakable::DataStorage::FileDataStore
- Inherits:
-
Dragonfly::DataStorage::FileDataStore
- Object
- Dragonfly::DataStorage::FileDataStore
- Unbreakable::DataStorage::FileDataStore
- Includes:
- Dragonfly::Loggable, Observable
- Defined in:
- lib/unbreakable/data_storage/file_data_store.rb
Overview
Stores files to the filesystem. To configure:
scraper.configure do |c|
c.datastore = Unbreakable::DataStorage::FileDataStore.new(scraper,
:decorators => [:timeout], # optional
:observers => [:log]) # optional
c.datastore.root_path = '/path/dir' # default '/var/tmp/unbreakable'
c.datastore. = true # default false
end
Instance Method Summary collapse
-
#clobber ⇒ Boolean, ...
Configure the datastore to overwrite files upon repeated download.
-
#defer_store(opts = {}, &block) ⇒ String
Stores a record in the datastore.
-
#initialize(app, opts = {}) ⇒ FileDataStore
constructor
A new instance of FileDataStore.
-
#records(pattern = nil) ⇒ Array<String>
Returns all filenames matching a pattern, if given.
Constructor Details
#initialize(app, opts = {}) ⇒ FileDataStore
Returns a new instance of FileDataStore.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/unbreakable/data_storage/file_data_store.rb', line 37 def initialize(app, opts = {}) use_same_log_as(app) use_as_fallback_config(app) if opts[:decorators] opts[:decorators].each do |decorator| extend Symbol === decorator ? Unbreakable::Decorators.const_get(decorator.capitalize) : decorator end end if opts[:observers] opts[:observers].each do |observer| add_observer Symbol === observer ? Unbreakable::Observers.const_get(observer.capitalize).new(self) : observer.new(self) end end end |
Instance Method Details
#clobber ⇒ Boolean, ...
Configure the datastore to overwrite files upon repeated download.
scraper.configure do |c|
c.datastore.clobber = true # default false
end
29 |
# File 'lib/unbreakable/data_storage/file_data_store.rb', line 29 configurable_attr :clobber, false |
#defer_store(opts = {}, &block) ⇒ String
Stores a record in the datastore. This method does lazy evaluation of the record’s contents, e.g.:
defer_store(:path => 'index.html') do
open('http://www.example.com/').read
end
The open
method is called only if the record hasn’t already been downloaded or if the datastore has been configured to overwrite files upon repeated download.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/unbreakable/data_storage/file_data_store.rb', line 70 def defer_store(opts = {}, &block) = opts[:meta] || {} relative_path = if opts[:path] opts[:path] else filename = [:name] || 'file' relative_path = relative_path_for(filename) end changed if empty?(relative_path) or clobber?(relative_path) begin path = absolute(relative_path) prepare_path(path) string = yield_block(relative_path, &block) Dragonfly::TempObject.new(string).to_file(path).close (path, ) if notify_observers :store, relative_path, string relative(path) rescue InvalidRemoteFile => e log.error e. rescue Errno::EACCES => e raise UnableToStore, e. end else notify_observers :skip, relative_path end end |
#records(pattern = nil) ⇒ Array<String>
Returns all filenames matching a pattern, if given.
102 103 104 105 106 107 108 109 110 |
# File 'lib/unbreakable/data_storage/file_data_store.rb', line 102 def records(pattern = nil) if pattern Dir[File.join(root_path, '**', pattern)] else Dir[File.join(root_path, '**', '*')] end.map do |absolute_path| relative absolute_path end end |