wsdsl 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -66,7 +66,10 @@ module ParamsVerification
66
66
  param.list_required.each do |rule|
67
67
  updated_params = validate_required_rule(rule, updated_params, param.space_name.to_s)
68
68
  end
69
- # TODO add verification for namespaced optional rules
69
+ param.list_optional.each do |rule|
70
+ updated_params = run_optional_rule(rule, updated_params, param.space_name.to_s)
71
+ end
72
+
70
73
  end
71
74
 
72
75
  # verify nested params, only 1 level deep tho
@@ -98,22 +101,16 @@ module ParamsVerification
98
101
  def self.validate_required_rule(rule, params, namespace=nil)
99
102
  param_name = rule.name.to_s
100
103
 
101
- # Namespace check
102
- if namespace == '' || namespace.nil?
103
- param_value = params[param_name]
104
- else
105
- # puts "namespace: #{namespace} - params #{params[namespace].inspect}"
106
- namespaced_params = params[namespace]
107
- param_value = namespaced_params ? namespaced_params[param_name] : nil
108
- end
104
+ param_value, namespaced_params = extract_param_values(params, param_name, namespace)
109
105
  # puts "verify #{param_name} params, current value: #{param_value}"
110
106
 
111
- # Checks default
112
- if param_value.nil? && rule.options && rule.options[:default]
113
- param_ref = namespace.nil? ? params[param_name] : params[namespace][param_name]
114
- param_ref = rule.options[:default]
107
+ #This is disabled since required params shouldn't have a default, otherwise, why are they required?
108
+ #if param_value.nil? && rule.options && rule.options[:default]
109
+ #param_value = rule.options[:default]
110
+ #end
111
+
115
112
  # Checks presence
116
- elsif !(namespaced_params || params).keys.include?(param_name)
113
+ if !(namespaced_params || params).keys.include?(param_name)
117
114
  raise MissingParam, "'#{rule.name}' is missing - passed params: #{params.inspect}."
118
115
  # checks null
119
116
  elsif param_value.nil? && !rule.options[:null]
@@ -139,26 +136,65 @@ module ParamsVerification
139
136
  # puts "casting #{param_value} into type: #{rule.options[:type]}"
140
137
  params[param_name] = type_cast_value(rule.options[:type], param_value)
141
138
  end
142
-
143
139
  params
144
140
  end
141
+
142
+
143
+ # Extract the param valie and the namespaced params
144
+ # based on a passed namespace and params
145
+ #
146
+ # @param [Hash] params The passed params to extract info from.
147
+ # @param [String] param_name The param name to find the value.
148
+ # @param [NilClass, String] namespace the params' namespace.
149
+ # @return [Arrays<Object, String>]
150
+ #
151
+ # @api private
152
+ def self.extract_param_values(params, param_name, namespace=nil)
153
+ # Namespace check
154
+ if namespace == '' || namespace.nil?
155
+ [params[param_name], nil]
156
+ else
157
+ # puts "namespace: #{namespace} - params #{params[namespace].inspect}"
158
+ namespaced_params = params[namespace]
159
+ if namespaced_params
160
+ [namespaced_params[param_name], namespaced_params]
161
+ else
162
+ [nil, namespaced_params]
163
+ end
164
+ end
165
+ end
145
166
 
146
- # @todo add support for namespaces
147
167
  # @param [#WSDSL::Params::Rule] rule The optional rule
148
168
  # @param [Hash] params The request params
149
169
  # @param [String] namespace An optional namespace
150
170
  # @return [Hash] The potentially modified params
171
+ #
151
172
  # @api private
152
173
  def self.run_optional_rule(rule, params, namespace=nil)
153
174
  param_name = rule.name.to_s
154
- param_value = params[param_name]
175
+
176
+ param_value, namespaced_params = extract_param_values(params, param_name, namespace)
177
+
155
178
  if param_value.nil? && rule.options[:default]
