Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erc form #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions lib/mrt/ingest/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ module Ingest
# #File.
class Component # :nodoc:

attr_reader :server, :uri
attr_reader :uri

def initialize(server, location, options)
@server = server
def initialize(location, options)
@name = options[:name]
@digest = options[:digest]
@mime_type = options[:mime_type]
Expand All @@ -18,25 +17,6 @@ def initialize(server, location, options)
init_uri(location)
end

class << self
def from_erc(server, erc)
return Component.new(server, erc, name: 'mrt-erc.txt') if erc.is_a?(URI) || erc.is_a?(File)
return from_hash(server, erc) if erc.is_a?(Hash)

raise ArgumentError, 'Bad ERC supplied: must be a URI, File, or Hash'
end

def from_hash(server, erc_h)
uri_str, path = server.add_file do |f|
f.write("erc:\n")
erc_h.each_pair { |k, v| f.write("#{k}: #{v}\n") }
end

digest = Mrt::Ingest::MessageDigest::MD5.from_file(File.new(path))
Component.new(server, URI.parse(uri_str), name: 'mrt-erc.txt', digest: digest)
end
end

def to_manifest_entry
"#{@uri} | #{digest_type} | #{digest_value} | #{@size} | | #{@name} | #{@mime_type}\n"
end
Expand Down Expand Up @@ -64,7 +44,7 @@ def init_uri(location)

def init_from_file(file)
@name = File.basename(file.path) if @name.nil?
@uri = server.add_file(file)[0]
# @uri = server.add_file(file)[0]
@digest = Mrt::Ingest::MessageDigest::MD5.from_file(file) if @digest.nil?
@size = File.size(file.path) if @size.nil?
end
Expand Down
33 changes: 9 additions & 24 deletions lib/mrt/ingest/iobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ module Ingest
# An object prepared for ingest into Merritt.
class IObject

attr_accessor :primary_identifier, :local_identifier, :erc
attr_reader :server
attr_accessor :primary_identifier, :local_identifier, :erc, :what, :who, :when

# Options can have the keys :primary_identifier,
# :local_identifier, :server, or :erc. :erc can be a #File, #Uri
# or a #Hash of metadata. :server is a #OneTimeServer.
# :local_identifier, or :erc. :erc can be a #File, #Uri
# or a #Hash of metadata.
def initialize(options = {})
@primary_identifier = options[:primary_identifier]
@local_identifier = options[:local_identifier]
@erc = options[:erc] || {}
@components = []
@server = options[:server] || Mrt::Ingest::OneTimeServer.new
end

# Add a component to the object. where can be either a #URI or a
Expand All @@ -32,33 +30,20 @@ def initialize(options = {})
# subclass of Mrt::Ingest::MessageDigest::Base. If where is a
# #File, it will be hosted on an embedded web server.
def add_component(where, options = {})
@components.push(Component.new(@server, where, options))
@components.push(Component.new(where, options))
end

# Make a Mrt::Ingest::Request object for this mrt-object
def mk_request(profile, user_agent)
manifest_file = Tempfile.new('mrt-ingest')
erc_component = Component.from_erc(@server, @erc)
mk_manifest(manifest_file, erc_component)
mk_manifest(manifest_file)
# reset to beginning
manifest_file.open
new_request(manifest_file, profile, user_agent)
end

def start_server # :nodoc:
@server.start_server
end

def join_server # :nodoc:
@server.join_server
end

def stop_server # :nodoc:
@server.stop_server
end

# rubocop:disable Metrics/LineLength
def mk_manifest(manifest, erc_component) # :nodoc:
def mk_manifest(manifest) # :nodoc:
manifest.write("#%checkm_0.7\n")
manifest.write("#%profile http://uc3.cdlib.org/registry/ingest/manifest/mrt-ingest-manifest\n")
manifest.write("#%prefix | mrt: | http://uc3.cdlib.org/ontology/mom#\n")
Expand All @@ -67,7 +52,6 @@ def mk_manifest(manifest, erc_component) # :nodoc:
@components.each do |c|
manifest.write(c.to_manifest_entry)
end
manifest.write(erc_component.to_manifest_entry)
manifest.write("#%EOF\n")
end
# rubocop:enable Metrics/LineLength
Expand All @@ -76,15 +60,13 @@ def mk_manifest(manifest, erc_component) # :nodoc:
# submitter.
def start_ingest(client, profile, submitter)
request = mk_request(profile, submitter)
start_server
@response = client.ingest(request)
end

