Class: FetchAndProcess::Base
- Inherits:
-
Object
- Object
- FetchAndProcess::Base
- Defined in:
- lib/fetch_and_process/base.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Class Method Summary collapse
- .add_handler(scheme, method) ⇒ Object
- .handle?(uri) ⇒ Boolean
- .handler(uri) ⇒ Object
- .handlers ⇒ Object
Instance Method Summary collapse
- #cache_location ⇒ Object
-
#download ⇒ Object
After handle is executed, the fetched file should exist on the path specified by ‘target`.
- #handle? ⇒ Boolean
- #handler ⇒ Object
-
#initialize(file_path) ⇒ Base
constructor
A new instance of Base.
- #process ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(file_path) ⇒ Base
Returns a new instance of Base.
10 11 12 |
# File 'lib/fetch_and_process/base.rb', line 10 def initialize(file_path) @file_path = file_path end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/fetch_and_process/base.rb', line 8 def file_path @file_path end |
Class Method Details
.add_handler(scheme, method) ⇒ Object
56 57 58 |
# File 'lib/fetch_and_process/base.rb', line 56 def add_handler(scheme, method) handlers[scheme.to_sym] = method.to_sym end |
.handle?(uri) ⇒ Boolean
48 49 50 |
# File 'lib/fetch_and_process/base.rb', line 48 def handle?(uri) handlers.key? uri.scheme.to_sym end |
.handler(uri) ⇒ Object
52 53 54 |
# File 'lib/fetch_and_process/base.rb', line 52 def handler(uri) handlers[uri.scheme.to_sym] end |
.handlers ⇒ Object
61 62 63 |
# File 'lib/fetch_and_process/base.rb', line 61 def self.handlers @handlers ||= {} end |
Instance Method Details
#cache_location ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/fetch_and_process/base.rb', line 38 def cache_location @cache_location ||= begin hash = Digest::MD5.hexdigest uri.to_s path = "#{Dir.tmpdir}/fetch_and_process" Dir.mkdir(path, 0o700) unless File.exist?(path) "#{path}/#{hash}" end end |
#download ⇒ Object
After handle is executed, the fetched file should exist on the path specified by ‘target`
24 25 26 27 28 |
# File 'lib/fetch_and_process/base.rb', line 24 def download raise FetchAndProcess::UnsupportedFileError unless uri.scheme && handle? send(handler) end |
#handle? ⇒ Boolean
30 31 32 |
# File 'lib/fetch_and_process/base.rb', line 30 def handle? self.class.handle?(uri) end |
#handler ⇒ Object
34 35 36 |
# File 'lib/fetch_and_process/base.rb', line 34 def handler self.class.handler(uri) end |
#process ⇒ Object
18 19 20 21 |
# File 'lib/fetch_and_process/base.rb', line 18 def process # No action defined by default raise FetchAndProcess::UnimplementedFileError end |
#uri ⇒ Object
14 15 16 |
# File 'lib/fetch_and_process/base.rb', line 14 def uri @uri ||= URI.parse(file_path) end |