Skip to content

Commit

Permalink
simplify, simplify, simplify (#12)
Browse files Browse the repository at this point in the history
simplify, simplify, simplify
  • Loading branch information
mjgiarlo authored Dec 3, 2019
2 parents 3bbef9d + 87f4b3a commit 734ada7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-12-03 11:09:29 -0800 using RuboCop version 0.74.0.
# on 2019-12-03 15:41:39 -0800 using RuboCop version 0.74.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
Metrics/AbcSize:
Max: 31
Max: 34
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ Note that the preservation service is behind a firewall.
- Preservation::Client.objects.current_version('oo000oo0000') (can also be 'druid:oo000oo0000')
- Preservation::Client.objects.checksums(druids: druids) - will return raw csv
- Preservation::Client.objects.checksums(druids: druids, format: 'json') - will return json
- Preservation::Client.objects.content('oo000oo0000', 'my_file.pdf') - will return contents of my_file.pdf in most recent version of Moab object
- Preservation::Client.objects.content('oo000oo0000', 'my_file.pdf', '1') - will return contents of my_file.pdf in version 1 of Moab object
- Preservation::Client.objects.manifest('oo000oo0000', 'versionInventory.xml') - will return contents of versionInventory.xml in most recent version of Moab object
- Preservation::Client.objects.manifest('oo000oo0000', 'versionInventory.xml', '3') - will return contents of versionInventory.xml in version 3 of Moab object
- Preservation::Client.objects.metadata('oo000oo0000', 'identityMetadata.xml') - will return contents of identityMetadata.xml in most recent version of Moab object
- Preservation::Client.objects.metadata('oo000oo0000', 'identityMetadata.xml', '8') - will return contents of identityMetadata.xml in version 8 of Moab object
- Preservation::Client.objects.signature_catalog('oo000oo0000') - will return contents of latest version of signatureCatalog.xml from Moab object
- Preservation::Client.objects.content(druid: 'oo000oo0000', filepath: 'my_file.pdf') - will return contents of my_file.pdf in most recent version of Moab object
- Preservation::Client.objects.content(druid: 'oo000oo0000', filepath: 'my_file.pdf', version: '1') - will return contents of my_file.pdf in version 1 of Moab object
- Preservation::Client.objects.manifest(druid: 'oo000oo0000', filepath: 'versionInventory.xml') - will return contents of versionInventory.xml in most recent version of Moab object
- Preservation::Client.objects.manifest(druid: 'oo000oo0000', filepath: 'versionInventory.xml', version: '3') - will return contents of versionInventory.xml in version 3 of Moab object
- Preservation::Client.objects.metadata(druid: 'oo000oo0000', filepath: 'identityMetadata.xml') - will return contents of identityMetadata.xml in most recent version of Moab object
- Preservation::Client.objects.metadata(druid: 'oo000oo0000', filepath: 'identityMetadata.xml', version: '8') - will return contents of identityMetadata.xml in version 8 of Moab object
- Preservation::Client.objects.signature_catalog(druid: 'oo000oo0000') - will return contents of latest version of signatureCatalog.xml from Moab object

## Development

Expand Down
16 changes: 8 additions & 8 deletions lib/preservation/client/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@ class Client
# API calls that are about Preserved Objects
class Objects < VersionedApiService
# @param [Array] druids - required list of druids with or without prefix: 'druid:ab123cd4567' OR 'ab123cd4567'
# @param [String] :resp_format - desired format of the HTTP response (default csv, json also possible)
# @param [String] resp_format - desired format of the HTTP response (default csv, json also possible)
# @return body of HTTP response from Preservation API - the checksums and filesize for each druid
def checksums(druids: [], resp_format: 'csv')
post('objects/checksums', { druids: druids, format: resp_format }, 'checksums')
post('objects/checksums', druids: druids, format: resp_format)
end

# @param [String] druid - with or without prefix: 'druid:ab123cd4567' OR 'ab123cd4567'
# @return [Integer] the current version of the Preserved Object
def current_version(druid)
resp_body = get_json("objects/#{druid}.json", druid, 'current_version')
resp_body = get_json("objects/#{druid}.json", druid)
resp_body[:current_version]
end

# retrieve a content file from a Moab object
# @param [String] druid - with or without prefix: 'druid:ab123cd4567' OR 'ab123cd4567'
# @param [String] filepath - the path of the file relative to the moab content directory
# @param [String] version - the version of the file requested (defaults to nil for latest version)
def content(druid, filepath, version = nil)
def content(druid:, filepath:, version: nil)
file(druid, 'content', filepath, version)
end

# retrieve a manifest file from a Moab object
# @param [String] druid - with or without prefix: 'druid:ab123cd4567' OR 'ab123cd4567'
# @param [String] filepath - the path of the file relative to the moab manifest directory
# @param [String] version - the version of the file requested (defaults to nil for latest version)
def manifest(druid, filepath, version = nil)
def manifest(druid:, filepath:, version: nil)
file(druid, 'manifest', filepath, version)
end

# retrieve a metadata file from a Moab object
# @param [String] druid - with or without prefix: 'druid:ab123cd4567' OR 'ab123cd4567'
# @param [String] filepath - the path of the file relative to the moab metadata directory
# @param [String] version - the version of the file requested (defaults to nil for latest version)
def metadata(druid, filepath, version = nil)
def metadata(druid:, filepath:, version: nil)
file(druid, 'metadata', filepath, version)
end

# convenience method for retrieving latest signatureCatalog.xml file from a Moab object
# @param [String] druid - with or without prefix: 'druid:ab123cd4567' OR 'ab123cd4567'
def signature_catalog(druid)
manifest(druid, 'signatureCatalog.xml')
manifest(druid: druid, filepath: 'signatureCatalog.xml')
end

private
Expand All @@ -57,7 +57,7 @@ def signature_catalog(druid)
# @param [String] version - the version of the file requested (defaults to nil for latest version)
# @return the retrieved file
def file(druid, category, filepath, version = nil)
get("objects/#{druid}/file", { category: category, filepath: filepath, version: version }, 'file')
get("objects/#{druid}/file", category: category, filepath: filepath, version: version)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/preservation/client/versioned_api_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(connection:, api_version:)
attr_reader :connection, :api_version

# @param path [String] path to be appended to connection url (no leading slash)
def get_json(path, object_id, caller_method_name)
def get_json(path, object_id)
req_url = api_version.present? ? "#{api_version}/#{path}" : path
resp = connection.get do |req|
req.url req_url
Expand All @@ -28,7 +28,7 @@ def get_json(path, object_id, caller_method_name)
raise Preservation::Client::NotFoundError, errmsg
else
errmsg = ResponseErrorFormatter
.format(response: resp, object_id: object_id, client_method_name: caller_method_name)
.format(response: resp, object_id: object_id, client_method_name: caller_locations.first.label)
raise Preservation::Client::UnexpectedResponseError, errmsg
end
rescue Faraday::ResourceNotFound => e
Expand All @@ -41,13 +41,13 @@ def get_json(path, object_id, caller_method_name)

# @param path [String] path to be appended to connection url (no leading slash)
# @param params [Hash] optional params
def get(path, params, caller_method_name)
def get(path, params)
get_path = api_version.present? ? "#{api_version}/#{path}" : path
resp = connection.get get_path, params
return resp.body if resp.success?

errmsg = ResponseErrorFormatter
.format(response: resp, client_method_name: caller_method_name)
.format(response: resp, client_method_name: caller_locations.first.label)
raise Preservation::Client::UnexpectedResponseError, errmsg
rescue Faraday::ResourceNotFound => e
errmsg = "HTTP GET to #{connection.url_prefix}#{path} failed with #{e.class}: #{e.message}"
Expand All @@ -59,13 +59,13 @@ def get(path, params, caller_method_name)

# @param path [String] path to be appended to connection url (no leading slash)
# @param params [Hash] optional params
def post(path, params, caller_method_name)
def post(path, params)
post_path = api_version.present? ? "#{api_version}/#{path}" : path
resp = connection.post post_path, params
return resp.body if resp.success?

errmsg = ResponseErrorFormatter
.format(response: resp, client_method_name: caller_method_name)
.format(response: resp, client_method_name: caller_locations.first.label)
raise Preservation::Client::UnexpectedResponseError, errmsg
rescue Faraday::ResourceNotFound => e
errmsg = "HTTP POST to #{connection.url_prefix}#{path} failed with #{e.class}: #{e.message}"
Expand Down
48 changes: 24 additions & 24 deletions spec/preservation/client/objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
end

before do
allow(subject).to receive(:get_json).with(path, druid, 'current_version').and_return(valid_response_body)
allow(subject).to receive(:get_json).with(path, druid).and_return(valid_response_body)
end

it 'returns the current version as an integer' do
Expand All @@ -39,7 +39,7 @@

context 'when API request fails' do
before do
allow(subject).to receive(:get_json).with(path, druid, 'current_version').and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
allow(subject).to receive(:get_json).with(path, druid).and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
end

it 'raises an error' do
Expand All @@ -64,7 +64,7 @@
end

before do
allow(subject).to receive(:post).with(path, params, 'checksums').and_return(valid_csv_response)
allow(subject).to receive(:post).with(path, params).and_return(valid_csv_response)
end

it 'returns the API response' do
Expand All @@ -74,7 +74,7 @@

context 'when API request fails' do
before do
allow(subject).to receive(:post).with(path, params, 'checksums').and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
allow(subject).to receive(:post).with(path, params).and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
end

it 'raises an error' do
Expand Down Expand Up @@ -103,11 +103,11 @@
end

before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_return(valid_response_body)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_return(valid_response_body)
end

it 'returns the content file' do
expect(subject.content(file_druid, filename)).to eq valid_response_body
expect(subject.content(druid: file_druid, filepath: filename)).to eq valid_response_body
end

it 'returns the content file for specified version' do
Expand All @@ -117,18 +117,18 @@
filepath: filename,
version: '6'
}
allow(subject).to receive(:get).with(file_api_path, my_file_api_params, 'file').and_return(valid_response_body)
expect(subject.content(file_druid, filename, '6')).to eq valid_response_body
allow(subject).to receive(:get).with(file_api_path, my_file_api_params).and_return(valid_response_body)
expect(subject.content(druid: file_druid, filepath: filename, version: '6')).to eq valid_response_body
end
end

