Permalink
Browse files

Add ability to configure using a JSON string

  • Loading branch information...
1 parent 720077f commit e103a3fafdd9ed25c69492504c69238c89dacd58 @jacquescrocker jacquescrocker committed Nov 5, 2010
Showing with 27 additions and 20 deletions.
  1. +10 −12 lib/recurly.rb
  2. +1 −1 lib/recurly/rails3/railtie.rb
  3. +16 −7 spec/unit/config_spec.rb
View
22 lib/recurly.rb
@@ -1,5 +1,6 @@
require 'active_resource'
require 'active_support/deprecation'
+require 'active_support/json'
require 'cgi'
require 'recurly/version'
@@ -53,6 +54,8 @@ def configure
true
end
+ # allows configuration from a yml file that contains the fields:
+ # username,password,site
def configure_from_yaml(path = nil)
configure do |c|
# parse configuration from yml
@@ -66,19 +69,14 @@ def configure_from_yaml(path = nil)
end
end
- def configure_from_heroku(config_string)
+ # allows configuration from a json string that contains the fields:
+ # username,password,site
+ def configure_from_json(json_string)
+ config_data = ActiveSupport::JSON.decode(json_string)
configure do |c|
-
- # pull out the site
- parts = config_string.split("@")
- c.site = parts.last
- config_string = parts[0..-2].join("@")
-
- # pull out the username and password
- parts = config_string.split(":")
- c.username = parts.first
- c.password = parts[1..-1].join(":")
-
+ c.username = config_data['username']
+ c.password = config_data['password']
+ c.site = config_data['site']
end
end
end
View
2 lib/recurly/rails3/railtie.rb
@@ -7,7 +7,7 @@ class Railtie < ::Rails::Railtie
config.after_initialize do
unless Recurly.configured?
if ENV["RECURLY_CONFIG"]
- ::Recurly.configure_from_heroku(ENV["RECURLY_CONFIG"])
+ ::Recurly.configure_from_json(ENV["RECURLY_CONFIG"])
else
# setup recurly authentication details for testing
::Recurly.configure_from_yaml
View
23 spec/unit/config_spec.rb
@@ -11,16 +11,25 @@
end
end
- context "loading from heroku config string" do
- it "should load configuration from a config string" do
- Recurly.configure_from_heroku("someuser:somepass@https://recurlytest3-test.recurly.com")
- Recurly.username.should == "someuser"
+ context "loading from json" do
+ it "should load configuration from a json config string" do
+ Recurly.configure_from_json({
+ :username => "someuser@heroku.com",
+ :password => "somepass",
+ :site => "https://recurlytest3-test.recurly.com"
+ }.to_json)
+ Recurly.username.should == "someuser@heroku.com"
Recurly.password.should == "somepass"
Recurly.site.should == "https://recurlytest3-test.recurly.com"
- # test with some crazy chars
- Recurly.configure_from_heroku("someuser:*$&!!::@&!)*)*_@https://recurlytest3-test.recurly.com")
- Recurly.username.should == "someuser"
+ # test with some crazy chars in the password
+ Recurly.configure_from_json({
+ :username => "api-someuser@heroku.com",
+ :password => "*$&!!::@&!)*)*_",
+ :site => "https://recurlytest3-test.recurly.com"
+ }.to_json)
+
+ Recurly.username.should == "api-someuser@heroku.com"
Recurly.password.should == "*$&!!::@&!)*)*_"
Recurly.site.should == "https://recurlytest3-test.recurly.com"
end

0 comments on commit e103a3f

Please sign in to comment.