playerconnect-wsdsl 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ Jeweler::Tasks.new do |gem|
11
11
  gem.description = %Q{A Ruby DSL describing Web Services without implementation details.}
12
12
  gem.email = "sdod"
13
13
  gem.authors = ["Team SDOD"]
14
- gem.version = "0.3.3"
14
+ gem.version = "0.3.4"
15
15
  # Include your dependencies below. Runtime dependencies are required when using your gem,
16
16
  # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
17
17
  # gem.add_runtime_dependency 'jabber4r', '> 0.1'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
@@ -5,14 +5,14 @@
5
5
  #
6
6
  # @api public
7
7
  module ParamsVerification
8
-
8
+
9
9
  class ParamError < StandardError; end #:nodoc
10
10
  class NoParamsDefined < ParamError; end #:nodoc
11
11
  class MissingParam < ParamError; end #:nodoc
12
12
  class UnexpectedParam < ParamError; end #:nodoc
13
13
  class InvalidParamType < ParamError; end #:nodoc
14
14
  class InvalidParamValue < ParamError; end #:nodoc
15
-
15
+
16
16
  # An array of validation regular expressions.
17
17
  # The array gets cached but can be accessed via the symbol key.
18
18
  #
@@ -27,12 +27,12 @@ module ParamsVerification
27
27
  #:array => /,/
28
28
  }
29
29
  end
30
-
30
+
31
31
  # Validation against each required WSDSL::Params::Rule
32
32
  # and returns the potentially modified params (with default values)
33
- #
33
+ #
34
34
  # @param [Hash] params The params to verify (incoming request params)
35
- # @param [WSDSL::Params] service_params A Playco service param compatible object listing required and optional params
35
+ # @param [WSDSL::Params] service_params A Playco service param compatible object listing required and optional params
36
36
  # @param [Boolean] ignore_unexpected Flag letting the validation know if unexpected params should be ignored
37
37
  #
38
38
  # @return [Hash]
@@ -40,10 +40,10 @@ module ParamsVerification
40
40
  #
41
41
  # @example Validate request params against a service's defined param rules
42
42
  # ParamsVerification.validate!(request.params, @service.defined_params)
43
- #
43
+ #
44
44
  # @api public
45
45
  def self.validate!(params, service_params, ignore_unexpected=false)
46
-
46
+
47
47
  # Verify that no garbage params are passed, if they are, an exception is raised.
48
48
  # only the first level is checked at this point
49
49
  unless ignore_unexpected
@@ -53,17 +53,17 @@ module ParamsVerification
53
53
  # Create a duplicate of the params hash that uses symbols as keys,
54
54
  # while preserving the original hash
55
55
  updated_params = symbolify_keys(params)
56
-
56
+
57
57
  # Required param verification
58
58
  service_params.list_required.each do |rule|
59
59
  updated_params = validate_required_rule(rule, updated_params)
60
60
  end
61
-
61
+
62
62
  # Set optional defaults if any optional
63
63
  service_params.list_optional.each do |rule|
64
64
  updated_params = run_optional_rule(rule, updated_params)
65
65
  end
66
-
66
+
67
67
  # check the namespaced params
68
68
  service_params.namespaced_params.each do |param|
69
69
  param.list_required.each do |rule|
@@ -74,10 +74,10 @@ module ParamsVerification
74
74
  end
75
75
 
76
76
  end
77
-
77
+
78
78
  # verify nested params, only 1 level deep tho
79
79
  params.each_pair do |key, value|
80
- # We are now assuming a file param is a hash due to Rack::Mulitpart.parse_multipart
80
+ # We are now assuming a file param is a hash due to Rack::Mulitpart.parse_multipart
81
81
  # turns this data into a hash, but param verification/DSL dont expect this or define this behavior and it shouldn't.
82
82
  # so special case it if its a file type and the value is a hash.
83
83
  if value.is_a?(Hash) && type_for_param(service_params, key) != :file
@@ -128,10 +128,10 @@ module ParamsVerification
128
128
  def self.validate_required_rule(rule, params, namespace=nil)