context 'when API request fails' do
before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
end

it 'raises an error' do
expect { subject.content(file_druid, filename) }.to raise_error(Preservation::Client::UnexpectedResponseError, err_msg)
expect { subject.content(druid: file_druid, filepath: filename) }.to raise_error(Preservation::Client::UnexpectedResponseError, err_msg)
end
end
end
Expand All @@ -155,11 +155,11 @@
end

before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_return(valid_response_body)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_return(valid_response_body)
end

it 'returns the manifest file' do
expect(subject.manifest(file_druid, manifest_filename)).to eq valid_response_body
expect(subject.manifest(druid: file_druid, filepath: manifest_filename)).to eq valid_response_body
end

it 'returns the manifest file for specified version' do
Expand All @@ -169,18 +169,18 @@
filepath: manifest_filename,
version: '6'
}
allow(subject).to receive(:get).with(file_api_path, my_file_api_params, 'file').and_return(valid_response_body)
expect(subject.manifest(file_druid, manifest_filename, '6')).to eq valid_response_body
allow(subject).to receive(:get).with(file_api_path, my_file_api_params).and_return(valid_response_body)
expect(subject.manifest(druid: file_druid, filepath: manifest_filename, version: '6')).to eq valid_response_body
end
end

context 'when API request fails' do
before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
end

