snapi 0.0.7 → 0.0.8
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/lib/snapi/argument.rb +9 -0
- data/lib/snapi/errors.rb +1 -0
- data/lib/snapi/function.rb +5 -11
- data/lib/snapi/validator.rb +17 -0
- data/lib/snapi/version.rb +1 -1
- data/spec/argument_spec.rb +5 -0
- data/spec/function_spec.rb +40 -6
- data/spec/validator_spec.rb +17 -4
- metadata +2 -2
data/lib/snapi/argument.rb
CHANGED
@@ -94,6 +94,15 @@ module Snapi
|
|
94
94
|
@attributes[:required] = bool
|
95
95
|
end
|
96
96
|
|
97
|
+
# DSL Setter
|
98
|
+
# Description of the argument
|
99
|
+
#
|
100
|
+
# @param desc, String
|
101
|
+
def description(desc)
|
102
|
+
raise InvalidDescriptionError unless desc.class == String
|
103
|
+
@attributes[:description] = desc
|
104
|
+
end
|
105
|
+
|
97
106
|
# DSL Setter
|
98
107
|
# What type of value is this argument. This will impact the way in which
|
99
108
|
# this argument value gets validated later on
|
data/lib/snapi/errors.rb
CHANGED
@@ -14,6 +14,7 @@ module Snapi
|
|
14
14
|
InvalidArgumentAttributeError = Class.new(StandardError)
|
15
15
|
InvalidBooleanError = Class.new(StandardError)
|
16
16
|
InvalidCapabilityError = Class.new(StandardError)
|
17
|
+
InvalidDescriptionError = Class.new(StandardError)
|
17
18
|
InvalidFormatError = Class.new(StandardError)
|
18
19
|
InvalidFunctionCallError = Class.new(StandardError)
|
19
20
|
InvalidFunctionNameError = Class.new(StandardError)
|
data/lib/snapi/function.rb
CHANGED
@@ -60,25 +60,19 @@ module Snapi
|
|
60
60
|
# @params args Hash
|
61
61
|
# @returns Boolean
|
62
62
|
def valid_args?(args={})
|
63
|
-
valid = false
|
64
|
-
|
65
63
|
if arguments
|
66
64
|
arguments.each do |name, argument|
|
67
|
-
if argument[:required]
|
68
|
-
return false if args[name] == nil
|
69
|
-
end
|
70
65
|
|
71
|
-
if argument[:default_value]
|
72
|
-
args[name] = argument[:default_value]
|
66
|
+
if argument[:default_value]
|
67
|
+
args[name] = args[name] || argument[:default_value]
|
73
68
|
end
|
74
69
|
|
75
|
-
|
70
|
+
return false if (argument[:required] && (args[name] == nil))
|
71
|
+
return false if (argument[:required] && !(argument.valid_input? args[name]))
|
76
72
|
end
|
77
|
-
else
|
78
|
-
valid = true
|
79
73
|
end
|
80
74
|
|
81
|
-
return
|
75
|
+
return true
|
82
76
|
end
|
83
77
|
end
|
84
78
|
end
|
data/lib/snapi/validator.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'json'
|
1
2
|
module Snapi
|
2
3
|
|
3
4
|
# Helpful module to check strings against named Regexp patterns which
|
@@ -53,6 +54,7 @@ module Snapi
|
|
53
54
|
:ip => [IP_V4_REGEX, IP_V6_REGEX],
|
54
55
|
:ipv6 => [IP_V6_REGEX],
|
55
56
|
:ipv4 => [IP_V4_REGEX],
|
57
|
+
:json => [JsonValidator],
|
56
58
|
:mac => [MAC_REGEX],
|
57
59
|
:snapi_function_name => [SNAPI_FUNCTION_NAME],
|
58
60
|
:on_off => [ON_OFF_REGEX],
|
@@ -120,4 +122,19 @@ module Snapi
|
|
120
122
|
#
|
121
123
|
URI_REGEX = /https?:\/\/[\S]+/
|
122
124
|
end
|
125
|
+
|
126
|
+
# Provide an object to validate JSON with a =~ method that mirrors Regexp.=~.
|
127
|
+
# The existing validation code relies on the fact that Regexp.=~ will return
|
128
|
+
# the index for which the string matches the regex. We return 0 if the JSON
|
129
|
+
# is parsable, since the JSON string will always start at index 0.
|
130
|
+
class JsonValidator
|
131
|
+
def self.=~(obj)
|
132
|
+
begin
|
133
|
+
JSON.parse(obj)
|
134
|
+
0
|
135
|
+
rescue
|
136
|
+
nil
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
123
140
|
end
|
data/lib/snapi/version.rb
CHANGED
data/spec/argument_spec.rb
CHANGED
@@ -42,10 +42,15 @@ describe Snapi::Argument do
|
|
42
42
|
subject.type(:string).should == :string
|
43
43
|
end
|
44
44
|
|
45
|
+
it "can have a description set" do
|
46
|
+
subject.description("describe the argument").should == "describe the argument"
|
47
|
+
end
|
48
|
+
|
45
49
|
it "can return a hash of its own options" do
|
46
50
|
subject.attributes.keys.sort.should == []
|
47
51
|
end
|
48
52
|
|
53
|
+
|
49
54
|
describe "can validate types such as" do
|
50
55
|
it ":boolean" do
|
51
56
|
a = Snapi::Argument.new
|
data/spec/function_spec.rb
CHANGED
@@ -21,15 +21,49 @@ describe Snapi::Function do
|
|
21
21
|
|
22
22
|
it "can validate a ruby hash of keys and values against its arguments" do
|
23
23
|
subject.argument :argument do |arg|
|
24
|
-
arg.default_value "test"
|
25
24
|
arg.required true
|
26
25
|
arg.type :string
|
27
26
|
end
|
28
27
|
|
29
|
-
subject.
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
subject.argument :optional_argument do |arg|
|
29
|
+
arg.type :string
|
30
|
+
end
|
31
|
+
|
32
|
+
blank = {}
|
33
|
+
|
34
|
+
# no relevant args
|
35
|
+
wrong1 = {
|
36
|
+
:not_argument => "test value"
|
37
|
+
}
|
38
|
+
|
39
|
+
# just the optional arg
|
40
|
+
wrong2 = {
|
41
|
+
:optional_argument => "test value"
|
42
|
+
}
|
43
|
+
|
44
|
+
# just required, missing non-required
|
45
|
+
valid1 = {
|
46
|
+
:argument => "test value"
|
47
|
+
}
|
48
|
+
|
49
|
+
# required + optional
|
50
|
+
valid2 = {
|
51
|
+
:argument => "test value",
|
52
|
+
:optional_argument => "test value"
|
53
|
+
}
|
54
|
+
|
55
|
+
# ignores unexected key / values
|
56
|
+
valid3 = {
|
57
|
+
:argument => "test value",
|
58
|
+
:not => :relevant
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
subject.valid_args?(blank).should == false
|
63
|
+
subject.valid_args?(wrong1).should == false
|
64
|
+
subject.valid_args?(wrong2).should == false
|
65
|
+
subject.valid_args?(valid1).should == true
|
66
|
+
subject.valid_args?(valid2).should == true
|
67
|
+
subject.valid_args?(valid3).should == true
|
34
68
|
end
|
35
69
|
end
|
data/spec/validator_spec.rb
CHANGED
@@ -19,10 +19,15 @@ describe "Snapi::Validator" do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it "offers an array of regular expressions in exchange for a format type" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
ip_reg = Snapi::Validator.validation_regex[:ip]
|
23
|
+
ip_reg.class.should == Array
|
24
|
+
ip_reg.first.class.should == Regexp
|
25
|
+
ip_reg.first.respond_to?(:=~).should == true
|
26
|
+
|
27
|
+
json_reg = Snapi::Validator.validation_regex[:json]
|
28
|
+
json_reg.class.should == Array
|
29
|
+
json_reg.first.class.should == Class
|
30
|
+
json_reg.first.respond_to?(:=~).should == true
|
26
31
|
end
|
27
32
|
|
28
33
|
it "will attempt to confirm if a string matches the specified regex" do
|
@@ -30,4 +35,12 @@ describe "Snapi::Validator" do
|
|
30
35
|
Snapi::Validator.valid_input?( :ip, "192.168.10.256" ).should == false
|
31
36
|
Snapi::Validator.valid_input?( :ip, "Jake the Dog" ).should == false
|
32
37
|
end
|
38
|
+
|
39
|
+
it "validates well-formed json" do
|
40
|
+
Snapi::Validator.valid_input?( :json, "{}" ).should == true
|
41
|
+
Snapi::Validator.valid_input?( :json, "{'}" ).should == false
|
42
|
+
Snapi::Validator.valid_input?( :json, "{\"asdf\": 3}" ).should == true
|
43
|
+
Snapi::Validator.valid_input?( :json, "{asdf: 3" ).should == false
|
44
|
+
Snapi::Validator.valid_input?( :json, "[{},{}]" ).should == true
|
45
|
+
end
|
33
46
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
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: 2014-03-
|
12
|
+
date: 2014-03-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|