hydra-works 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5992ee7b9f7184fd70053978e0604cd9ed10ccc
4
- data.tar.gz: 20f024bb82a6b76da36d5197dc7709879d44c690
3
+ metadata.gz: 3fe62ca3557324aa1d06b321706bef921ed64768
4
+ data.tar.gz: afc8ed7122ec5a3902905dca229a5a1768665090
5
5
  SHA512:
6
- metadata.gz: 76b34210f7d2fbb513108ed8637d0d7b302ee35f205ed9a2333dd479f34d576caf7bc8c3c399724de9100980ebe2a2e18efc6500e437924f41d657735ab9c593
7
- data.tar.gz: 3a213fa47a3feafd53474ede6e5b2211ee9706e647694e393655b3744e1b0f8b062a37adc461731561300876b123702ab6a65cf709aafe5f630dc9999187ce1e
6
+ metadata.gz: 9ca71d423754de86d680ac3e3dd28e7f367c202e5db3a3182e74fafa56ffe9dadcc29b731f9908f6efd97244561e05df7d6bccd5a6c271e492bdf5ac2366431e
7
+ data.tar.gz: f6ae7e93b05b5cfa69d22a48db2b163a72af5a61033bc88a0869ee25ab706b25843f257c1b7fba33775e99daf7d99cf310ec68c1c9e0ae78d2a3eb1513af9f04
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ fcrepo4-test-data/
20
20
  .DS_Store
21
21
  .ruby-gemset
22
22
  .ruby-version
23
+ /spec/examples.txt
data/.rspec CHANGED
@@ -1 +1,2 @@
1
- --colour
1
+ --color
2
+ --require spec_helper
data/lib/hydra/works.rb CHANGED
@@ -51,6 +51,8 @@ module Hydra
51
51
  autoload :UploadFileToFileSet
52
52
  autoload :PersistDerivative
53
53
  autoload :CharacterizationService
54
+ autoload :DetermineMimeType
55
+ autoload :DetermineOriginalName
54
56
  end
55
57
 
56
58
  ActiveFedora::WithMetadata::DefaultMetadataClassFactory.file_metadata_schemas +=
@@ -37,43 +37,19 @@ module Hydra::Works
37
37
 
38
38
  private
39
39
 
40
+ # Persist a new file with its containing file set; otherwise, just save the file itself
40
41
  def persist
41
42
  if current_file.new_record?
42
- # persist current_file and its membership in file_set.files container
43
43
  file_set.save
44
44
  else
45
- # we updated the content of an existing file, so we need to save the file explicitly
46
45
  current_file.save
47
46
  end
48
47
  end
49
48
 
50
49
  def attach_attributes(file)
51
50
  current_file.content = file
52
- current_file.original_name = determine_original_name(file)
53
- current_file.mime_type = determine_mime_type(file)
54
- end
55
-
56
- # Return mime_type based on methods available to file
57
- # @param object for mimetype to be determined. Attempts to use methods: :mime_type, :content_type, and :path.
58
- def determine_mime_type(file)
59
- return file.mime_type if file.respond_to? :mime_type
60
- return file.content_type if file.respond_to? :content_type
61
- return Hydra::PCDM::GetMimeTypeForFile.call(file.path) if file.respond_to? :path
62
- 'application/octet-stream'
63
- end
64
-
65
- # Return original_name based on methods available to file
66
- # @param object for original name to be determined. Attempts to use methods: :original_name, :original_filename, and :path.
67
- def determine_original_name(file)
68
- if file.respond_to? :original_name
69
- file.original_name
70
- elsif file.respond_to? :original_filename
71
- file.original_filename
72
- elsif file.respond_to? :path
73
- ::File.basename(file.path)
74
- else
75
- ''
76
- end
51
+ current_file.original_name = DetermineOriginalName.call(file)
52
+ current_file.mime_type = DetermineMimeType.call(file, current_file.original_name)
77
53
  end
78
54
 
79
55
  # @param [Symbol, RDF::URI] the type of association or filter to use