129
129
  param_name = rule.name.to_sym
130
130
  namespace = namespace.to_sym if namespace
131
-
131
+
132
132
  param_value, namespaced_params = extract_param_values(params, param_name, namespace)
133
133
  # puts "verify #{param_name} params, current value: #{param_value}"
134
-
134
+
135
135
  #This is disabled since required params shouldn't have a default, otherwise, why are they required?
136
136
  #if param_value.nil? && rule.options && rule.options[:default]
137
137
  #param_value = rule.options[:default]
@@ -141,36 +141,49 @@ module ParamsVerification
141
141
  if !(namespaced_params || params).keys.include?(param_name)
142
142
  raise MissingParam, "'#{rule.name}' is missing - passed params: #{params.inspect}."
143
143
  end
144
-
144
+
145
145
  # checks null
146
146
  if param_value.nil? && !rule.options[:null]
147
147
  raise InvalidParamValue, "Value for parameter '#{param_name}' is missing - passed params: #{params.inspect}."
148
148
  end
149
-
149
+
150
150
  # checks type
151
151
  if rule.options[:type]
152
152
  verify_cast(param_name, param_value, rule.options[:type])
153
153
  end
154
-
154
+
155
155
  if rule.options[:options] || rule.options[:in]
156
156
  choices = rule.options[:options] || rule.options[:in]
157
+
157
158
  if rule.options[:type]
158
159
  # Force the cast so we can compare properly
159
- param_value = params[param_name] = type_cast_value(rule.options[:type], param_value)
160
+ param_value = type_cast_value(rule.options[:type], param_value)
161
+ if namespace
162
+ params[namespace][param_name] = param_value
163
+ else
164
+ params[param_name] = param_value
165
+ end
160
166
  end
161
- raise InvalidParamValue, "Value for parameter '#{param_name}' (#{param_value}) is not in the allowed set of values." unless choices.include?(param_value)
167
+
168
+ validate_inclusion_of(param_name, param_value, choices)
169
+
162
170
  end
163
-
171
+
164
172
  if rule.options[:minvalue]
165
173
  min = rule.options[:minvalue]
166
174
  raise InvalidParamValue, "Value for parameter '#{param_name}' is lower than the min accepted value (#{min})." if param_value.to_i < min
167
175
  end
168
176
  # Returns the updated params
169
-
177
+
170
178
  # 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
171
179
  if rule.options[:type] && !(rule.options[:options] || rule.options[:in])
172
180
  # puts "casting #{param_value} into type: #{rule.options[:type]}"
173
- params[param_name] = type_cast_value(rule.options[:type], param_value)
181
+ param_value = type_cast_value(rule.options[:type], param_value)
182
+ if namespace
183
+ params[namespace][param_name] = param_value
184
+ else
185
+ params[param_name] = param_value
186
+ end
174
187
  end
175
188
  params
176
189
  end
@@ -199,12 +212,12 @@ module ParamsVerification
199
212
  end
200
213
  end
201
214
  end
202
-
215
+
203
216
  # @param [#WSDSL::Params::Rule] rule The optional rule
204
217
  # @param [Hash] params The request params
205
218
  # @param [String] namespace An optional namespace
206
219
  # @return [Hash] The potentially modified params
207
- #
220
+ #
208
221
  # @api private
209
222
  def self.run_optional_rule(rule, params, namespace=nil)
210
223
  param_name = rule.name.to_sym
@@ -220,7 +233,7 @@ module ParamsVerification
220
233
  params[param_name] = param_value = rule.options[:default]
221
234
  end
222
235
  end
223
-
236
+
224
237
  # 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
225
238
  if rule.options[:type] && !param_value.nil?
226
239
  if namespace
@@ -232,9 +245,8 @@ module ParamsVerification
232
245
  end
233
246
 
234
247
  choices = rule.options[:options] || rule.options[:in]
235
- if choices && param_value && !choices.include?(param_value)
236
- raise InvalidParamValue, "Value for parameter '#{param_name}' (#{param_value}) is not in the allowed set of values."
237
- end
248
+
249
+ validate_inclusion_of(param_name, param_value, choices)
238
250
 
