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

Fix treeify algorithm #41

Merged
merged 1 commit into from
Oct 21, 2016
Merged
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
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