2019-08-14 09:48:44 +00:00
|
|
|
class LocalResource
|
2019-10-16 06:52:30 +00:00
|
|
|
attr_reader :uri
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def initialize(uri)
|
|
|
|
@uri = uri
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def file
|
|
|
|
@file ||= Tempfile.new(tmp_filename, tmp_folder, encoding: encoding).tap do |f|
|
|
|
|
io.rewind
|
|
|
|
f.write(io.read)
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def io
|
|
|
|
@io ||= uri.open
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def encoding
|
|
|
|
io.rewind
|
|
|
|
io.read.encoding
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def tmp_filename
|
|
|
|
[
|
|
|
|
Time.now.to_i.to_s,
|
|
|
|
Pathname.new(uri.path).extname
|
|
|
|
]
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def tmp_folder
|
|
|
|
# If we're using Rails:
|
|
|
|
Rails.root.join('tmp')
|
|
|
|
# Otherwise:
|
|
|
|
# '/wherever/you/want'
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|