Class: EchoUploads::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/echo_uploads/mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Mapper

Returns a new instance of Mapper.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/echo_uploads/mapper.rb', line 5

def initialize(file)
  unless(
    file.is_a?(ActionDispatch::Http::UploadedFile) or
    file.is_a?(Rack::Test::UploadedFile)
  )
    raise(
      "Expected file to be a ActionDispatch::Http::UploadedFile "+
      "or Rack::Test::UploadedFile, but was #{file.inspect}"
    )
  end
  
  @uploaded_file = file
  @outputs = []
end

Instance Attribute Details

#outputsObject (readonly)

Returns the value of attribute outputs.



20
21
22
# File 'lib/echo_uploads/mapper.rb', line 20

def outputs
  @outputs
end

Instance Method Details

#write(ext) {|path| ... } ⇒ Object

Yields:

  • (path)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/echo_uploads/mapper.rb', line 22

def write(ext)
  folder = ::File.join Rails.root, 'tmp/echo_uploads'
  FileUtils.mkdir_p folder
  path = ::File.join(folder, SecureRandom.hex(15) + ext)
  yield path
  file = ::File.open path, 'rb'
  mapped_file = ::EchoUploads::MappedFile.new(
    tempfile: file, filename: @uploaded_file.original_filename
  )
  mapped_file.mapped_filename = ::File.basename path
  outputs << mapped_file
end