semi 0.2.1 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80fb95486ba180d4b96a45f40360ffa8df51b044
4
- data.tar.gz: ed494f2aac254260547e5ebe76d9ce32a61c7d29
3
+ metadata.gz: c88aca233d156427126e2693c9d21017314452b2
4
+ data.tar.gz: 4f34c48307d462e0f99ef3b9f7d24fad6a5ff8af
5
5
  SHA512:
6
- metadata.gz: 1e9b7a6311d3fedae0fefd7de3332f191434913e1817dd858ba7bbda072ea360005483ff58cea1b0fe3de9bc584c568d43c6c654333ddda5e76cc5f17de57c86
7
- data.tar.gz: 013a7b0ecb085481b9c9e81195ca04ae029c4f3101b5ef94d5b9d6da1209f84c66b2fc2a78381ded61061a6f792fb64b48047dbf2f497b098178dc2514f14c65
6
+ metadata.gz: 32c8dd6903b8b38a3c0bd2c6f7cecfce654e09be9c03a5de9b9fa51e7507b9f1791cd5281d92ac7c1af3c0e40ba447fb3b70e10ceaa2731dbf0249dcd8130629
7
+ data.tar.gz: dfecc677729c0941179a42af404b07c553bba3f2eb665ac8b405d852a23c4b25dae586d5493be8c312d008d7fd9d64d0485eb2387c27936dce41110511164a00
data/Gemfile CHANGED
@@ -1 +1,4 @@
1
- gemspec
1
+ source "https://rubygems.org/"
2
+
3
+ gemspec
4
+
data/lib/semi/driver.rb CHANGED
@@ -10,7 +10,12 @@ module Semi
10
10
 
11
11
  def start
12
12
  # Merge defaults into the environment
13
- @dictionary = @config.defaults.dup
13
+ @dictionary = {}
14
+ @config.defaults.each_pair do |name,val|
15
+ @dictionary[name] = Semi::Variable.import(val)
16
+ end
17
+
18
+ puts "dictionary = #{@dictionary.inspect}"
14
19
  @dictionary.merge!(ENV.to_h)
15
20
 
16
21
  # Check any validations being asserted
data/lib/semi/validate.rb CHANGED
@@ -11,14 +11,18 @@ module Semi
11
11
 
12
12
  tests.keys.each do |rule|
13
13
  case rule
14
- when 'required'
14
+ when 'required'
15
15
  tests[rule] = true unless value.nil?
16
16
  when 'integer'
17
- tests[rule] = true if value.is_a? Integer
17
+ tests[rule] = Semi::Variables::Integer.validate(value)
18
18
  when 'string'
19
- tests[rule] = true if value.is_a? String
19
+ tests[rule] = Semi::Variables::String.validate(value)
20
20
  when 'boolean'
21
- tests[rule] = true if value.is_a? Boolean
21
+ tests[rule] = Semi::Variables::Boolean.validate(value)
22
+ when 'path'
23
+ tests[rule] = Semi::Variables::Path.validate(value)
24
+ when 'url'
25
+ tests[rule] = Semi::Variables::Url.validate(value)
22
26
  end
23
27
 
24
28
  # test for regular expression
