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

Allow environment variables in the configuration file #75

Open
wants to merge 6 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
4 changes: 3 additions & 1 deletion lib/private_pub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require "private_pub/faye_extension"
require "private_pub/engine" if defined? Rails
require "yaml"

module PrivatePub
class Error < StandardError; end
Expand All @@ -18,9 +19,10 @@ 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(File.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 }
config[:signature_expiration] = config[:signature_expiration].to_i if config[:signature_expiration] && !config[:signature_expiration].is_a?(Integer)
end

# Publish the given data to a specific channel. This ends up sending
Expand Down
4 changes: 2 additions & 2 deletions private_pub.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Gem::Specification.new do |s|
s.name = "private_pub"
s.version = "1.0.3"
s.version = "1.0.4"
s.author = "Ryan Bates"
s.email = "ryan@railscasts.com"
s.homepage = "http://github.com/ryanb/private_pub"
s.summary = "Private pub/sub messaging in Rails."
s.description = "Private pub/sub messaging in Rails through Faye."
s.description = "Private pub/sub messaging in Rails through Faye with command env params."

s.files = Dir["{app,lib,spec}/**/*", "[A-Z]*", "init.rb"] - ["Gemfile.lock"]
s.require_path = "lib"
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/private_pub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ development:
server: http://dev.local:9292/faye
secret_token: DEVELOPMENT_SECRET_TOKEN
signature_expiration: 600
staging:
server: <%= ENV['FAYE_SERVER'] %>
secret_token: <%= ENV['FAYE_TOKEN'] %>
signature_expiration: <%= ENV['FAYE_EXPIRATION'] %>
production:
server: http://example.com/faye
secret_token: PRODUCTION_SECRET_TOKEN
Expand Down
10 changes: 10 additions & 0 deletions spec/private_pub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
PrivatePub.config[:signature_expiration].should eq(600)
end

it "loads a configuration file with erb tags via load_config" do
ENV["FAYE_SERVER"] = "http://example.com/faye"
ENV["FAYE_TOKEN"] = "STAGING_SECRET_TOKEN"
ENV["FAYE_EXPIRATION"] = "600"
PrivatePub.load_config("spec/fixtures/private_pub.yml", "staging")
PrivatePub.config[:server].should eq("http://example.com/faye")
PrivatePub.config[:secret_token].should eq("STAGING_SECRET_TOKEN")
PrivatePub.config[:signature_expiration].should eq(600)
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