@@ -0,0 +1,39 @@
1
+ module Hydra::Works
2
+ class DetermineMimeType
3
+ # Determines the mime type for a given file
4
+ # @param [IO, File, Rack::Multipart::UploadedFile, #read] file
5
+ # @param [String, NilClass] original_name of the file
6
+ # @return [String]
7
+ def self.call(file, original_name = nil)
8
+ new(file, original_name).determine_mime_type
9
+ end
10
+
11
+ attr_reader :file, :original_name
12
+
13
+ def initialize(file, original_name)
14
+ @file = file
15
+ @original_name = original_name
16
+ end
17
+
18
+ def determine_mime_type
19
+ return file.mime_type if mime_type?
20
+ return file.content_type if content_type?
21
+ mime_type_from_name_or_path || 'application/octet-stream'
22
+ end
23
+
24
+ def mime_type_from_name_or_path
25
+ return Hydra::PCDM::GetMimeTypeForFile.call(original_name) if original_name.present?
26
+ return Hydra::PCDM::GetMimeTypeForFile.call(file.path) if file.respond_to?(:path)
27
+ end
28
+
29
+ private
30
+
31
+ def mime_type?
32
+ file.respond_to?(:mime_type) && file.mime_type.present?
33
+ end
34
+
35
+ def content_type?
36
+ file.respond_to?(:content_type) && file.content_type.present?
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ module Hydra::Works
2
+ class DetermineOriginalName
3
+ # Determines the original name for a given file
4
+ # @param [IO, File, Rack::Multipart::UploadedFile, #read] file
5
+ # @return [String]
6
+ def self.call(file)
7
+ return file.original_name if file.respond_to?(:original_name)
8
+ return file.original_filename if file.respond_to?(:original_filename)
9
+ return ::File.basename(file.path) if file.respond_to?(:path)
10
+ ''
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  module Hydra
2
2
  module Works
3
- VERSION = '0.13.0'.freeze
3
+ VERSION = '0.14.0'.freeze
4
4
  end
5
5
  end
@@ -54,7 +54,7 @@ describe Hydra::Works::AddFileToFileSet do
54
54
  end
55
55
 
56
56
  context 'when the file does not support any of the methods' do
57
- let(:file3) { double('file') }
57
+ let(:file3) { instance_double(File) }
58
58
  before do
59
59
  allow(file3).to receive(:read).and_return('')
60
60
  allow(file3).to receive(:size).and_return(0)
@@ -67,10 +67,10 @@ describe Hydra::Works::AddFileToFileSet do
67
67
  end
68
68
  end
69
69
 
70
- context 'file responds to :path but not to :mime_type nor :original_name' do
71
- it 'defaults to Hydra::PCDM for mimetype and ::File for basename.' do
72
- expect(Hydra::PCDM::GetMimeTypeForFile).to receive(:call).with(file.path)
73
- expect(::File).to receive(:basename).with(file.path)
70
+ context 'when determining mime type and name' do
71
+ it 'uses services to assign the values' do
72
+ expect(Hydra::Works::DetermineOriginalName).to receive(:call).with(file)
73
+ expect(Hydra::Works::DetermineMimeType).to receive(:call).with(file, nil)
74
74
  described_class.call(file_set, file, type)
75
75
  end