239
251
  if rule.options[:minvalue] && param_value
240
252
  min = rule.options[:minvalue]
@@ -243,7 +255,7 @@ module ParamsVerification
243
255
 
244
256
  params
245
257
  end
246
-
258
+
247
259
  def self.unexpected_params?(params, param_names)
248
260
  # Raise an exception unless no unexpected params were found
249
261
  unexpected_keys = (params.keys - param_names)
@@ -251,8 +263,8 @@ module ParamsVerification
251
263
  raise UnexpectedParam, "Request included unexpected parameter(s): #{unexpected_keys.join(', ')}"
252
264
  end
253
265
  end
254
-
255
-
266
+
267
+
256
268
  def self.type_cast_value(type, value)
257
269
  case type
258
270
  when :integer
@@ -304,7 +316,7 @@ module ParamsVerification
304
316
  raise InvalidParamType, "Value for parameter '#{name}' (#{value}) is of the wrong type (expected #{expected_type})"
305
317
  end
306
318
  end
307
-
319
+
308
320
  def self.type_for_param(service_params, name)
309
321
  (service_params.list_required + service_params.list_optional).each do |rule|
310
322
  if rule.name.to_s == name.to_s
@@ -312,5 +324,13 @@ module ParamsVerification
312
324
  end
313
325
  end
314
326
  end
315
-
327
+
328
+ def self.validate_inclusion_of(name, value, choices)
329
+ return unless choices && value
330
+ valid = value.is_a?(Array) ? (value & choices == value) : choices.include?(value)
331
+ unless valid
332
+ raise InvalidParamValue, "Value for parameter '#{name}' (#{value}) is not in the allowed set of values."
333
+ end
334
+ end
335
+
316
336
  end
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "playerconnect-wsdsl"
8
- s.version = "0.3.3"
7
+ s.name = %q{playerconnect-wsdsl}
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Team SDOD"]
12
- s.date = "2011-10-28"
13
- s.description = "A Ruby DSL describing Web Services without implementation details."
14
- s.email = "sdod"
11
+ s.authors = [%q{Team SDOD}]
12
+ s.date = %q{2011-11-02}
13
+ s.description = %q{A Ruby DSL describing Web Services without implementation details.}
14
+ s.email = %q{sdod}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.md"
@@ -42,11 +42,11 @@ Gem::Specification.new do |s|
42
42
  "spec/wsdsl_spec.rb",
43
43
  "wsdsl.gemspec"
44
44
  ]