@@ -0,0 +1,42 @@
1
+
2
+ require 'semi/variables/string'
3
+ require 'semi/variables/integer'
4
+ require 'semi/variables/boolean'
5
+ require 'semi/variables/path'
6
+ require 'semi/variables/url'
7
+
8
+ module Semi
9
+
10
+ class VariableError < RuntimeError; end
11
+
12
+ module Variable
13
+
14
+ def self.import(val)
15
+ puts "#{val}: #{val.class}"
16
+ # look for the obsure patterns before returning a string var
17
+ case
18
+ when Semi::Variables::Url::validate(val)
19
+ return Semi::Variables::Url.new(val)
20
+ when Semi::Variables::Path.validate(val)
21
+ return Semi::Variables::Path.new(val)
22
+ when Semi::Variables::Boolean.validate(val)
23
+ return Semi::Variables::Boolean.new(val)
24
+ when Semi::Variables::Integer.validate(val)
25
+ return Semi::Variables::Integer.new(val)
26
+ when val.class == Fixnum
27
+ return Semi::Variables::Integer.new(val)
28
+ when val.class == TrueClass
29
+ return Semi::Variables::Boolean.new(val)
30
+ when val.class == FalseClass
31
+ return Semi::Variables::Boolean.new(val)
32
+ else
33
+ return Semi::Variables::String.new(val)
34
+ end
35
+
36
+ end
37
+
38
+
39
+
40
+
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module Semi
3
+ module Variables
4
+ class Base
5
+
6
+ def initialize(val)
7
+ set(val)
8
+ end
9
+
10
+ def set(val)
11
+ @value = val
12
+ end
13
+
14
+ def to_s
15
+ @value
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'base'
2
+
3
+ module Semi::Variables
4
+ class Boolean < Semi::Variables::Base
5
+
6
+
7
+ def set(val)
8
+ # test to see if the value is a common true value
9
+ if value =~ /true|yes|enable/i
10
+ @value = true
11
+ elsif value =~ /false|no|disable/i
12
+ @value = false
13
+ else
14
+ raise Semi::VariableError, "#{val} trying to be set as a boolean"
15
+ end
16
+ end
17
+
18
+ def validate
19
+ self.validate(@value)
20
+ end
21
+
22
+ def self.validate(value)
23
+ real_value = nil
24
+
25
+ # test to see if the value is a common true value
26
+ if value =~ /true|yes|enable/i
27
+ real_value = true
28
+ elsif value =~ /false|no|disable/i
29
+ real_value = false
30
+ end
31
+
32
+ if !!real_value == real_value
33
+ return true
34
+ end
35
+ false
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'base'
2
+
3
+ module Semi::Variables
4
+ class Integer < Semi::Variables::Base
5
+
6
+ #def set(val)
7
+ # # test to see if the value is a common true value
8
+ # if value =~ /true|yes|enable/i
9
+ # @value = true
10
+ # elsif value =~ /false|no|disable/i
11
+ # @value = false
12
+ # else
13
+ # raise Semi::VariableError, "#{val} trying to be set as a boolean"
14
+ # end
15
+ #end
16
+
17
+ def validate
18
+ self.validate(@value)
19
+ end
20
+
21
+ def self.validate(value)
22
+ if value.class == Fixnum
23
+ return true
24
+ elsif value.class == Semi::Variables::Integer and value.value =~ /^\d+$/
25
+ return true
26
+ end
27
+ false
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'base'
2
+
3
+ module Semi::Variables
4
+ class Path < Semi::Variables::Base
5
+
6
+ @@path_re = Regexp.new('^(?<path>(?:\.{1,2}|\/).*?)\/(?<file>[^\/]+)?$')
7
+
8
+ def validate
9
+ self.validate(@value)
10
+ end
11
+
12
+ def self.validate(value)
13
+ if ['String', 'Semi::Variables::Path'].include? value.class.to_s
14
+ if @@path_re.match(value)
15
+ return true
16
+ end
17
+ end
18
+ false
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'base'
2
+
3
+ module Semi::Variables
4
+ class String < Semi::Variables::Base
5
+
6
+ def validate
7
+ self.validate(@value)
8
+ end
9
+
10
+ def self.validate(value)
11
+ if value.class.to_s == 'String'
12
+ return true
13
+ end
14
+ false
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'base'
2
+
3
+ module Semi::Variables
4
+ class Url < Semi::Variables::Base
5
+
6
+ @@url_re = Regexp.new('^(?<proto>https?|ftp|file):\/\/(?<host>[a-z\.0-9\-_]+)?(?::(?<port>\d{1,5}))?\/?(?<path>.*?)\/?(?<file>[^\/\?]+)?(?:\?(?<params>.*?))?$', Regexp::IGNORECASE)
7
+
8
+ def validate
9
+ self.validate(@value)
10
+ end
11
+
12
+ def self.validate(value)
13
+ if ['String', 'Semi::Variables::Url'].include? value.class.to_s
14
+ if @@url_re.match(value)
15
+ return true
16
+ end
17
+ end
18
+ false
19
+ end
20
+
21
+ end
22
+ end
data/lib/semi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Semi
2
- version = '0.2.1'
2
+ version = '0.3.0'
3
3
  end
data/lib/semi.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'semi/version'
2
2
  require 'semi/config'
3
3
  require 'semi/validate'
4
+ require 'semi/variable'
4
5
  require 'semi/driver'
@@ -16,7 +16,24 @@ describe "Semi::validator" do
16
16
  ['foobar', ['/foo/'], true],
17
17
  ['foobar', ['/fubar/'], false],
18
18
  [nil, 'required', false],
19
- [nil, ['required'], false]
19
+ [nil, ['required'], false],
20
+ ['/etc/passwd', 'path', true],
21
+ ['../parent', ['path'], true],
22
+ ['.005', 'path', false],
23
+ ['http://www.simple.com',
24
+ 'url', true],
25
+ ['http://abit.more.complex.com/',
26
+ 'url', true],
27
+ ['http://www.complex.com:8080/some/page.html',
28
+ 'url', true],
29
+ ['http://really.complex.com:888/a/lot/of/dirs/1/2/3/4/5/p.json?key=value&key2=val2',
30
+ 'url', true],
31
+ ['no', 'boolean', true],
32
+ ['false', 'boolean', true],
33
+ ['yes', 'boolean', true],
34
+ ['true', 'boolean', true],
35
+ ['enable', 'boolean', true],
36
+ ['disable', 'boolean', true]
20
37
  ].each do |ruleset|
21
38
  it "validates #{ruleset[0]} against #{ruleset[1]}" do
22
39
  if ruleset[2] == true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerard Hickey
@@ -82,6 +82,13 @@ files:
82
82
  - lib/semi/config.rb
83
83
  - lib/semi/driver.rb
84
84
  - lib/semi/validate.rb
85
+ - lib/semi/variable.rb
86
+ - lib/semi/variables/base.rb
87
+ - lib/semi/variables/boolean.rb
88
+ - lib/semi/variables/integer.rb
89
+ - lib/semi/variables/path.rb
90
+ - lib/semi/variables/string.rb
91
+ - lib/semi/variables/url.rb
85
92
  - lib/semi/version.rb
86
93
  - spec/semi_validator_spec.rb
87
94
  - spec/spec_helper.rb