156
- params[param_name] = rule.options[:default]
179
+ if namespace
180
+ params[namespace][param_name] = param_value = rule.options[:default]
181
+ else
182
+ params[param_name] = param_value = rule.options[:default]
183
+ end
157
184
  end
158
185
 
159
186
  # cast the type if a type is defined and if a range of options isn't defined since the casting should have been done already
160
187
  if rule.options[:type] && !param_value.nil?
161
- params[param_name] = type_cast_value(rule.options[:type], param_value)
188
+ if namespace
189
+ params[namespace][param_name] = param_value = type_cast_value(rule.options[:type], param_value)
190
+ else
191
+ params[param_name] = param_value = type_cast_value(rule.options[:type], param_value)
192
+ end
193
+ end
194
+
195
+ choices = rule.options[:options] || rule.options[:in]
196
+ if choices && param_value && !choices.include?(param_value)
197
+ raise InvalidParamValue, "Value for parameter '#{param_name}' (#{param_value}) is not in the allowed set of values."
162
198
  end
163
199
 
164
200
  params
@@ -2,10 +2,6 @@ require_relative "spec_helper"
2
2
 
3
3
  describe ParamsVerification do
4
4
 
5
- def app
6
- Sinatra::Application
7
- end
8
-
9
5
  before :all do
10
6
  @service = WSList.all.find{|s| s.url == 'services/test.xml'}
11
7
  @service.should_not be_nil
@@ -13,20 +9,30 @@ describe ParamsVerification do
13
9
  end
14
10
 
15
11
  it "should validate valid params" do
16
- lambda{ ParamsVerification.validate!(@valid_params, @service.defined_params) }.should_not raise_exception
12
+ params = @valid_params.dup
13
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should_not raise_exception
17
14
  end
18
15
 
19
16
  it "should return the params" do
20
- returned_params = ParamsVerification.validate!(@valid_params, @service.defined_params)
17
+ params = @valid_params.dup
18
+ returned_params = ParamsVerification.validate!(params, @service.defined_params)
21
19
  returned_params.should be_an_instance_of(Hash)
22
20
  returned_params.keys.size.should >= 3
23
21
  end
24
22
 
25
- it "should set the default values" do
26
- @valid_params['timestamp'].should be_nil
27
- returned_params = ParamsVerification.validate!(@valid_params, @service.defined_params)
23
+ it "should set the default value for an optional param" do
24
+ params = @valid_params.dup
25
+ params['timestamp'].should be_nil
26
+ returned_params = ParamsVerification.validate!(params, @service.defined_params)
28
27
  returned_params['timestamp'].should_not be_nil
29
28
  end
29
+
30
+ it "should set the default value for a namespace optional param" do
31
+ params = {'framework' => 'RSpec', 'version' => '1.02', 'user' => {'id' => '123'}}
32
+ params['user']['mailing_list'].should be_nil
33
+ returned_params = ParamsVerification.validate!(params, @service.defined_params)
34
+ returned_params['user']['mailing_list'].should be_true
35
+ end
30
36
 
31
37
  it "should raise an exception when a required param is missing" do
32
38
  params = @valid_params.dup
@@ -49,13 +55,25 @@ describe ParamsVerification do
49
55
  it "should raise an exception when a param isn't in the param option list" do
50
56
  params = @valid_params.dup
51
57
  params['alpha'] = 'z'
58
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamValue)
59
+ end
60
+
61
+ it "should raise an exception when a nested optional param isn't in the param option list" do
62
+ params = @valid_params.dup
63
+ params['user']['sex'] = 'large'
52
64
  lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamType)
