Skip to content

Commit

Permalink
Add support for aliases in Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jlestavel authored Mar 31, 2022
1 parent a06dfc6 commit ddde239
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/cloudinary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,18 @@ def self.make_new_config(config_module)
#
# @return [OpenStruct]
def self.import_settings_from_file
OpenStruct.new((YAML.load(ERB.new(IO.read(config_dir.join("cloudinary.yml"))).result)[config_env] rescue {}))
yaml_env_config = begin
yaml_source = ERB.new(IO.read(config_dir.join("cloudinary.yml"))).result
yaml_config = if YAML.respond_to?(:safe_load)
YAML.safe_load(yaml_source, aliases: true)
else
YAML.load(yaml_source)
end
yaml_config[config_env]
rescue StandardError
{}
end
OpenStruct.new(yaml_env_config)
end

private_class_method :import_settings_from_file
Expand Down
7 changes: 7 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,12 @@
expect(Cloudinary::config.api_secret).to eq(API_SECRET)
expect(Cloudinary::config.oauth_token).to eq(OAUTH_TOKEN)
end

it "supports config from Yaml, with aliases and ERB interpolation" do
ENV["CLOUDINARY_CONFIG_DIR"] = File.join(File.dirname(__FILE__), 'data')
ENV["CLOUDINARY_API_KEY"] = 'api_key'
expect(Cloudinary::config.cloud_name).to eq('test_cloud')
expect(Cloudinary::config.api_key).to eq('api_key')
end
end
end
8 changes: 8 additions & 0 deletions spec/data/cloudinary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default: &default
api_key: '<%= ENV['CLOUDINARY_API_KEY'] %>'
test:
<<: *default
cloud_name: test_cloud
production:
<<: *default
cloud_name: production_cloud

0 comments on commit ddde239

Please sign in to comment.