geekier_factory 0.1.4 → 0.1.5

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.
@@ -1,6 +1,7 @@
1
1
  module GeekierFactory
2
2
  class Action
3
3
  AttributesMissing = Class.new(Exception)
4
+ ValidationException = Class.new(Exception)
4
5
 
5
6
  def initialize(api, structure)
6
7
  @api = api
@@ -78,7 +79,8 @@ module GeekierFactory
78
79
  @api.api_connection
79
80
  end
80
81
 
81
- def call(param_values)
82
+ def call(param_values = {})
83
+ validate_parameter_values(param_values)
82
84
  reqhash = request_hash(param_values)
83
85
  response = api_connection.run_request(reqhash[:verb], reqhash[:url], reqhash[:body], reqhash[:headers])
84
86
  handle_response!(response)
@@ -88,6 +90,46 @@ module GeekierFactory
88
90
  retry
89
91
  end
90
92
 
93
+ def validate_parameter_values(param_values)
94
+ errors = Hash.new { |hash,key|hash[key] = [] }
95
+
96
+ param_values.each do |k,v|
97
+ parameter = params.detect{|p| p['name'] == k.to_s}
98
+
99
+ errors[k] << "expected to be #{parameter['dataType']} (was #{p.class})" unless case parameter['dataType']
100
+ when 'byte'
101
+ true
102
+ when 'string'
103
+ v.is_a?(String)
104
+ when 'boolean'
105
+ v.is_a?(TrueClass) ||
106
+ v.is_a?(FalseClass)
107
+ when 'int'
108
+ v.is_a?(Integer)
109
+ when 'float'
110
+ v.is_a?(Float)
111
+ when 'double'
112
+ v.is_a?(Double)
113
+ when 'Date'
114
+ (v.is_a?(Date) || Date.parse(v)) rescue false
115
+ end
116
+
117
+ if parameter.has_key?('allowableValues')
118
+ vals = case parameter['allowableValues']['valueType']
119
+ when 'RANGE'
120
+ Range.new(parameter['allowableValues']['min'].to_i, parameter['allowableValues']['max'].to_i)
121
+ when 'LIST'
122
+ parameter['allowableValues']['values']
123
+ end
124
+ errors[k] << "expected to be one of #{vals}" unless vals.include?(v)
125
+ end
126
+ end
127
+
128
+ if errors.any?
129
+ raise ValidationException.new(errors.inspect)
130
+ end
131
+ end
132
+
91
133
  def error_responses
92
134
  (@structure['errorResponses'] || []) + @api.error_responses
93
135
  end
@@ -1,3 +1,3 @@
1
1
  module GeekierFactory
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -40,6 +40,19 @@
40
40
  "name": "b2",
41
41
  "paramType": "body",
42
42
  "required": false
43
+ },
44
+ {
45
+ "allowMultiple": false,
46
+ "dataType": "int",
47
+ "description": "third body parameter - integer",
48
+ "name": "b3",
49
+ "paramType": "body",
50
+ "required": false,
51
+ "allowableValues": {
52
+ "valueType": "RANGE",
53
+ "min": 1,
54
+ "max": 5
55
+ }
43
56
  },
44
57
  {
45
58
  "allowMultiple": false,
@@ -55,7 +68,11 @@
55
68
  "description": "specify format",
56
69
  "name": "format",
57
70
  "paramType": "path",
58
- "required": true
71
+ "required": false,
72
+ "allowableValues": {
73
+ "valueType": "LIST",
74
+ "values": ["json", "yaml"]
75
+ }
59
76
  }
60
77
  ],
61
78
  "summary": "Call to test the API"
@@ -21,19 +21,19 @@ class TestFactory < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  test "action should have 4 parameters" do
24
- assert_equal 6, @action.params.size
25
- assert_equal ['q1', 'q2', 'b1', 'b2', 'action', 'format'], @action.params.map{ |p| p['name'] }
24
+ assert_equal 7, @action.params.size
25
+ assert_equal ['q1', 'q2', 'b1', 'b2', 'b3', 'action', 'format'], @action.params.map{ |p| p['name'] }
26
26
  end