65
+ # other service
66
+ params = {'preference' => {'region_code' => 'us', 'language_code' => 'de'}}
67
+ service = WSList.all.find{|s| s.url == 'preferences.xml'}
68
+ service.should_not be_nil
69
+ lambda{ ParamsVerification.validate!(params, service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamValue)
53
70
  end
54
71
 
55
72
  it "should validate that no params are passed when accept_no_params! is set on a service" do
56
73
  service = WSList.all.find{|s| s.url == "services/test_no_params.xml"}
57
74
  service.should_not be_nil
58
- lambda{ ParamsVerification.validate!(@valid_params, service.defined_params) }.should raise_exception
75
+ params = @valid_params.dup
76
+ lambda{ ParamsVerification.validate!(params, service.defined_params) }.should raise_exception
59
77
  end
60
78
 
61
79
  end
@@ -0,0 +1,10 @@
1
+ describe_service "preferences.xml" do |service|
2
+
3
+ service.params do |p|
4
+ p.namespace :preference do |pr|
5
+ pr.string :language_code, :options => ['en', 'fr']
6
+ pr.string :region_code, :options => ['europe']
7
+ end
8
+ end
9
+
10
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,7 @@ require 'sinatra'
4
4
 
5
5
  require_relative "../lib/wsdsl"
6
6
  require_relative 'test_services'
7
+ require_relative 'preferences_service'
7
8
  require_relative "../lib/framework_ext/sinatra_controller"
8
9
 
9
10
  ENV["RACK_ENV"] = 'test'
@@ -11,4 +12,3 @@ ENV["RACK_ENV"] = 'test'
11
12
  RSpec.configure do |conf|
12
13
  conf.include Rack::Test::Methods
13
14
  end
14
-
@@ -11,6 +11,7 @@ describe_service "services/test.xml" do |service|
11
11
  p.string :alpha, :in => ['a', 'b', 'c']
12
12
  p.string :version, :null => false
13
13
  p.integer :num, :minvalue => 42
14
+
14
15
  end
15
16
 
16
17
  # service.param :delta, :optional => true, :type => 'float'
@@ -19,6 +20,8 @@ describe_service "services/test.xml" do |service|
19
20
 
20
21
  service.params.namespace :user do |user|
21
22
  user.integer :id, :required => :true
23
+ user.string :sex, :in => %Q{female, male}
24
+ user.boolean :mailing_list, :default => true
22
25
  end
23
26
 
24
27
  # the response contains a list of player creation ratings each object in the list
data/wsdsl.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wsdsl}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matt Aimonetti"]
12
- s.date = %q{2011-03-14}
12
+ s.date = %q{2011-03-15}
13
13
  s.description = %q{A Ruby DSL describing Web Services without implementation details.}
14
14
  s.email = %q{mattaimonetti@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
33
33
  "spec/hello_world_controller.rb",
34
34
  "spec/hello_world_service.rb",
35
35
  "spec/params_verification_spec.rb",
36
+ "spec/preferences_service.rb",
36
37
  "spec/spec_helper.rb",
37
38
  "spec/test_services.rb",
38
39
  "spec/wsdsl_sinatra_ext_spec.rb",
@@ -48,6 +49,7 @@ Gem::Specification.new do |s|
48
49
  "spec/hello_world_controller.rb",
49
50
  "spec/hello_world_service.rb",
50
51
  "spec/params_verification_spec.rb",
52
+ "spec/preferences_service.rb",
51
53
  "spec/spec_helper.rb",
52
54
  "spec/test_services.rb",
53
55
  "spec/wsdsl_sinatra_ext_spec.rb",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Aimonetti
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-14 00:00:00 -07:00
17
+ date: 2011-03-15 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -44,6 +44,7 @@ files:
44
44
  - spec/hello_world_controller.rb
45
45
  - spec/hello_world_service.rb
46
46
  - spec/params_verification_spec.rb
47
+ - spec/preferences_service.rb
47
48
  - spec/spec_helper.rb
48
49
  - spec/test_services.rb
49
50
  - spec/wsdsl_sinatra_ext_spec.rb
@@ -85,6 +86,7 @@ test_files:
85
86
  - spec/hello_world_controller.rb
86
87
  - spec/hello_world_service.rb
87
88
  - spec/params_verification_spec.rb
89
+ - spec/preferences_service.rb
88
90
  - spec/spec_helper.rb
89
91
  - spec/test_services.rb
90
92
  - spec/wsdsl_sinatra_ext_spec.rb