opt_parse_validator 0.0.12.1 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1209eb7a2eef65f85a84150ba8214a3b5357190
4
- data.tar.gz: 40e4a3c671bbe2dba9f4c61eed03c8d96366c558
3
+ metadata.gz: 07e2cf108f44ced6d598e64cc670a884ce7f07d3
4
+ data.tar.gz: 2df6f9b8762e49d5fc9ab23a43e1225e956cb9e3
5
5
  SHA512:
6
- metadata.gz: a77296172e928048d89ef685da148edd360cd35635f985425bd4aadd379a92b7fd8f97cd51fbd8c22700251487c311998db3479fc2ce808c747c71a46fe0e5e4
7
- data.tar.gz: cbeeca877fffb10c99ed4436591a586c9a1ac714de0096518cb69012fe5700f17db18eeddb71c80bd59d502e6096dc252d7d7a4bce06b96848cd86bcb48bac3d
6
+ metadata.gz: b272dba6c7e5cb56104d2fb361d76b9d792369707dcff1e53b9f21672a47c72664a5723a3e7c4fb602159608f59cddf718a7209081e17e5336b4a542c450d9db
7
+ data.tar.gz: 0210003389f1f445f1613c35a71565dc212e82a007661dc9b2e3c5e9e041f55a42eebbe204d39694eb9d25f8ab0855775226c94e50880fb1fb03aa4486d0a560
@@ -9,7 +9,7 @@ require 'opt_parse_validator/errors'
9
9
  require 'opt_parse_validator/hacks'
10
10
  require 'opt_parse_validator/opts'
11
11
  require 'opt_parse_validator/version'
12
- require 'opt_parse_validator/options_file'
12
+ require 'opt_parse_validator/options_files'
13
13
 
14
14
  # Gem namespace
15
15
  module OptParseValidator
@@ -72,6 +72,25 @@ module OptParseValidator
72
72
  post_processing
73
73
 
74
74
  @results
75
+ rescue => e
76
+ # Raise it as an OptParseValidator::Error if not already one
77
+ raise e.is_a?(Error) ? e.class : Error, e.message
78
+ end
79
+
80
+ # @return [ Void ]
81
+ def load_options_files
82
+ files_data = options_files.parse
83
+
84
+ @opts.each do |opt|
85
+ next unless files_data.key?(opt.to_sym)
86
+
87
+ @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym]))
88
+ end
89
+ end
90
+
91
+ # @return [ OptParseValidator::OptionsFiles ]
92
+ def options_files
93
+ @options_files ||= OptionsFiles.new
75
94
  end
76
95
 
77
96
  # Ensure that all required options are supplied
@@ -0,0 +1,22 @@
1
+ module OptParseValidator
2
+ module OptionsFile
3
+ # Base class, #parse should be implemented in child classes
4
+ class Base
5
+ attr_reader :path
6
+
7
+ # @param [ String ] path The file path of the option file
8
+ def initialize(path)
9
+ @path = path
10
+ end
11
+
12
+ # @return [ Hash ] a { key: value } hash
13
+ def parse
14
+ fail NotImplementedError
15
+ end
16
+
17
+ def ==(other)
18
+ path == other.path
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ require 'json'
2
+
3
+ module OptParseValidator
4
+ module OptionsFile
5
+ # Json Implementation
6
+ class JSON < Base
7
+ # @return [ Hash ] a { key: value } hash
8
+ def parse
9
+ ::JSON.parse(File.read(path), symbolize_names: true)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'yaml'
2
+
3
+ module OptParseValidator
4
+ module OptionsFile
5
+ # Yaml Implementation
6
+ class YML < Base
7
+ # @return [ Hash ] a { key: value } hash
8
+ def parse
9
+ Hash[YAML.load_file(path).map { |k, v| [k.to_sym, v] }]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ require 'opt_parse_validator/options_file/base'
2
+ require 'opt_parse_validator/options_file/json'
3
+ require 'opt_parse_validator/options_file/yml'
4
+
5
+ module OptParseValidator
6
+ # Options Files container
7
+ class OptionsFiles < Array
8
+ # @return [ Array<String> ] The downcased supported extensions list
9
+ def supported_extensions
10
+ extensions = OptionsFile.constants.select do |const|
11
+ name = OptionsFile.const_get(const)
12
+ name.is_a?(Class) && name != OptionsFile::Base
13
+ end
14
+
15
+ extensions.map { |sym| sym.to_s.downcase }
16
+ end
17
+
18
+ # @param [ String ] file_path
19
+ #
20
+ # @return [ Self ]
21
+ def <<(file_path)
22
+ return self unless File.exist?(file_path)
23
+
24
+ ext = File.extname(file_path).delete('.')
25
+
26
+ fail Error, "The option file's extension '#{ext}' is not supported" unless supported_extensions.include?(ext)
27
+
28
+ super(OptionsFile.const_get(ext.upcase).new(file_path))
29
+ end
30
+
31
+ # @return [ Hash ] a { key: value } hash
32
+ def parse
33
+ files_data = {}
34
+
35
+ each do |option_file|
36
+ data = option_file.parse
37
+ files_data.merge!(data) if data
38
+ end
39
+
40
+ files_data
41
+ end
42
+ end
43
+ end
@@ -1,4 +1,4 @@
1
1
  # Gem Version
