Skip to content

Commit

Permalink
use confiture validate! method
Browse files Browse the repository at this point in the history
  • Loading branch information
phoet committed Jul 6, 2012
1 parent 2991618 commit 12acff4
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GEM
specs:
addressable (2.2.8)
coderay (1.0.7)
confiture (0.1.0)
confiture (0.1.2)
crack (0.3.1)
diff-lcs (1.1.3)
fuubar (0.0.6)
Expand Down
2 changes: 1 addition & 1 deletion lib/asin/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def cart(operation, params={})
end

def call(params)
Configuration.validate_credentials!
Configuration.validate!

log(:debug, "calling with params=#{params}")
signed = create_signed_query_string(params)
Expand Down
17 changes: 1 addition & 16 deletions lib/asin/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module ASIN
class Configuration
include Confiture::Configuration
confiture_allowed_keys(:secret, :key, :host, :version, :associate_tag, :logger, :item_type, :cart_type, :node_type)
confiture_mandatory_keys(:secret, :key)
confiture_defaults({
:secret => '',
:key => '',
Expand All @@ -52,21 +53,5 @@ class Configuration
:cart_type => SimpleCart,
:node_type => SimpleNode,
})

class << self
# Checks if given credentials are valid and raises an error if not.
#
def validate_credentials!
raise "you have to configure ASIN: 'configure :secret => 'your-secret', :key => 'your-key'" if blank?(:secret) || blank?(:key)
[:host, :item_type, :cart_type, :node_type, :version, :associate_tag].each { |item| raise "nil is not a valid value for #{item}" unless self.send item }
end

# Check if a key is set
#
def blank?(key)
val = self.send :key
val.nil? || val.empty?
end
end
end
end
7 changes: 1 addition & 6 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ module ASIN
describe ASIN do
context "configuration" do
it "should fail without secret and key" do
lambda { @helper.lookup 'bla' }.should raise_error(RuntimeError, "you have to configure ASIN: 'configure :secret => 'your-secret', :key => 'your-key'")
end

it "should fail with configuration key set to nil" do
@helper.configure :secret => @secret, :key => @key, :associate_tag => nil
lambda { @helper.lookup 'bla' }.should raise_error(RuntimeError, "nil is not a valid value for associate_tag")
expect { @helper.lookup 'bla' }.should raise_error(ArgumentError, "you are missing mandatory configuration options. please set [:secret, :key]")
end

it "should fail with wrong configuration key" do
Expand Down

0 comments on commit 12acff4

Please sign in to comment.