45
- s.homepage = "http://github.com/playerconnect/wsdsl"
46
- s.licenses = ["MIT"]
47
- s.require_paths = ["lib"]
48
- s.rubygems_version = "1.8.10"
49
- s.summary = "Web Service DSL"
45
+ s.homepage = %q{http://github.com/playerconnect/wsdsl}
46
+ s.licenses = [%q{MIT}]
47
+ s.require_paths = [%q{lib}]
48
+ s.rubygems_version = %q{1.8.5}
49
+ s.summary = %q{Web Service DSL}
50
50
 
51
51
  if s.respond_to? :specification_version then
52
52
  s.specification_version = 3
@@ -1,39 +1,70 @@
1
1
  require_relative "spec_helper"
2
2
 
3
3
  describe ParamsVerification do
4
-
4
+
5
5
  before :all do
6
6
  @service = WSList.all.find{|s| s.url == 'services/test.xml'}
7
7
  @service.should_not be_nil
8
- @valid_params = {'framework' => 'RSpec', 'version' => '1.02', 'user' => {'id' => '123'}}
9
8
  end
10
-
9
+
10
+ before do
11
+ @valid_params = {'framework' => 'RSpec', 'version' => '1.02', 'user' => {'id' => '123', 'groups' => 'manager,developer', 'skills' => 'java,ruby'}}
12
+ end
13
+
11
14
  it "should validate valid params" do
12
15
  params = @valid_params.dup
13
16
  lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should_not raise_exception
14
17
  end
15
-
18
+
16
19
  it "should return the params" do
17
20
  params = @valid_params.dup
18
21
  returned_params = ParamsVerification.validate!(params, @service.defined_params)
19
22
  returned_params.should be_an_instance_of(Hash)
20
23
  returned_params.keys.size.should >= 3
21
24
  end
22
-
25
+
26
+ it "should return array in the params" do
27
+ params = @valid_params
28
+ returned_params = ParamsVerification.validate!(params, @service.defined_params)
29
+ returned_params[:user][:groups].should == @valid_params['user']['groups'].split(",")
30
+ returned_params[:user][:skills].should == @valid_params['user']['skills'].split(",")
31
+ end
32
+
33
+ it "should not duplicate params in the root level" do
34
+ params = @valid_params
35
+ returned_params = ParamsVerification.validate!(params, @service.defined_params)
36
+ returned_params[:groups].should be_nil
37
+ returned_params[:skills].should be_nil
38
+ end
39
+
40
+ it "should raise exception when values of required param are not in the allowed list" do
41
+ params = @valid_params
42
+ params['user']['groups'] = 'admin,root,manager'
43
+ lambda { ParamsVerification.validate!(params, @service.defined_params) }.
44
+ should raise_error(ParamsVerification::InvalidParamValue)
45
+ end
46
+
47
+ it "should raise exception when values of opitonal param are not in the allowed list" do
48
+ params = @valid_params
49
+ params['user']['skills'] = 'ruby,java,php'
50
+ lambda { ParamsVerification.validate!(params, @service.defined_params) }.
51
+ should raise_error(ParamsVerification::InvalidParamValue)
52
+ end
53
+
23
54
  it "should set the default value for an optional param" do
24
- params = @valid_params.dup
55
+ params = @valid_params
25
56
  params[:timestamp].should be_nil
26
57
  returned_params = ParamsVerification.validate!(params, @service.defined_params)
27
58
  returned_params[:timestamp].should_not be_nil
28
59
  end
29
60
 
30
61
  it "should set the default value for a namespace optional param" do
31
- params = {'framework' => 'RSpec', 'version' => '1.02', 'user' => {'id' => '123'}}
62
+ params = {'framework' => 'RSpec', 'version' => '1.02', 'user' => {'id' => '123', 'groups' => 'admin'}}
32
63
  params[:user].should be_nil
33
64
  returned_params = ParamsVerification.validate!(params, @service.defined_params)
34
65
  returned_params[:user][:mailing_list].should be_true
35
66
  end
36
-
67
+
37
68
  it "should raise an exception when a required param is missing" do
38
69
  params = @valid_params.dup
39
70
  params.delete('framework')
@@ -53,19 +84,19 @@ describe ParamsVerification do
53
84
  params = {'seq' => "a b c d e g"}
54
85
  lambda{ ParamsVerification.validate!(params, service.defined_params) }.should_not raise_exception(ParamsVerification::InvalidParamType)
55
86
  end
56
-
87
+
57
88
  it "should raise an exception when a param is of the wrong type" do
58
89
  params = @valid_params.dup
59
90
  params['user']['id'] = 'abc'
60
91
  lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamType)
61
92
  end
62
-
93
+
63
94
  it "should raise an exception when a param is under the minvalue" do
64
95
  params = @valid_params.dup
65
96
  params['num'] = 1
66
97
  lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamValue)
67
98
  end
68
-
99
+
69
100
  it "should raise an exception when a param isn't in the param option list" do
70
101
  params = @valid_params.dup
71
102
  params['alpha'] = 'z'
@@ -75,19 +106,19 @@ describe ParamsVerification do
75
106
  it "should raise an exception when a nested optional param isn't in the param option list" do
76
107
  params = @valid_params.dup
77
108
  params['user']['sex'] = 'large'
78
- lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamType)
109
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamValue)
79
110
  # other service
80
111
  params = {'preference' => {'region_code' => 'us', 'language_code' => 'de'}}
