Skip to content
Browse files

replaced HashArgs with AssertHash

  • Loading branch information...
1 parent 44fa937 commit ff906a0ce5b5496df7c7fd8af2ce566ab61aa525 Anders Törnqvist committed Jun 10, 2011
Showing with 24 additions and 140 deletions.
  1. +20 −0 lib/resto/extra/assert_hash.rb
  2. +0 −56 lib/resto/extra/hash_args.rb
  3. +4 −13 lib/resto/request/header.rb
  4. +0 −71 spec/resto/extra/hash_args_spec.rb
View
20 lib/resto/extra/assert_hash.rb
@@ -0,0 +1,20 @@
+# encoding: utf-8
+
+class AssertHash
+
+ def self.keys(hash, *valid_keys)
+ hash ||= {}
+
+ hash.each { |key, value| hash[key.to_sym] = hash.delete(key) }
+
+ known_keys = [valid_keys].flatten
+ unknown_keys = hash.keys - known_keys
+ unless unknown_keys.empty?
+ unknown = "Invalid key(s): #{unknown_keys.join(", ")}"
+ known = "Valid key(s): #{known_keys.join(", ")}"
+ raise(ArgumentError, "#{unknown} \n #{known}")
+ end
+
+ hash
+ end
+end
View
56 lib/resto/extra/hash_args.rb
@@ -1,56 +0,0 @@
-# encoding: utf-8
-
-class Resto::Extra::HashArgs; end
-
-class << Resto::Extra::HashArgs
-
- def key(key)
- @keys ||= []
-
- unless key.is_a?(Symbol)
- raise ArgumentError, "The key '#{key}' must be a symbol"
- end
-
- if @keys.include?(key)
- raise ArgumentError, "The key '#{key}' has already been defined."
- end
-
- @keys << key
- end
-
-private
- def assert_key(key)
- unless @keys.include?(key.to_sym)
- raise ArgumentError, "The key '#{key}' is not valid.
- Valid keys are: #{@keys.join(' ,')}"
- end
- end
-end
-
-class Resto::Extra::HashArgs
-
- def initialize(hash)
- hash ||= {}
- raise ArgumentError, "'#{hash}' must be a Hash" unless hash.is_a?(Hash)
- keys = hash.keys
- keys_as_symbols = keys.map(&:to_sym)
- if (keys_as_symbols.uniq.size != keys_as_symbols.size)
- raise ArgumentError, "duplicated keys: #{keys.join(', ')}"
- end
-
- @hash = {}
- keys.each do |key|
- self.class.send(:assert_key, key)
- @hash[key.to_sym] = hash.fetch(key)
- end
- end
-
- def fetch(key, &block)
- self.class.send(:assert_key, key)
- @hash.fetch(key, &block)
- end
-
- def keys
- @hash.keys
- end
-end
View
17 lib/resto/request/header.rb
@@ -1,15 +1,6 @@
# encoding: utf-8
require 'resto/format'
-
-require 'resto/extra/hash_args'
-class BasicAuth < Resto::Extra::HashArgs
- key :username
- key :password
-end
-
-class FormatExtension < Resto::Extra::HashArgs
- key :extension
-end
+require 'resto/extra/assert_hash'
module Resto
module Request
@@ -20,8 +11,8 @@ def format(symbol, options=nil)
end
def formatter(formatter, options=nil)
- @add_extension =
- FormatExtension.new(options).fetch(:extension) { false }
+ options = AssertHash.keys(options, :extension)
+ @add_extension = options.fetch(:extension) { false }
@formatter = formatter
accept(formatter.accept)
content_type(formatter.content_type)
@@ -44,7 +35,7 @@ def content_type(content_type)
end
def basic_auth(options)
- options = BasicAuth.new(options)
+ options = AssertHash.keys(options, :username, :password)
username = options.fetch(:username)
password = options.fetch(:password)
View
71 spec/resto/extra/hash_args_spec.rb
@@ -1,71 +0,0 @@
-# encoding: utf-8
-
-require 'spec_helper'
-require 'resto/extra/hash_args'
-
-describe Resto::Extra::HashArgs do
-
- class_context(%Q{
- class BasicAuthication < Resto::Extra::HashArgs
- key :username
- key :password
- end}) do
-
- it "returns the value from the block when no value is found by key" do
- BasicAuthication.new(nil).fetch(:username) { 'anders' }.should == 'anders'
- end
-
- it "returns the value found by the key" do
- BasicAuthication.new({'username' => 'anders', :password => 'secret'}).
- fetch(:password) { 'other' }.should == 'secret'
- end
-
- it "the key is translated to its symbol" do
- BasicAuthication.new({'username' => 'anders', :password => 'secret'}).
- fetch(:username) { 'other' }.should == 'anders'
- end
- end
-
- class_context(%Q{
- class FormatExt < Resto::Extra::HashArgs
- key :extension
- end}) do
-
- it "returns the value from the block" do
- FormatExt.new({}).fetch(:extension) { 'block' }.should == 'block'
- end
-
- if RUBY_VERSION < '1.9'
-
- it "raises IndexError when no value and no block" do
- expect { FormatExt.new({}).fetch(:extension) }.
- to raise_error(IndexError, 'key not found')
- end
-
- else
-
- it "raises KeyError when no value and no block" do
- lambda { FormatExt.new({}).fetch(:extension) }.
- should raise_error(KeyError, 'key not found: :extension')
- end
-
- end
-
- it "raises" do
- expect { FormatExt.new({:username => "anders"}) }.
- to raise_error(ArgumentError, /The key 'username'/)
-
- expect { FormatExt.new("string") }.
- to raise_error(ArgumentError, "'string' must be a Hash")
-
- expect { FormatExt.new(:extension => 'value', 'extension' => 'value') }.
- to raise_error(ArgumentError, "duplicated keys: extension, extension")
-
- expect { FormatExt.new({:invalid_key => 'invalid' }) }.
- to raise_error(ArgumentError, /The key 'invalid_key' is not valid/)
-
- expect { FormatExt.new({:extension => 'value' }).fetch(:invalid_key) }.
- to raise_error(ArgumentError, /The key 'invalid_key' is not valid/)
- end
- end
-end

0 comments on commit ff906a0

Please sign in to comment.
Something went wrong with that request. Please try again.