Module: Crackup::Driver
- Included in:
- FileDriver, FtpDriver
- Defined in:
- lib/crackup/driver.rb,
lib/crackup/drivers/ftp.rb,
lib/crackup/drivers/file.rb
Overview
Base storage driver module for Crackup.
To write a Crackup storage driver:
-
Create a class in Crackup::Driver named “FooDriver”, where “Foo” is the capitalized version of the URI scheme your driver will handle (e.g., “Ftp”, “Sftp”, etc.).
-
Name your class file
foo.rb(“foo” being the lowercase URI scheme this time) and place it in Crackup’slib/crackup/driversdirectory. -
In your class, mixin the Crackup::Driver module and override at least the delete, get, and put methods.
That’s all there is to it. See Crackup::Driver::FileDriver and Crackup::Driver::FtpDriver for examples.
Defined Under Namespace
Classes: FileDriver, FtpDriver
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.get_driver(url) ⇒ Object
Gets an instance of the appropriate storage driver to handle the specified url.
Instance Method Summary collapse
-
#delete(url) ⇒ Object
Deletes the file at the specified url.
-
#get(url, local_filename) ⇒ Object
Downloads the file at url to local_filename.
-
#get_path(url) ⇒ Object
Gets the path portion of url.
- #initialize(url) ⇒ Object
-
#put(url, local_filename) ⇒ Object
Uploads the file at local_filename to url.
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
21 22 23 |
# File 'lib/crackup/driver.rb', line 21 def url @url end |
Class Method Details
.get_driver(url) ⇒ Object
Gets an instance of the appropriate storage driver to handle the specified url. If no suitable driver is found, raises a Crackup::StorageError.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/crackup/driver.rb', line 25 def self.get_driver(url) begin uri = URI::parse(url) rescue => e raise Crackup::StorageError, "Invalid URL: #{url}: #{e}" end # Use the filesystem driver if no scheme is specified or if the scheme is # a single letter (which indicates a Windows drive letter). if uri.scheme.nil? || uri.scheme =~ /^[a-z]$/i scheme = 'file' else scheme = uri.scheme.downcase end # Load the driver. unless require(File.dirname(__FILE__) + "/drivers/#{scheme}") raise Crackup::StorageError, "Driver not found for scheme '#{uri.scheme}'" end return const_get("#{scheme.capitalize}Driver").new(url) end |
Instance Method Details
#delete(url) ⇒ Object
Deletes the file at the specified url. This method does nothing and is intended to be overridden by a driver class.
54 55 56 |
# File 'lib/crackup/driver.rb', line 54 def delete(url) return false end |
#get(url, local_filename) ⇒ Object
Downloads the file at url to local_filename. This method does nothing and is intended to be overridden by a driver class.
60 61 62 |
# File 'lib/crackup/driver.rb', line 60 def get(url, local_filename) return false end |
#get_path(url) ⇒ Object
Gets the path portion of url.
65 66 67 68 69 70 71 |
# File 'lib/crackup/driver.rb', line 65 def get_path(url) uri = URI::parse(url) return uri.path rescue => e raise Crackup::StorageError, "Invalid URL: #{url}: #{e}" end |
#initialize(url) ⇒ Object
48 49 50 |
# File 'lib/crackup/driver.rb', line 48 def initialize(url) @url = url end |
#put(url, local_filename) ⇒ Object
Uploads the file at local_filename to url. This method does nothing and is intended to be overridden by a driver class.
75 76 77 |
# File 'lib/crackup/driver.rb', line 75 def put(url, local_filename) return false end |