81
112
  service = WSList.all.find{|s| s.url == 'preferences.xml'}
82
113
  service.should_not be_nil
83
114
  lambda{ ParamsVerification.validate!(params, service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamValue)
84
115
  end
85
-
116
+
86
117
  it "should validate that no params are passed when accept_no_params! is set on a service" do
87
118
  service = WSList.all.find{|s| s.url == "services/test_no_params.xml"}
88
119
  service.should_not be_nil
89
120
  params = @valid_params.dup
90
121
  lambda{ ParamsVerification.validate!(params, service.defined_params) }.should raise_exception
91
122
  end
92
-
123
+
93
124
  end
@@ -3,35 +3,37 @@ WSDSLSpecOptions = ['RSpec', 'Bacon'] # usually pulled from a model
3
3
  describe_service "services/test.xml" do |service|
4
4
  service.formats :xml, :json
5
5
  service.http_verb :get
6
-
6
+
7
7
  service.params do |p|
8
8
  p.string :framework, :in => WSDSLSpecOptions, :null => false, :required => true
9
-
9
+
10
10
  p.datetime :timestamp, :default => Time.now
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
15
  end
16
-
16
+
17
17
  # service.param :delta, :optional => true, :type => 'float'
18
- # # if the optional flag isn't passed, the param is considered required.
18
+ # # if the optional flag isn't passed, the param is considered required.
19
19
  # service.param :epsilon, :type => 'string'
20
-
20
+
21
21
  service.params.namespace :user do |user|
22
22
  user.integer :id, :required => :true
23
23
  user.string :sex, :in => %Q{female, male}
24
24
  user.boolean :mailing_list, :default => true
25
+ user.array :groups, :required => true, :in => %w{developer admin manager}
26
+ user.array :skills, :in => %w{ruby java networking}
25
27
  end
26
-
27
- # the response contains a list of player creation ratings each object in the list
28
-
28
+
29
+ # the response contains a list of player creation ratings each object in the list
30
+
29
31
  service.response do |response|
30
32
  response.element(:name => "player_creation_ratings") do |e|
31
33
  e.attribute :id => :integer, :doc => "id doc"
32
34
  e.attribute :is_accepted => :boolean, :doc => "is accepted doc"
33
35
  e.attribute :name => :string, :doc => "name doc"
34
-
36
+
35
37
  e.array :name => 'player_creation_rating', :type => 'PlayerCreationRating' do |a|
36
38
  a.attribute :comments => :string, :doc => "comments doc"
37
39
  a.attribute :player_id => :integer, :doc => "player_id doc"
@@ -40,17 +42,17 @@ describe_service "services/test.xml" do |service|
40
42
  end
41
43
  end
42
44
  end
43
-
45
+
44
46
  service.documentation do |doc|
45
47
  # doc.overall <markdown description text>
46
48
  doc.overall <<-DOC
47
49
  This is a test service used to test the framework.
48
50
  DOC
49
-
51
+
50
52
  # doc.params <name>, <definition>
51
53
  doc.param :framework, "The test framework used, could be one of the two following: #{WSDSLSpecOptions.join(", ")}."
52
54
  doc.param :version, "The version of the framework to use."
53
-
55
+
54
56
  # doc.example <markdown text>
55
57
  doc.example <<-DOC
56
58
  The most common way to use this service looks like that:
@@ -69,7 +71,7 @@ end
69
71
  describe_service "services.xml" do |service|
70
72
  service.formats :xml
71
73
  service.http_verb :put
72
-
74
+
73
75
  end
74
76
 
75
77
  describe_service "services/array_param.xml" do |s|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playerconnect-wsdsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-28 00:00:00.000000000Z
12
+ date: 2011-11-02 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A Ruby DSL describing Web Services without implementation details.
15
15
  email: sdod
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: '0'
65
65
  requirements: []
66
66
  rubyforge_project:
67
- rubygems_version: 1.8.10
67
+ rubygems_version: 1.8.5
68
68
  signing_key:
69
69
  specification_version: 3
70
70
  summary: Web Service DSL