Skip to content

Commit

Permalink
Fix treeify algorithm
Browse files Browse the repository at this point in the history
Identifiers that 1) are sufficiently short (< 8 characters) and 2) contain structure (a slash or a hash) were being treefied in a way that led to double slashes and misplaced hashes. Fixes #34
  • Loading branch information
mjgiarlo committed Oct 21, 2016
1 parent e6d4af4 commit 2e29325
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/active_fedora/noid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def config
end

def treeify(identifier)
(identifier.scan(/..?/).first(4) + [identifier]).join('/')
head = identifier.split('/').first
head.gsub!(/#.*/, '')
(head.scan(/..?/).first(4) + [identifier]).join('/')
end
end
end
Expand Down
50 changes: 46 additions & 4 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

context "with a hash code uri" do
let(:uri) { "http://localhost:8983/fedora/rest/test/hh/63/vz/22/hh63vz22q#g123" }
it { is_expected.to eq 'hh63vz22q#g123' }
it { is_expected.to eq 'hh63vz22q#g123' }
end

describe 'with a short custom template' do
context 'with a short custom template' do
let(:uri) { "http://localhost:8983/fedora/rest/test/ab/cd/abcd/members" }
let(:custom_template) { '.reeee' }
before { config.template = custom_template }
Expand All @@ -51,7 +51,7 @@
it { is_expected.to eq 'abcd/members' }
end

describe 'with an even shorter custom template' do
context 'with an even shorter custom template' do
let(:uri) { "http://localhost:8983/fedora/rest/test/ab/c/abc/members" }
let(:custom_template) { '.reee' }
before { config.template = custom_template }
Expand All @@ -60,14 +60,56 @@
it { is_expected.to eq 'abc/members' }
end

describe 'with a long custom template' do
context 'with a long custom template' do
let(:uri) { "http://localhost:8983/fedora/rest/test/ab/cd/ef/gh/abcdefghijklmnopqrstuvwxyz/members" }
let(:custom_template) { '.reeeeeeeeeeeeeeeeeeeeeeeeee' }
before { config.template = custom_template }
subject { translator.call(uri) }

it { is_expected.to eq 'abcdefghijklmnopqrstuvwxyz/members' }
end
end

describe '#translate_id_to_uri' do
let(:config) { described_class.new }
let(:translator) { config.translate_id_to_uri }
let(:id) { "hh63vz2/members" }
let(:ActiveFedora) { double(ActiveFedora) }
subject { translator.call(id) }
before do
allow(ActiveFedora).to receive_message_chain("fedora.host") { "http://localhost:8983" }
allow(ActiveFedora).to receive_message_chain("fedora.base_path") { "/fedora/rest/test" }
end

it { is_expected.to eq "http://localhost:8983/fedora/rest/test/hh/63/vz/2/hh63vz2/members" }

context "with a hash code id" do
let(:id) { 'hh63vz2#g123' }
it { is_expected.to eq "http://localhost:8983/fedora/rest/test/hh/63/vz/2/hh63vz2#g123" }
end

context 'with a short custom template' do
let(:id) { "abcd/members" }
let(:custom_template) { '.reeee' }
before { config.template = custom_template }
subject { translator.call(id) }
it { is_expected.to eq "http://localhost:8983/fedora/rest/test/ab/cd/abcd/members" }
end

context 'with an even shorter custom template' do
let(:id) { 'abc/members' }
let(:custom_template) { '.reee' }
before { config.template = custom_template }
subject { translator.call(id) }
it { is_expected.to eq "http://localhost:8983/fedora/rest/test/ab/c/abc/members" }
end

context 'with a long custom template' do
let(:id) { "abcdefghijklmnopqrstuvwxyz/members" }
let(:custom_template) { '.reeeeeeeeeeeeeeeeeeeeeeeeee' }
before { config.template = custom_template }
subject { translator.call(id) }
it { is_expected.to eq "http://localhost:8983/fedora/rest/test/ab/cd/ef/gh/abcdefghijklmnopqrstuvwxyz/members" }
end
end
end
4 changes: 4 additions & 0 deletions spec/unit/noid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
subject { ActiveFedora::Noid.treeify(id) }
let(:id) { 'abc123def45' }
it { is_expected.to eq 'ab/c1/23/de/abc123def45' }
context 'with a seven-digit identifier' do
let(:id) { 'abc123z' }
it { is_expected.to eq 'ab/c1/23/z/abc123z' }
end
end
end

0 comments on commit 2e29325

Please sign in to comment.