diff --git a/lib/private_pub.rb b/lib/private_pub.rb index a595cf8..8fc4f7f 100644 --- a/lib/private_pub.rb +++ b/lib/private_pub.rb @@ -4,6 +4,7 @@ require "private_pub/faye_extension" require "private_pub/engine" if defined? Rails +require "yaml" module PrivatePub class Error < StandardError; end @@ -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 diff --git a/private_pub.gemspec b/private_pub.gemspec index 5c0b872..a7ede7d 100644 --- a/private_pub.gemspec +++ b/private_pub.gemspec @@ -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" diff --git a/spec/fixtures/private_pub.yml b/spec/fixtures/private_pub.yml index 57eb4d3..04f468d 100644 --- a/spec/fixtures/private_pub.yml +++ b/spec/fixtures/private_pub.yml @@ -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 diff --git a/spec/private_pub_spec.rb b/spec/private_pub_spec.rb index 3929009..644a4db 100644 --- a/spec/private_pub_spec.rb +++ b/spec/private_pub_spec.rb @@ -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)