Skip to content

Commit

Permalink
Simplify provider name shortening
Browse files Browse the repository at this point in the history
Instead of using a regular expression to parse
the name, we try to find the provider name inside
the already existing constant that knows providers
short names.
  • Loading branch information
NobodysNightmare committed Dec 5, 2024
1 parent 5ceee81 commit 832e1bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 6 additions & 8 deletions modules/storages/app/models/storages/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,12 @@ class Storage < ApplicationRecord
}.freeze, _prefix: :health

def self.shorten_provider_type(provider_type)
case /Storages::(?'provider_name'.*)Storage/.match(provider_type)
in provider_name:
provider_name.underscore
else
raise ArgumentError,
"Unknown provider_type! Given: #{provider_type}. " \
"Expected the following signature: Storages::{Name of the provider}Storage"
end
short, = PROVIDER_TYPE_SHORT_NAMES.find { |_, long| provider_type == long }
return short.to_s if short

raise ArgumentError,
"Unknown provider_type! Given: #{provider_type}. " \
"Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES."
end

def self.one_drive_without_ee_token?(provider_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@
end
end

context "when provider_type does not match the signature" do
context "when provider_type is unknown" do
it "raises an error", :aggregate_failures do
expect do
described_class.shorten_provider_type("Storages::Nextcloud")
end.to raise_error("Unknown provider_type! Given: Storages::Nextcloud. " \
"Expected the following signature: Storages::{Name of the provider}Storage")
"Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.")
expect do
described_class.shorten_provider_type("Storages:NextcloudStorage")
end.to raise_error("Unknown provider_type! Given: Storages:NextcloudStorage. " \
"Expected the following signature: Storages::{Name of the provider}Storage")
"Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.")
expect do
described_class.shorten_provider_type("Storages::NextcloudStorag")
end.to raise_error("Unknown provider_type! Given: Storages::NextcloudStorag. " \
"Expected the following signature: Storages::{Name of the provider}Storage")
"Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.")
expect do
described_class.shorten_provider_type("Storages::UnknownStorage")
end.to raise_error("Unknown provider_type! Given: Storages::UnknownStorage. " \
"Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.")
end
end
end
Expand Down

0 comments on commit 832e1bc

Please sign in to comment.