27
27
 
28
28
  test "action should have 2 body and 2 query parameters and they should be different" do
29
- assert_equal 2, @action.body_params.size
29
+ assert_equal 3, @action.body_params.size
30
30
  assert_equal 2, @action.url_params.size
31
31
  assert_not_equal @action.body_params, @action.url_params
32
32
  end
33
33
 
34
34
  test "calling the action should call the right url" do
35
35
  stub_http_request(:post, "localhost/api/v0.1/call.json").
36
- with(:body => {:b1 => "body", :b2 => "test"}, :query => {:q1 => 'testing', :q2 => 'query'})
37
- @action.call(:b1 => "body", :b2 => "test", :q1 => 'testing', :q2 => 'query', :action => 'call', :format => 'json')
36
+ with(:body => {:b1 => "body", :b3 => 3}, :query => {:q1 => 'testing', :q2 => 'query'})
37
+ @action.call(:b1 => "body", :b3 => 3, :q1 => 'testing', :q2 => 'query', :action => 'call', :format => 'json')
38
38
  end
39
39
  end
@@ -0,0 +1,56 @@
1
+ require 'helper'
2
+
3
+ class TestValidation < Test::Unit::TestCase
4
+ setup do
5
+ @api = GeekierFactory.factorize(File.join(File.dirname(File.expand_path(__FILE__)), 'mock_definition.json'))
6
+ @action = @api.available_actions["test api"].first
7
+ end
8
+
9
+ test "should complain if all mandatory parameters are missing" do
10
+ assert_raise GeekierFactory::Action::AttributesMissing do
11
+ @action.call
12
+ end
13
+ end
14
+
15
+ test "should complain if any one mandatory parameter is missing" do
16
+ params = {:q1 => 'a', :b1 => 'b', :action => 'c'}
17
+ params.keys.each do |mp|
18
+ hsh = params.dup
19
+ hsh.delete(mp)
20
+ assert_raise GeekierFactory::Action::AttributesMissing do
21
+ @action.call hsh
22
+ end
23
+ end
24
+ end
25
+
26
+ test "should complain if any non-mandatory path parameter is missing (because I don't know yet what to do when those are missing)" do
27
+ params = {:q1 => 'a', :b1 => 'b', :action => 'c'}
28
+ assert_raise GeekierFactory::Action::AttributesMissing do
29
+ @action.call params
30
+ end
31
+ end
32
+
33
+ test "should complain if a parameter is the wrong type" do
34
+ params = {:q1 => 'a', :b1 => 'b', :action => 'c'}
35
+ wrong_values = [{}, 4, true, nil, [], Object.new]
36
+ wrong_values.each do |wval|
37
+ assert_raise GeekierFactory::Action::ValidationException do
38
+ @action.call params.merge(:format => wval)
39
+ end
40
+ end
41
+ end
42
+
43
+ test "should complain if a parameter is not included in a list" do
44
+ params = {:q1 => 'a', :b1 => 'b', :action => 'c'}
45
+ assert_raise GeekierFactory::Action::ValidationException do
46
+ @action.call params.merge(:format => 'xml')
47
+ end
48
+ end
49
+
50
+ test "should complain if a parameter is not included in a range" do
51
+ params = {:q1 => 'a', :b1 => 'b', :action => 'c', :format => 'json'}
52
+ assert_raise GeekierFactory::Action::ValidationException do
53
+ @action.call params.merge(:b3 => 7)
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geekier_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-05 00:00:00.000000000 Z
13
+ date: 2013-03-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -160,6 +160,7 @@ files:
160
160
  - test/helper.rb
161
161
  - test/mock_definition.json
162
162
  - test/test_factory.rb
163
+ - test/test_validation.rb
163
164
  homepage: http://github.com/rulesio/geekier_factory_gem
164
165
  licenses: []
165
166
  post_install_message: