opt_parse_validator 0.0.2 → 0.0.3

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: 1b0a34c11c84d6fa32580ed925dac42346cee747
4
- data.tar.gz: 7c50ea36943042d5cf7f8b77c15fd93b0969339c
3
+ metadata.gz: a2723f3613fc6738288980bec82ff2541520ff7d
4
+ data.tar.gz: 0917da11c83e5ed23ee707b8c0858c367e5caac9
5
5
  SHA512:
6
- metadata.gz: 051c726116d357bd876f87d9a113ba3c00f53a2b146e92e69d2447bef77de471fe2150f2c834356d52a1ec0b5b14f0b8e94e3177e3e6c84190ceac2068be1579
7
- data.tar.gz: 910103742d3c7cfab15980043a02a169a9708f4ed3f11086ee280eec9d8cdd57621d82a8cf04f7739083f7384fd50c98e45ac2e5a129175c5a5461f52d2c8ff6
6
+ metadata.gz: 0629015ce8aec7f45c5a30e96d4c9ebe2188fffd14c5813f800fa7786cdb694c7480958e6c97bd6826d869348f8364e25b92addcebd82ddb1ace3d3930116c2e
7
+ data.tar.gz: a36713b133c62e53dcdcc7cdf34188850be49c6fd9dc1f6122597e05fab572d92d85b3e6df0369667ea33afef6a7983f7fc9bc49858e1fd615a496d138ef3500
data/README.md CHANGED
@@ -34,7 +34,10 @@ OptParseValidator
34
34
  - :writable
35
35
  - Proxy
36
36
  - :protocols
37
+ - :default_protocol
37
38
  - String
38
39
  - URI
39
40
  - :protocols
41
+ - :default_protocol
40
42
  - URL
43
+ - :default_protocol
@@ -5,14 +5,22 @@ module OptParseValidator
5
5
  @allowed_protocols ||= [*attrs[:protocols]]
6
6
  end
7
7
 
8
+ # The default protocol (or scheme) to use if none was given
9
+ def default_protocol
10
+ @default_protocol ||= attrs[:default_protocol]
11
+ end
12
+
8
13
  # @param [ String ] value
9
14
  #
10
15
  # @return [ String ]
11
16
  def validate(value)
12
- uri = Addressable::URI.parse(value)
13
- protocols = allowed_protocols
17
+ uri = Addressable::URI.parse(value)
18
+
19
+ if !uri.scheme && default_protocol
20
+ uri = Addressable::URI.parse("#{default_protocol}://#{value}")
21
+ end
14
22
 
15
- unless protocols.empty? || protocols.include?(uri.scheme)
23
+ unless allowed_protocols.empty? || allowed_protocols.include?(uri.scheme)
16
24
  # For future refs: will have to check if the uri.scheme exists,
17
25
  # otherwise it means that the value was empty
18
26
  fail Addressable::URI::InvalidURIError
@@ -1,4 +1,4 @@
1
1
  # Gem Version
2
2
  module OptParseValidator
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
@@ -8,6 +8,7 @@ describe OptParseValidator::OptURI do
8
8
  describe '#new, #allowed_protocols' do
9
9
  context 'when no attrs supplied' do
10
10
  its(:allowed_protocols) { should be_empty }
11
+ its(:default_protocol) { should be nil }
11
12
  end
12
13
 
13
14
  context 'when only one protocol supplied' do
@@ -29,7 +30,7 @@ describe OptParseValidator::OptURI do
29
30
  end
30
31
 
31
32
  describe '#validate' do
32
- context 'when the allowed_protocols is empty' do
33
+ context 'when allowed_protocols is empty' do
33
34
  it 'accepts all protocols' do
34
35
  %w(http ftp file).each do |p|
35
36
  expected = "#{p}://testing"
@@ -53,6 +54,22 @@ describe OptParseValidator::OptURI do
53
54
  expect(opt.validate(expected)).to eq expected
54
55
  end
55
56
  end
57
+
58
+ context 'when default_protocol' do
59
+ let(:attrs) { { default_protocol: 'ftp' } }
60
+
61
+ context 'when the argument already contains a protocol' do
62
+ it 'does not add the default protocol' do
63
+ expect(opt.validate('http://ex.lo')).to eq 'http://ex.lo'
64
+ end
65
+ end
66
+
67
+ context 'when no protocol given in the argument' do
68
+ it 'adds it' do
69
+ expect(opt.validate('ex.lo')).to eq 'ftp://ex.lo'
70
+ end
71
+ end
72
+ end
56
73
  end
57
74
 
58
75
  end
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - WPScanTeam - Erwan le Rousseau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-16 00:00:00.000000000 Z
11
+ date: 2014-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable