Skip to content

Commit

Permalink
Merge pull request #41 from projecthydra-labs/treeify_with_short_stru…
Browse files Browse the repository at this point in the history
…ctured_identifiers

Fix treeify algorithm
  • Loading branch information
Thomas Johnson authored Oct 21, 2016
2 parents 76cba3f + 21942bb commit 1d69590
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 @@ -18,7 +18,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 @@ -38,10 +38,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 @@ -50,7 +50,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 @@ -59,14 +59,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 @@ -13,5 +13,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 1d69590

Please sign in to comment.