Skip to content

Commit

Permalink
creating instructions to use a Rails initializers with yaml configs
Browse files Browse the repository at this point in the history
  • Loading branch information
matin committed Feb 20, 2011
1 parent 8fa8068 commit f522a2f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
10 changes: 7 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Poundpay is a payments platform for marketplaces

1. Add the following to your Gemfile

gem 'poundpay', '~> 0.2.0'
gem 'poundpay', '~> 0.2.1'

2. At the command prompt, install the gem with bundler

bundle install

3. Add your configuration to config/poundpay.yml in your Rails project
3. Create the file config/poundpay.yml and add your configurations

development:
developer_sid: DV0383d447360511e0bbac00264a09ff3c
Expand All @@ -25,7 +25,11 @@ Poundpay is a payments platform for marketplaces
developer_sid: DV8dd93f0f3c6411e0863f00264a09ff3c
auth_token: d8c4ea1bafd3fcac8c1062a72c22bcdb09321deb1041df257165cd6449def0de

4. Protect your callback controller
4. Create the file initializers/poundpay.rb and add the following

Poundpay.configure_from_yaml "config/poundpay.yml"

5. Protect your callback controller

before_filter :verify_poundpay_callback

Expand Down
10 changes: 3 additions & 7 deletions lib/poundpay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ class << self
attr_writer :api_version

def configure(developer_sid, auth_token)
if not developer_sid
raise ArgumentError.new "developer_sid is required"
end

if not auth_token
raise ArgumentError.new "auth_token is required"
end
warn "warning: Poundpay is already configured" if configured?
raise ArgumentError.new "developer_sid is required" unless developer_sid
raise ArgumentError.new "auth_token is required" unless auth_token

unless developer_sid.start_with? "DV"
raise ArgumentError.new "developer_sid should start with 'DV'. Make sure " \
Expand Down
10 changes: 7 additions & 3 deletions lib/poundpay/rails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
if defined? Rails and Rails.root.join("config", "poundpay.yml").exist?
if defined? Rails
module Poundpay
@rails_config = YAML::load_file(Rails.root.join("config", "poundpay.yml"))[Rails.env]
Poundpay.configure_from_hash(@rails_config)
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]
Poundpay.configure_from_hash(config)
end
end
end
2 changes: 1 addition & 1 deletion lib/poundpay/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Poundpay
VERSION = "0.2.0"
VERSION = "0.2.1"
end
43 changes: 23 additions & 20 deletions spec/poundpay/rails_spec.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
require 'poundpay'

describe Poundpay do
module Rails
end

after (:each) do
Poundpay.clear_config!
end

it "should automatically load config if exists" do
load File.expand_path("../../../lib/poundpay/rails.rb", __FILE__)

module Rails
class << self
def root
Pathname(File.expand_path("../../fixtures", __FILE__))
end

def env
"development"
end
def self.root
Pathname(File.expand_path("../../fixtures", __FILE__))
end

def self.env
"development"
end
end

Poundpay.configured?.should be_false
load File.expand_path("../../../lib/poundpay/rails.rb", __FILE__)
Poundpay.configure_from_yaml "config/poundpay.yml"
Poundpay.configured?.should be_true
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__)

module Rails
class << self
def root
Pathname(File.expand_path("wrong_directory", __FILE__))
end

def env
"development"
end
def self.root
Pathname(File.expand_path("../../fixtures", __FILE__))
end

def self.env
"development"
end
end

Poundpay.configured?.should be_false
load File.expand_path("../../../lib/poundpay/rails.rb", __FILE__)
expect { Poundpay.configure_from_yaml "wrong_path" }.to raise_error(ArgumentError, /wrong_path/)
Poundpay.configured?.should be_false
Poundpay::Resource.password.should == nil
end
Expand Down

0 comments on commit f522a2f

Please sign in to comment.