diff --git a/lib/aviator/request.rb b/lib/aviator/request.rb index 66d92f6..3c351bd 100644 --- a/lib/aviator/request.rb +++ b/lib/aviator/request.rb @@ -2,8 +2,18 @@ module Aviator class Request + class PathNotDefinedError < StandardError + def initialize + super "path is not defined in #{ self.class }" + end + end + + def initialize(params={}) validate_params(params) + @params = params + + raise PathNotDefinedError.new unless respond_to?(:path) end @@ -12,12 +22,27 @@ module Aviator end + def body? + respond_to? :body + end + + def http_method :get end + + def querystring? + respond_to? :querystring + end + + private + def params + @params.dup + end + def validate_params(params) validators = methods.select{ |name| name =~ /^param_validator_/ } validators.each do |name| @@ -38,7 +63,7 @@ module Aviator def http_method(http_method_name) define_method :http_method, lambda { http_method_name } end - + def requires_param(param_name) last_num = instance_methods.map{|n| n.to_s.gsub(/^param_validator_/, '').to_i }.max diff --git a/test/aviator/request_test.rb b/test/aviator/request_test.rb index 21a5b56..63c4b2b 100644 --- a/test/aviator/request_test.rb +++ b/test/aviator/request_test.rb @@ -4,11 +4,32 @@ class Aviator::Test::Request < Aviator::Test::Base describe 'Aviator::Request' do + def valid_class + Class.new(Aviator::Request) do + def path; end + end + end + + describe '::new' do + + it 'raises an error if path is not defined' do + invalid_class = Class.new(Aviator::Request) + + initializer = lambda { invalid_class.new({}) } + initializer.must_raise Aviator::Request::PathNotDefinedError + + error = initializer.call rescue $! + + error.message.wont_be_nil + end + + end + describe '#allow_anonymous?' do it 'is false by default' do - obj = Aviator::Request.new + obj = valid_class.new obj.allow_anonymous?.must_equal false end @@ -27,12 +48,13 @@ class Aviator::Test::Request < Aviator::Test::Base error.message.wont_be_nil end + it 'sets a Request instance to allow anonymous access' do - klass = Class.new(Aviator::Request) do + klass = Class.new(valid_class) do allow_anonymous end - obj = klass.new + obj = klass.new obj.allow_anonymous?.must_equal true end @@ -40,11 +62,29 @@ class Aviator::Test::Request < Aviator::Test::Base end + describe '#body?' do + + it 'returns false if the body method is not defined' do + valid_class.new.body?.must_equal false + end + + + it 'returns true if the body method is defined' do + klass = Class.new(valid_class) do + def body; end + end + + klass.new.body?.must_equal true + end + + end + + describe '#http_method' do it 'is set to get by default' do - obj = Aviator::Request.new - + obj = valid_class.new + obj.http_method.must_equal :get end @@ -52,46 +92,64 @@ class Aviator::Test::Request < Aviator::Test::Base describe '::http_method' do - + it 'is a private class method' do private_method = lambda { Aviator::Request.http_method } private_method.must_raise NoMethodError - + error = private_method.call rescue $! - + error.message.wont_be_nil error.message.must_include "private method" end - - + + it 'sets the http method of its instances' do - klass = Class.new(Aviator::Request) do + klass = Class.new(valid_class) do http_method :post end obj = klass.new - + obj.http_method.must_equal :post end - + end - - + + + describe '#querystring?' do + + it 'returns false if the querystring method is not defined' do + valid_class.new.querystring?.must_equal false + end + + + it 'returns true if the querystring method is defined' do + klass = Class.new(valid_class) do + def querystring; end + end + + klass.new.querystring?.must_equal true + end + + end + + describe '::requires_param' do - + it 'is a private class method' do private_method = lambda { Aviator::Request.requires_param } private_method.must_raise NoMethodError - + error = private_method.call rescue $! - + error.message.wont_be_nil error.message.must_include "private method" end it 'causes a Request instance to raise an error when initialized without the param' do - klass = Class.new(Aviator::Request) do + klass = Class.new(valid_class) do requires_param :name end @@ -102,18 +160,18 @@ class Aviator::Test::Request < Aviator::Test::Base error.message.wont_be_nil end - - + + it 'does not raise any error when the required param is provided on initialization' do - klass = Class.new(Aviator::Request) do + klass = Class.new(valid_class) do requires_param :name end obj = klass.new({ name: 'somename' }) end - + end - + end end