76
76
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hydra::Works::DetermineMimeType do
4
+ let(:original_name) { nil }
5
+ let(:file) { File.open(File.join(fixture_path, 'sample-file.pdf')) }
6
+
7
+ subject { described_class.call(file, original_name) }
8
+
9
+ context "when file has :mime_type" do
10
+ before { allow(file).to receive(:mime_type).and_return("mime_type") }
11
+ it { is_expected.to eq("mime_type") }
12
+ end
13
+
14
+ context "when file has :content_type" do
15
+ before { allow(file).to receive(:content_type).and_return("content_type") }
16
+ it { is_expected.to eq("content_type") }
17
+ end
18
+
19
+ context "when file has :path" do
20
+ it { is_expected.to eq("application/pdf") }
21
+ end
22
+
23
+ context "when an original_name is supplied" do
24
+ let(:original_name) { "some-other-file.txt" }
25
+ it { is_expected.to eq("text/plain") }
26
+ end
27
+
28
+ context "when an empty original_name is supplied" do
29
+ let(:original_name) { "" }
30
+ it { is_expected.to eq("application/pdf") }
31
+ end
32
+
33
+ context "when all else fails" do
34
+ before do
35
+ allow(file).to receive(:respond_to?).with(:mime_type).and_return(false)
36
+ allow(file).to receive(:respond_to?).with(:content_type).and_return(false)
37
+ allow(file).to receive(:respond_to?).with(:path).and_return(false)
38
+ end
39
+ it { is_expected.to eq("application/octet-stream") }
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hydra::Works::DetermineOriginalName do
4
+ let(:file) { File.open(File.join(fixture_path, 'sample-file.pdf')) }
5
+
6
+ subject { described_class.call(file) }
7
+
8
+ context "when file has :original_name" do
9
+ before { allow(file).to receive(:original_name).and_return("original_name") }
10
+ it { is_expected.to eq("original_name") }
11
+ end
12
+
13
+ context "when file has :original_filename" do
14
+ before { allow(file).to receive(:original_filename).and_return("original_filename") }
15
+ it { is_expected.to eq("original_filename") }
16
+ end
17
+
18
+ context "when file has :path" do
19
+ it { is_expected.to eq("sample-file.pdf") }
20
+ end
21
+
22
+ context "when all else fails" do
23
+ before do
24
+ allow(file).to receive(:respond_to?).with(:original_name).and_return(false)
25
+ allow(file).to receive(:respond_to?).with(:original_filename).and_return(false)
26
+ allow(file).to receive(:respond_to?).with(:path).and_return(false)
27
+ end
28
+ it { is_expected.to be_empty }
29
+ end
30
+ end
@@ -7,7 +7,7 @@ describe Hydra::Works::VirusCheckerService do
7
7
 
8
8
  context '.file_has_virus?' do
9
9
  it 'is a convenience method' do
10
- mock_object = double(file_has_virus?: true)
10
+ mock_object = instance_double(described_class, file_has_virus?: true)
11
11
  allow(described_class).to receive(:new).and_return(mock_object)
12
12
  described_class.file_has_virus?(file)
13
13
  expect(mock_object).to have_received(:file_has_virus?)
data/spec/spec_helper.rb CHANGED
@@ -28,18 +28,75 @@ Dir['./spec/support/**/*.rb'].each { |f| require f }
28
28
  # HttpLogger.log_headers = true
29
29
 
30
30
  RSpec.configure do |config|
31
- config.color = true
32
- config.tty = true
33
-
34
31
  # Uncomment the following line to get errors and backtrace for deprecation warnings
35
32
  # config.raise_errors_for_deprecations!
36
33
 
37
- # Use the specified formatter
38
- config.formatter = :progress
39
-
40
34
  config.before :each do |example|
41
35
  ActiveFedora::Cleaner.clean! unless example.metadata[:no_clean]
42
36
  end
