Skip to content

Commit

Permalink
Merge pull request #16 from jobready/feature/RTO-12031-update-happi-h…
Browse files Browse the repository at this point in the history
…andle-file-ext

RTO-12031: Handle original filenames
  • Loading branch information
chesterl authored Mar 4, 2019
2 parents a5e7967 + 3f7c436 commit 8589c83
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/happi/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Happi::File
attr_accessor :file_name
attr_accessor :mime_type
attr_accessor :original_filename

def initialize(file)
if file.is_a?(String)
Expand All @@ -11,6 +12,7 @@ def initialize(file)
else
@mime_type = file.content_type
@file_name = file.path
@original_filename = file.original_filename
end
end

Expand All @@ -19,7 +21,7 @@ def exists?
end

def multipart
Faraday::UploadIO.new(file_name, mime_type) if exists?
Faraday::UploadIO.new(file_name, mime_type, original_filename) if exists?
end

def encode_file
Expand Down
36 changes: 26 additions & 10 deletions spec/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
context "with a file name" do
subject { Happi::File.new(__FILE__) }

describe '#initialize' do
it 'assigns the correct attributes' do
aggregate_failures do
expect(subject.mime_type).to eq('application/x-ruby')
expect(subject.file_name).to include('spec/file_spec.rb')
expect(subject.original_filename).to be_nil
end
end
end

describe '#encode_file' do
encoded = Base64.encode64(File.read(__FILE__))
specify { expect(subject.encode_file).to eql(encoded) }
end

describe '#mime_type' do
specify { expect(subject.mime_type).to eql('application/x-ruby') }
end

describe '#multipart' do
# specify { expect(subject.multipart).to }
specify { expect(subject.multipart).to be_an_instance_of(Faraday::UploadIO) }
specify { expect(subject.multipart.content_type).to eq('application/x-ruby') }
specify { expect(subject.multipart.original_filename).to eq('file_spec.rb') }
end

describe '#exists?' do
Expand All @@ -26,17 +34,25 @@
context "with an ActionDispatch::Http::UploadedFile" do
subject { Happi::File.new(Rack::Test::UploadedFile.new(__FILE__, 'application/x-ruby')) }

describe '#initialize' do
it 'assigns the correct attributes' do
aggregate_failures do
expect(subject.mime_type).to eq('application/x-ruby')
expect(subject.file_name).not_to eq('file_spec.rb')
expect(subject.original_filename).to eq('file_spec.rb')
end
end
end

describe '#encode_file' do
encoded = Base64.encode64(File.read(__FILE__))
specify { expect(subject.encode_file).to eql(encoded) }
end

describe '#mime_type' do
specify { expect(subject.mime_type).to eql('application/x-ruby') }
end

describe '#multipart' do
# specify { expect(subject.multipart).to }
specify { expect(subject.multipart).to be_an_instance_of(Faraday::UploadIO) }
specify { expect(subject.multipart.content_type).to eq('application/x-ruby') }
specify { expect(subject.multipart.original_filename).to include('file_spec') }
end

describe '#exists?' do
Expand Down

0 comments on commit 8589c83

Please sign in to comment.