Class: FileLocator
- Inherits:
-
Object
- Object
- FileLocator
- Defined in:
- lib/file_locator.rb
Defined Under Namespace
Classes: S3File
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #attachment ⇒ Object
- #exist? ⇒ Boolean (also: #exists?)
-
#initialize(source) ⇒ FileLocator
constructor
A new instance of FileLocator.
- #location ⇒ Object
- #reader ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(source) ⇒ FileLocator
Returns a new instance of FileLocator.
21 22 23 |
# File 'lib/file_locator.rb', line 21 def initialize(source) @source = source end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
5 6 7 |
# File 'lib/file_locator.rb', line 5 def source @source end |
Instance Method Details
#attachment ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/file_locator.rb', line 84 def case uri.scheme when 's3' uri when 'file' File.open(location,'r') else location end end |
#exist? ⇒ Boolean Also known as: exists?
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/file_locator.rb', line 61 def exist? case uri.scheme when 's3' S3File.new(uri).object.exists? when 'file' File.exist?(location) else false end end |
#location ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/file_locator.rb', line 50 def location case uri.scheme when 's3' S3File.new(uri).object.presigned_url(:get) when 'file' URI.decode(uri.path) else @uri.to_s end end |
#reader ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/file_locator.rb', line 73 def reader case uri.scheme when 's3' S3File.new(uri).object.get.body when 'file' File.open(location,'r') else Kernel::open(uri.to_s, 'r') end end |
#uri ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/file_locator.rb', line 25 def uri if @uri.nil? if source.is_a? File @uri = Addressable::URI.parse("file://#{URI.encode(File.expand_path(source))}") else encoded_source = source begin @uri = Addressable::URI.parse(encoded_source) rescue URI::InvalidURIError if encoded_source == source encoded_source = URI.encode(encoded_source) retry else raise end end if @uri.scheme.nil? @uri = Addressable::URI.parse("file://#{URI.encode(File.expand_path(source))}") end end end @uri end |