37
+
38
+ # rspec-expectations config goes here. You can use an alternate
39
+ # assertion/expectation library such as wrong or the stdlib/minitest
40
+ # assertions if you prefer.
41
+ config.expect_with :rspec do |expectations|
42
+ # This option will default to `true` in RSpec 4. It makes the `description`
43
+ # and `failure_message` of custom matchers include text for helper methods
44
+ # defined using `chain`, e.g.:
45
+ # be_bigger_than(2).and_smaller_than(4).description
46
+ # # => "be bigger than 2 and smaller than 4"
47
+ # ...rather than:
48
+ # # => "be bigger than 2"
49
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
50
+ end
51
+
52
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
53
+ # have no way to turn it off -- the option exists only for backwards
54
+ # compatibility in RSpec 3). It causes shared context metadata to be
55
+ # inherited by the metadata hash of host groups and examples, rather than
56
+ # triggering implicit auto-inclusion in groups with matching metadata.
57
+ config.shared_context_metadata_behavior = :apply_to_host_groups
58
+
59
+ # The settings below are suggested to provide a good initial experience
60
+ # with RSpec, but feel free to customize to your heart's content
61
+
62
+ # This allows you to limit a spec run to individual examples or groups
63
+ # you care about by tagging them with `:focus` metadata. When nothing
64
+ # is tagged with `:focus`, all examples get run. RSpec also provides
65
+ # aliases for `it`, `describe`, and `context` that include `:focus`
66
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
67
+ config.filter_run_when_matching :focus
68
+
69
+ # Allows RSpec to persist some state between runs in order to support
70
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
71
+ # you configure your source control system to ignore this file.
72
+ config.example_status_persistence_file_path = 'spec/examples.txt'
73
+
74
+ # Many RSpec users commonly either run the entire suite or an individual
75
+ # file, and it's useful to allow more verbose output when running an
76
+ # individual spec file.
77
+ if config.files_to_run.one?
78
+ # Use the documentation formatter for detailed output,
79
+ # unless a formatter has already been configured
80
+ # (e.g. via a command-line flag).
81
+ config.default_formatter = 'doc'
82
+ end
83
+
84
+ # Print the 10 slowest examples and example groups at the
85
+ # end of the spec run, to help surface which specs are running
86
+ # particularly slow.
87
+ config.profile_examples = 10
88
+
89
+ # Run specs in random order to surface order dependencies. If you find an
90
+ # order dependency and want to debug it, you can fix the order by providing
91
+ # the seed, which is printed after each run.
92
+ # --seed 1234
93
+ config.order = :random
94
+
95
+ # Seed global randomization in this process using the `--seed` CLI option.
96
+ # Setting this allows you to use `--seed` to deterministically reproduce
97
+ # test failures related to randomization by passing the same `--seed` value
98
+ # as the one that triggered the failure.
99
+ Kernel.srand config.seed
43
100
  end
44
101
 
45
102
  def fixture_path
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydra-works
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-23 00:00:00.000000000 Z
11
+ date: 2016-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hydra-pcdm
@@ -227,6 +227,8 @@ files:
227
227
  - lib/hydra/works/not_file_set_validator.rb
228
228
  - lib/hydra/works/services/add_file_to_file_set.rb
229
229
  - lib/hydra/works/services/characterization_service.rb
230
+ - lib/hydra/works/services/determine_mime_type.rb
231
+ - lib/hydra/works/services/determine_original_name.rb
230
232
  - lib/hydra/works/services/persist_derivative.rb
231
233
  - lib/hydra/works/services/upload_file_to_file_set.rb
232
234
  - lib/hydra/works/services/virus_checker_service.rb
@@ -282,6 +284,8 @@ files:
282
284
  - spec/hydra/works/models/work_spec.rb
283
285
  - spec/hydra/works/services/add_file_to_file_set_spec.rb
284
286
  - spec/hydra/works/services/characterization_service_spec.rb
287
+ - spec/hydra/works/services/determine_mime_type_spec.rb
288
+ - spec/hydra/works/services/determine_original_name_spec.rb
285
289
  - spec/hydra/works/services/persist_derivatives_spec.rb
286
290
  - spec/hydra/works/services/upload_file_spec.rb
287
291
  - spec/hydra/works/services/virus_checker_service_spec.rb
@@ -318,7 +322,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
318
322
  version: '0'
319
323
  requirements: []
320
324
  rubyforge_project:
321
- rubygems_version: 2.4.5
325
+ rubygems_version: 2.6.4
322
326
  signing_key:
323
327
  specification_version: 4
324
328
  summary: Fundamental repository data model for hydra
@@ -356,6 +360,8 @@ test_files:
356
360
  - spec/hydra/works/models/work_spec.rb
357
361
  - spec/hydra/works/services/add_file_to_file_set_spec.rb
358
362
  - spec/hydra/works/services/characterization_service_spec.rb
363
+ - spec/hydra/works/services/determine_mime_type_spec.rb
364
+ - spec/hydra/works/services/determine_original_name_spec.rb
359
365
  - spec/hydra/works/services/persist_derivatives_spec.rb
360
366
  - spec/hydra/works/services/upload_file_spec.rb
361
367
  - spec/hydra/works/services/virus_checker_service_spec.rb