Allow configure from yaml to work with yml.erb files #9

Merged
merged 5 commits into from Mar 15, 2012
View
@@ -4,4 +4,6 @@ require "rspec/core/rake_task"
Bundler::GemHelper.install_tasks
-Rspec::Core::RakeTask.new(:spec)
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
View
@@ -1,10 +1,16 @@
if defined? Rails
+ require 'erb'
+
module Poundpay
def self.configure_from_yaml(path)
- pathname = Pathname.new Rails.root.join(path)
- raise ArgumentError.new "File does not exist: #{pathname.to_s}" unless pathname.exist?
- config = YAML::load_file(pathname)[Rails.env]
+ pathname = Rails.root.join(path)
+ raise ArgumentError, "File does not exist: #{pathname}" unless pathname.exist?
+
+ config = pathname.read
+ config = ERB.new(config).result if pathname.extname == '.erb'
+ config = YAML.load(config)[Rails.env]
+
Poundpay.configure_from_hash(config)
end
end
-end
+end
@@ -7,7 +7,7 @@ class Resource < ActiveResource::Base
self.format = Formats::UrlencodedJsonFormat
class << self
- attr_accessor :primary_key
+ attr_writer :primary_key
def primary_key
@primary_key ||= 'sid'
@@ -45,9 +45,9 @@ def instantiate_collection(collection, prefix_options = {})
end
protected
- def remove_extension(path)
- path.sub /(\.#{format.extension})/, ""
- end
+ def remove_extension(path)
+ path.sub /(\.#{format.extension})/, ""
+ end
end
# Poundpay accepts urlencoded form parameters
@@ -61,14 +61,14 @@ def collection_name
end
protected
- def self.urlencode(params)
- params.to_a.collect! { |k, v|
- if v.kind_of?(Array)
- v.collect! { |x| "#{k}=#{CGI.escape(x.to_s)}"}.join("&")
- else
- "#{k}=#{CGI.escape(v.to_s)}"
- end
- }.join("&")
+ def self.urlencode(params)
+ params.to_a.collect! { |k, v|
+ if v.kind_of?(Array)
+ v.collect! { |x| "#{k}=#{CGI.escape(x.to_s)}" }.join("&")
+ else
+ "#{k}=#{CGI.escape(v.to_s)}"
end
+ }.join("&")
+ end
end
-end
+end
View
@@ -14,9 +14,9 @@ Gem::Specification.new do |s|
s.rubyforge_project = "poundpay"
- s.add_dependency("activeresource", ">= 3.1.0")
+ s.add_dependency("activeresource", "~> 3.1")
- s.add_development_dependency("rspec", ">= 2.0")
+ s.add_development_dependency("rspec", "~> 2.0")
s.add_development_dependency("wirble")
s.files = `git ls-files`.split("\n")
@@ -16,5 +16,5 @@ future:
api_version: gold
invalid:
- developer_sid:
- auth_token:
+ developer_sid:
+ auth_token:
@@ -0,0 +1,5 @@
+development:
+ developer_sid: DV0383d447360511e0bbac00264a09ff3c
+ auth_token: <%= 'erb_development_auth_token' %>
+ www_url: https://www-sandbox.poundpay.com
+ api_url: https://api-sandbox.poundpay.com
@@ -2,48 +2,45 @@
describe Poundpay do
module Rails
+ def self.root
+ Pathname(File.expand_path("../../fixtures", __FILE__))
+ end
+
+ def self.env
+ "development"
+ end
+ end
+
+ load File.expand_path("../../../lib/poundpay/rails.rb", __FILE__)
+
+ before do
+ Poundpay.should_not be_configured
end
- after (:each) do
+ after do
Poundpay.clear_config!
end
it "should automatically load config if exists" do
- load File.expand_path("../../../lib/poundpay/rails.rb", __FILE__)
+ Poundpay.configure_from_yaml "poundpay.yml"
- module Rails
- def self.root
- Pathname(File.expand_path("../../fixtures", __FILE__))
- end
-
- def self.env
- "development"
- end
- end
-
- Poundpay.configured?.should be_false
- Poundpay.configure_from_yaml "config/poundpay.yml"
- Poundpay.configured?.should be_true
+ Poundpay.should be_configured
Poundpay::Resource.password.should == "development_auth_token"
end
- it "should not do anything if config does not exist" do
- load File.expand_path("../../../lib/poundpay/rails.rb", __FILE__)
+ it "configures using ERB files to allow ENV variables" do
+ Poundpay.configure_from_yaml "poundpay.yml.erb"
- module Rails
- def self.root
- Pathname(File.expand_path("../../fixtures", __FILE__))
- end
+ Poundpay.should be_configured
+ Poundpay::Resource.password.should == "erb_development_auth_token"
+ end
- def self.env
- "development"
- end
- end
+ it "should raise argument error and configure nothing if config does not exist" do
+ expect {
+ Poundpay.configure_from_yaml "wrong_path"
+ }.to raise_error(ArgumentError, /wrong_path/)
- Poundpay.configured?.should be_false
- expect { Poundpay.configure_from_yaml "wrong_path" }.to raise_error(ArgumentError, /wrong_path/)
- Poundpay.configured?.should be_false
- Poundpay::Resource.password.should == nil
+ Poundpay.should_not be_configured
+ Poundpay::Resource.password.should_not be
end
-
-end
+end