2
2
  module OptParseValidator
3
- VERSION = '0.0.12.1'
3
+ VERSION = '0.0.13'
4
4
  end
@@ -9,10 +9,11 @@ Gem::Specification.new do |s|
9
9
  s.version = OptParseValidator::VERSION
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.required_ruby_version = '>= 2.0.0'
12
- s.authors = ['WPScanTeam - Erwan le Rousseau']
13
- s.email = ['erwan.lr@gmail.com']
14
- s.summary = 'Testing Gem'
15
- s.description = 'Testing Gem ...'
12
+ s.authors = ['WPScanTeam']
13
+ s.email = ['team@wpscan.org']
14
+ s.summary = 'Ruby OptionParser Validators'
15
+ s.description = 'Implementation of validators for the ruby OptionParser lib. ' \
16
+ 'Mainly used in the CMSScanner gem to define the cli options available'
16
17
  s.homepage = 'https://github.com/wpscanteam/OptParseValidator'
17
18
  s.license = 'MIT'
18
19
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt_parse_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12.1
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
- - WPScanTeam - Erwan le Rousseau
7
+ - WPScanTeam
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2015-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -122,9 +122,10 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0.10'
125
- description: Testing Gem ...
125
+ description: Implementation of validators for the ruby OptionParser lib. Mainly used
126
+ in the CMSScanner gem to define the cli options available
126
127
  email:
127
- - erwan.lr@gmail.com
128
+ - team@wpscan.org
128
129
  executables: []
129
130
  extensions: []
130
131
  extra_rdoc_files: []
@@ -135,7 +136,10 @@ files:
135
136
  - lib/opt_parse_validator.rb
136
137
  - lib/opt_parse_validator/errors.rb
137
138
  - lib/opt_parse_validator/hacks.rb
138
- - lib/opt_parse_validator/options_file.rb
139
+ - lib/opt_parse_validator/options_file/base.rb
140
+ - lib/opt_parse_validator/options_file/json.rb
141
+ - lib/opt_parse_validator/options_file/yml.rb
142
+ - lib/opt_parse_validator/options_files.rb
139
143
  - lib/opt_parse_validator/opts.rb
140
144
  - lib/opt_parse_validator/opts/array.rb
141
145
  - lib/opt_parse_validator/opts/base.rb
@@ -179,6 +183,6 @@ rubyforge_project:
179
183
  rubygems_version: 2.4.8
180
184
  signing_key:
181
185
  specification_version: 4
182
- summary: Testing Gem
186
+ summary: Ruby OptionParser Validators
183
187
  test_files: []
184
188
  has_rdoc:
@@ -1,62 +0,0 @@
1
- require 'json'
2
- require 'yaml'
3
-
4
- module OptParseValidator
5
- # TODO
6
- class OptParser < OptionParser
7
- def options_files
8
- @options_files ||= []
9
- end
10
-
11
- def load_options_files
12
- files_data = {}
13
-
14
- options_files.each do |file|
15
- data = parse_file(file)
16
- files_data.merge!(data) if data
17
- end
18
-
19
- @opts.each do |opt|
20
- # Annoying thing: the hash returned from parse_file is a string-full {"key"=>"value"}
21
- # and not a ruby hash {key: value} :/ As a result, symbol.to_s has to be used
22
- next unless files_data.key?(opt.to_sym.to_s)
23
-
24
- @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym.to_s]))
25
- end
26
- end
27
-
28
- protected
29
-
30
- # @param [ String ] file_path
31
- #
32
- # @return [ Hash ]
33
- def parse_file(file_path)
34
- return unless File.exist?(file_path)
35
-
36
- file_ext = File.extname(file_path).delete('.')
37
- method_to_call = "parse_#{file_ext}"
38
-
39
- fail Error, "The format #{file_ext} is not supported" unless respond_to?(method_to_call, true) # The true allows to check protected & private methods
40
-
41
- begin
42
- method(method_to_call).call(file_path)
43
- rescue
44
- raise Error, "Parse Error, #{file_path} seems to be malformed"
45
- end
46
- end
47
-
48
- # @param [ String ] file_path
49
- #
50
- # @return [ Hash ]
51
- def parse_json(file_path)
52
- JSON.parse(File.read(file_path))
53
- end
54
-
55
- # @param [ String ] file_path
56
- #
57
- # @return [ Hash ]
58
- def parse_yml(file_path)
59
- YAML.load_file(file_path)
60
- end
61
- end
62
- end