Skip to content

Commit

Permalink
Merge pull request #156 from projecthydra/input_object_or_string
Browse files Browse the repository at this point in the history
Allow input of file name or object that knows the file name.
  • Loading branch information
cjcolvar committed Apr 21, 2017
2 parents e2f522a + 25a3704 commit 1d37b3d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/hydra/derivatives.rb
Expand Up @@ -33,12 +33,12 @@ module Derivatives

autoload_under 'services' do
autoload :RetrieveSourceFileService
autoload :RemoteSourceFile
autoload :PersistOutputFileService
autoload :PersistBasicContainedOutputFileService
autoload :TempfileService
autoload :MimeTypeService
autoload :NullOutputFileService
autoload :UriSourceFileService
end

# Raised if the timout elapses
Expand Down
22 changes: 12 additions & 10 deletions lib/hydra/derivatives/runners/active_encode_derivatives.rb
@@ -1,21 +1,23 @@
module Hydra::Derivatives
class ActiveEncodeDerivatives < Runner
# TODO: object_or_filename - I'm currently passing a String
# filename during my testing, but we'll probably need to
# change this so that we can pass in the FileSet or File
# object that the source file is attached to.
#
# @param [String, ActiveFedora::Base] object_or_filename path to the source file, or an object
# @param [String, ActiveFedora::Base] object_or_filename source file name (or path), or an object that has a method that will return the file name
# @param [Hash] options options to pass to the encoder
# @option options [Symbol] :source a method that can be called on the object to retrieve the source file's name
# @options options [Array] :outputs a list of desired outputs
def self.create(object_or_filename, options)
file_name = object_or_filename
transform_directives(options.delete(:outputs)).each do |instructions|
processor = processor_class.new(file_name, instructions, output_file_service: output_file_service)
processor.process
source_file(object_or_filename, options) do |file_name|
transform_directives(options.delete(:outputs)).each do |instructions|
processor = processor_class.new(file_name, instructions, output_file_service: output_file_service)
processor.process
end
end
end

# Use the source service configured for this class or default to the remote file service
def self.source_file_service
@source_file_service || RemoteSourceFile
end

# Use the output service configured for this class or default to the null output service
def self.output_file_service
@output_file_service || NullOutputFileService
Expand Down
18 changes: 18 additions & 0 deletions lib/hydra/derivatives/services/remote_source_file.rb
@@ -0,0 +1,18 @@
# For the case where the source file is a remote file, and we
# don't want to download the file locally, just return the
# file name or file path (or whatever we need to pass to the
# encoding service so that it can find the file).

module Hydra::Derivatives
class RemoteSourceFile
# Finds the file name of the remote source file.
# @param [String, ActiveFedora::Base] String file name, or an object that has a method that will return the file name
# @param [Hash] options
# @option options [Symbol] :source a method that can be called on the object to retrieve the source file's name
# @yield [String] the file name
def self.call(object, options, &_block)
source_name = options.fetch(:source, :to_s)
yield(object.send(source_name))
end
end
end
13 changes: 0 additions & 13 deletions lib/hydra/derivatives/services/uri_source_file_service.rb

This file was deleted.

21 changes: 21 additions & 0 deletions spec/runners/active_encode_derivatives_spec.rb
@@ -0,0 +1,21 @@
require 'spec_helper'

class TestVideo < ActiveFedora::Base
attr_accessor :remote_file_name
end

describe Hydra::Derivatives::ActiveEncodeDerivatives do
context '.create' do
let(:file_path) { 'some/path/to/my_video.mp4' }
let(:video_record) { TestVideo.new(remote_file_name: file_path) }
let(:options) { { source: :remote_file_name, outputs: [low_res_video] } }
let(:low_res_video) { { some_key: 'some options to pass to my encoder service' } }
let(:processor) { double('processor') }

it 'calls the processor with the right arguments' do
expect(Hydra::Derivatives::Processors::ActiveEncode).to receive(:new).with(file_path, low_res_video, output_file_service: Hydra::Derivatives::NullOutputFileService).and_return(processor)
expect(processor).to receive(:process)
described_class.create(video_record, options)
end
end
end
33 changes: 33 additions & 0 deletions spec/services/remote_source_file_spec.rb
@@ -0,0 +1,33 @@
require 'spec_helper'

class TestObject < ActiveFedora::Base
attr_accessor :source_file_name
end

describe Hydra::Derivatives::RemoteSourceFile do
describe '.call' do
let(:file_name) { 'my_source_file.mp4' }

context 'when you pass in a String file name' do
let(:input_obj) { file_name }
let(:options) { Hash.new }

it 'it yields the file name' do
expect do |blk|
described_class.call(input_obj, options, &blk)
end.to yield_with_args(file_name)
end
end

context 'when you pass in an ActiveFedora::Base object ' do
let(:input_obj) { TestObject.new(source_file_name: file_name) }
let(:options) { { source: :source_file_name } }

it 'it yields the file name' do
expect do |blk|
described_class.call(input_obj, options, &blk)
end.to yield_with_args(file_name)
end
end
end
end

0 comments on commit 1d37b3d

Please sign in to comment.