# Wait for the ingest of this object to finish.
def finish_ingest
# XXX Right now we only join the hosting server; in the future
# we will check the status via the ingest server.
join_server
end

private
Expand All @@ -96,6 +78,9 @@ def new_request(manifest_file, profile, user_agent)
type: 'object-manifest',
submitter: user_agent,
profile: profile,
title: @erc['what'],
creator: @erc['who'],
date: @erc['when'],
local_identifier: @local_identifier,
primary_identifier: @primary_identifier
)
Expand Down
111 changes: 0 additions & 111 deletions lib/mrt/ingest/one_time_server.rb

This file was deleted.

9 changes: 5 additions & 4 deletions mrt-ingest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $LOAD_PATH.push File.expand_path('lib', __dir__)
Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.4.0'
s.name = 'mrt-ingest'
s.version = '0.0.6'
s.version = '0.0.10'
s.platform = Gem::Platform::RUBY
s.authors = ['Mark Reyes', 'David Moles']
s.email = ['[email protected]', '[email protected]']
Expand All @@ -12,10 +12,11 @@ Gem::Specification.new do |s|
s.description = 'A client for the Merritt ingest system. More details available from https://github.com/CDLUC3/mrt-doc/wiki'
s.license = 'BSD-3-Clause'

s.add_dependency 'json', '~> 2.0'
s.add_dependency 'rest-client', '~> 2.0'
# s.add_dependency 'json', '~> 2.1'
s.add_dependency 'rest-client', '~> 2.1'

s.add_development_dependency 'bundler', '>= 2.2.10'

# s.add_development_dependency 'bundler', '>= 2.2.10'
s.add_development_dependency 'checkm', '0.0.6'
s.add_development_dependency 'mocha', '~> 1.7'
s.add_development_dependency 'rake', '~> 12.0'
Expand Down
2 changes: 0 additions & 2 deletions spec/unit/mrt/ingest/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Mrt::Ingest
describe Component do
describe :from_erc do
it 'rejects string ERCs' do
server = instance_double(OneTimeServer)
expect { Component.from_erc(server, 'I am not an ERC') }.to raise_error(ArgumentError)
end
end
end
Expand Down
11 changes: 2 additions & 9 deletions spec/unit/mrt/ingest/iobject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ def parse_erc_entry(erc_entry)
def check_erc_content(iobject, asserted_erc)
erc_entry = get_uri_for_name(iobject, 'mrt-erc.txt')
expect(erc_entry).not_to be_nil
iobject.start_server
begin
expect(parse_erc_entry(erc_entry)).to eq(asserted_erc)
ensure
iobject.stop_server
end
end

Expand Down Expand Up @@ -137,11 +135,9 @@ def check_erc_content(iobject, asserted_erc)

it 'should serve a valid mrt-erc.txt entry' do
expect(@erc_entry).not_to be_nil
@iobject.start_server
begin
open(@erc_entry.values[0]).read.lines.to_a
ensure
@iobject.stop_server
end
end

Expand Down Expand Up @@ -186,11 +182,9 @@ def check_erc_content(iobject, asserted_erc)
manifest = parse_object_manifest(iobject)
expect(manifest).not_to(be_nil)
expect(uri_entry).not_to be_nil
iobject.start_server
begin
expect(open(uri_entry.values[0]).read).to eq(FILE_CONTENT)
ensure
iobject.stop_server
end
end
end
Expand Down Expand Up @@ -221,9 +215,8 @@ def check_erc_content(iobject, asserted_erc)
@iobject.start_ingest(@client, 'example_profile', 'Atom processor/Example collection')

# TODO: just mock the server
server = @iobject.server
files = Dir.entries(server.dir).reject { |e| %w[. ..].include?(e) }
urls = files.map { |f| "http://#{Socket.gethostname}:#{server.port}/#{f}" }
# files = Dir.entries(server.dir).reject { |e| %w[. ..].include?(e) }
# urls = files.map { |f| "http://#{Socket.gethostname}:#{server.port}/#{f}" }

client_process_id = fork do
begin
Expand Down
Loading