Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Process ERB syntax in configuration file #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/private_pub.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "digest/sha1"
require "net/http"
require "net/https"
require 'erb'

require "private_pub/faye_extension"
require "private_pub/engine" if defined? Rails
Expand All @@ -18,7 +19,7 @@ def reset_config

# Loads the configuration from a given YAML file and environment (such as production)
def load_config(filename, environment)
yaml = YAML.load_file(filename)[environment.to_s]
yaml = YAML.load(ERB.new(IO.read(filename)).result)[environment.to_s]
raise ArgumentError, "The #{environment} environment does not exist in #{filename}" if yaml.nil?
yaml.each { |k, v| config[k.to_sym] = v }
end
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/private_pub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ production:
server: http://example.com/faye
secret_token: PRODUCTION_SECRET_TOKEN
signature_expiration: 600
staging:
server: <%= "http://" + "example.com" %>:<%= 9000 + 292 %>/faye
secret_token: "<%= %w{TEST SECRET TOKEN}.join('_') %>"
6 changes: 6 additions & 0 deletions spec/private_pub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
PrivatePub.config[:signature_expiration].should eq(600)
end

it "loads a simple configuration file via load_config and processing ERB template" do
PrivatePub.load_config("spec/fixtures/private_pub.yml", "staging")
PrivatePub.config[:server].should eq("http://example.com:9292/faye")
PrivatePub.config[:secret_token].should eq("TEST_SECRET_TOKEN")
end

it "raises an exception if an invalid environment is passed to load_config" do
lambda {
PrivatePub.load_config("spec/fixtures/private_pub.yml", :test)
Expand Down