it 'raises an error' do
expect { subject.manifest(file_druid, manifest_filename) }.to raise_error(Preservation::Client::UnexpectedResponseError, err_msg)
expect { subject.manifest(druid: file_druid, filepath: manifest_filename) }.to raise_error(Preservation::Client::UnexpectedResponseError, err_msg)
end
end
end
Expand Down Expand Up @@ -209,11 +209,11 @@
end

before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_return(valid_response_body)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_return(valid_response_body)
end

it 'returns the metadata file' do
expect(subject.metadata(file_druid, metadata_filename)).to eq valid_response_body
expect(subject.metadata(druid: file_druid, filepath: metadata_filename)).to eq valid_response_body
end

it 'returns the metadata file for specified version' do
Expand All @@ -223,18 +223,18 @@
filepath: metadata_filename,
version: '6'
}
allow(subject).to receive(:get).with(file_api_path, my_file_api_params, 'file').and_return(valid_response_body)
expect(subject.metadata(file_druid, metadata_filename, '6')).to eq valid_response_body
allow(subject).to receive(:get).with(file_api_path, my_file_api_params).and_return(valid_response_body)
expect(subject.metadata(druid: file_druid, filepath: metadata_filename, version: '6')).to eq valid_response_body
end
end

context 'when API request fails' do
before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
end

it 'raises an error' do
expect { subject.metadata(file_druid, metadata_filename) }.to raise_error(Preservation::Client::UnexpectedResponseError, err_msg)
expect { subject.metadata(druid: file_druid, filepath: metadata_filename) }.to raise_error(Preservation::Client::UnexpectedResponseError, err_msg)
end
end
end
Expand All @@ -261,7 +261,7 @@
end

before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_return(valid_response_body)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_return(valid_response_body)
end

it 'returns the signature catalog file' do
Expand All @@ -271,7 +271,7 @@

context 'when API request fails' do
before do
allow(subject).to receive(:get).with(file_api_path, file_api_params, 'file').and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
allow(subject).to receive(:get).with(file_api_path, file_api_params).and_raise(Preservation::Client::UnexpectedResponseError, err_msg)
end

it 'raises an error' do
Expand Down
Loading

0 comments on commit 734ada7

Please sign in to comment.