activemodel-ipaddr_validator 0.0.1 → 0.0.2
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 +4 -4
- data/.coverrails.yml +1 -0
- data/Gemfile +4 -0
- data/README.md +4 -1
- data/activemodel-ipaddr_validator.gemspec +1 -1
- data/lib/ipaddr_validator.rb +9 -4
- data/lib/ipaddr_validator/version.rb +1 -1
- data/spec/ipaddr_validator_spec.rb +165 -42
- data/spec/spec_helper.rb +3 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 037d801907b11ed64c3a96e80547ac4b656de833
|
4
|
+
data.tar.gz: ab0d5cdc1cbd57954d348438e6a955754e0750a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a623d971fb524e0c7e17c433e13552465bc7d1ce5ebc2f9dbf6d417d994c5d65d5f2bbb31c2881a0eed23823ed39b76dd57bf2e72e5e9d31ac8696ace8c68a9e
|
7
|
+
data.tar.gz: e5bec3f0def4897570ea9bb75e5b440c308e258019f3bf7c58fd2b3a10becbc812d6276e1648ebf396cae380cfffd4b6ced6be2ab7214177561fc7b7d7c2fd92
|
data/.coverrails.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
# activemodel-ipaddr_validator
|
1
|
+
# activemodel-ipaddr_validator
|
2
|
+
|
3
|
+
[](https://travis-ci.org/yuku-t/activemodel-ipaddr_validator) [](https://codeclimate.com/github/yuku-t/activemodel-ipaddr_validator) [](https://coveralls.io/r/yuku-t/activemodel-ipaddr_validator) [](https://gemnasium.com/yuku-t/activemodel-ipaddr_validator)
|
4
|
+
|
2
5
|
|
3
6
|
## Usage
|
4
7
|
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ['Yuku Takahashi']
|
9
9
|
spec.email = ['yuku@qiita.com']
|
10
10
|
spec.summary = 'A IPv4 and IPv6 validator for Rails 3 and 4.'
|
11
|
-
spec.homepage = 'https://github.com/
|
11
|
+
spec.homepage = 'https://github.com/yuku-t/activemodel-ipaddr_validator'
|
12
12
|
spec.license = 'MIT'
|
13
13
|
|
14
14
|
spec.files = `git ls-files -z`.split("\x0")
|
data/lib/ipaddr_validator.rb
CHANGED
@@ -11,8 +11,8 @@ class IpaddrValidator < ActiveModel::EachValidator
|
|
11
11
|
else
|
12
12
|
[value]
|
13
13
|
end
|
14
|
-
array.all? do |
|
15
|
-
validate_single_ipaddr(
|
14
|
+
array.all? do |value|
|
15
|
+
validate_single_ipaddr(value, options)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -22,8 +22,13 @@ class IpaddrValidator < ActiveModel::EachValidator
|
|
22
22
|
|
23
23
|
private
|
24
24
|
|
25
|
-
def validate_single_ipaddr(
|
26
|
-
ip =
|
25
|
+
def validate_single_ipaddr(value, options)
|
26
|
+
ip =
|
27
|
+
if value.is_a?(IPAddr)
|
28
|
+
value
|
29
|
+
else
|
30
|
+
IPAddr.new(value)
|
31
|
+
end
|
27
32
|
case
|
28
33
|
when options[:ipv4] && ip.ipv4?
|
29
34
|
true
|
@@ -38,6 +38,41 @@ shared_examples_for 'invalid array is given', given: :invalid_array do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
shared_examples_for 'valid IPv4 IPAddr is given', given: :ipv4_ipaddr do
|
42
|
+
let(:value) do
|
43
|
+
IPAddr.new('192.168.2.0/24')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
shared_examples_for 'valid IPv6 IPAddr is given', given: :ipv6_ipaddr do
|
48
|
+
let(:value) do
|
49
|
+
IPAddr.new('3ffe:505:2::1')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
shared_examples_for 'valid IPv4 array of IPAddrs is given', given: :ipv4_array_ipaddr do
|
54
|
+
let(:value) do
|
55
|
+
[IPAddr.new('192.168.2.0/24'), IPAddr.new('192.168.3.0/24')]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
shared_examples_for 'valid IPv6 array of IPAddrs is given', given: :ipv6_array_ipaddr do
|
60
|
+
let(:value) do
|
61
|
+
[IPAddr.new('3ffe:505:2::1'), IPAddr.new('3ffe:505:2::2')]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
shared_examples_for 'invalid string is given', given: :invalid_ipaddr do
|
66
|
+
let(:value) do
|
67
|
+
nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
shared_examples_for 'invalid array is given', given: :invalid_array_ipaddr do
|
72
|
+
let(:value) do
|
73
|
+
[nil]
|
74
|
+
end
|
75
|
+
end
|
41
76
|
# Shared examples for options.
|
42
77
|
|
43
78
|
IpaddrValidator.default_options.keys.product([true, false]).each do |option, value|
|
@@ -62,42 +97,74 @@ describe IpaddrValidator do
|
|
62
97
|
described_class.valid?(value, options)
|
63
98
|
end
|
64
99
|
|
65
|
-
context 'when
|
66
|
-
context '
|
67
|
-
context 'and
|
68
|
-
|
100
|
+
context 'when strings are given' do
|
101
|
+
context 'when a valid IPv4 string is given', given: :ipv4 do
|
102
|
+
context 'and ipv4 option is true', ipv4: true do
|
103
|
+
context 'and array option is true', array: true do
|
104
|
+
it { should be_falsey }
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'and array option is false', array: false do
|
108
|
+
it { should be_truthy }
|
109
|
+
end
|
69
110
|
end
|
70
111
|
|
71
|
-
context 'and
|
72
|
-
it { should
|
112
|
+
context 'and ipv4 option is false', ipv4: false do
|
113
|
+
it { should be_falsey }
|
73
114
|
end
|
74
115
|
end
|
75
116
|
|
76
|
-
context '
|
77
|
-
|
78
|
-
|
79
|
-
|
117
|
+
context 'when a valid IPv6 string is given', given: :ipv6 do
|
118
|
+
context 'and ipv6 option is true', ipv6: true do
|
119
|
+
context 'and array option is true', array: true do
|
120
|
+
it { should be_falsey }
|
121
|
+
end
|
80
122
|
|
81
|
-
|
82
|
-
|
83
|
-
|
123
|
+
context 'and array option is false', array: false do
|
124
|
+
it { should be_truthy }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'and ipv6 option is false', ipv6: false do
|
84
129
|
it { should be_falsey }
|
85
130
|
end
|
131
|
+
end
|
86
132
|
|
87
|
-
|
88
|
-
|
133
|
+
context 'when a valid IPv4 array is given', given: :ipv4_array do
|
134
|
+
context 'and ipv4 option is true', ipv4: true do
|
135
|
+
context 'and array option is true', array: true do
|
136
|
+
it { should be_truthy }
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'and array option is false', array: false do
|
140
|
+
it { should be_falsey }
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'and ipv4 option is false', ipv4: false do
|
145
|
+
it { should be_falsey }
|
89
146
|
end
|
90
147
|
end
|
91
148
|
|
92
|
-
context '
|
93
|
-
|
149
|
+
context 'when a valid IPv6 array is given', given: :ipv6_array do
|
150
|
+
context 'and ipv6 option is true', ipv6: true do
|
151
|
+
context 'and array option is true', array: true do
|
152
|
+
it { should be_truthy }
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'and array option is false', array: false do
|
156
|
+
it { should be_falsey }
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context 'and ipv6 option is false', ipv6: false do
|
161
|
+
it { should be_falsey }
|
162
|
+
end
|
94
163
|
end
|
95
|
-
end
|
96
164
|
|
97
|
-
|
98
|
-
context 'and ipv4 option is true', ipv4: true do
|
165
|
+
context 'when an invalid string is given', given: :invalid do
|
99
166
|
context 'and array option is true', array: true do
|
100
|
-
it { should
|
167
|
+
it { should be_falsey }
|
101
168
|
end
|
102
169
|
|
103
170
|
context 'and array option is false', array: false do
|
@@ -105,44 +172,100 @@ describe IpaddrValidator do
|
|
105
172
|
end
|
106
173
|
end
|
107
174
|
|
108
|
-
context '
|
109
|
-
|
175
|
+
context 'when an invalid array is given', given: :invalid_array do
|
176
|
+
context 'and array option is true', array: true do
|
177
|
+
it { should be_falsey }
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'and array option is false', array: false do
|
181
|
+
it { should be_falsey }
|
182
|
+
end
|
110
183
|
end
|
111
184
|
end
|
112
185
|
|
113
|
-
context 'when
|
114
|
-
context '
|
115
|
-
context 'and
|
116
|
-
|
186
|
+
context 'when IPAddrs are given' do
|
187
|
+
context 'when a valid IPv4 IPAddr is given', given: :ipv4_ipaddr do
|
188
|
+
context 'and ipv4 option is true', ipv4: true do
|
189
|
+
context 'and array option is true', array: true do
|
190
|
+
it { should be_falsey }
|
191
|
+
end
|
192
|
+
|
193
|
+
context 'and array option is false', array: false do
|
194
|
+
it { should be_truthy }
|
195
|
+
end
|
117
196
|
end
|
118
197
|
|
119
|
-
context 'and
|
198
|
+
context 'and ipv4 option is false', ipv4: false do
|
120
199
|
it { should be_falsey }
|
121
200
|
end
|
122
201
|
end
|
123
202
|
|
124
|
-
context '
|
125
|
-
|
203
|
+
context 'when a valid IPv6 IPAddr is given', given: :ipv6_ipaddr do
|
204
|
+
context 'and ipv6 option is true', ipv6: true do
|
205
|
+
context 'and array option is true', array: true do
|
206
|
+
it { should be_falsey }
|
207
|
+
end
|
208
|
+
|
209
|
+
context 'and array option is false', array: false do
|
210
|
+
it { should be_truthy }
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context 'and ipv6 option is false', ipv6: false do
|
215
|
+
it { should be_falsey }
|
216
|
+
end
|
126
217
|
end
|
127
|
-
end
|
128
218
|
|
129
|
-
|
130
|
-
|
131
|
-
|
219
|
+
context 'when a valid IPv4 array of IPAddrs is given', given: :ipv4_array_ipaddr do
|
220
|
+
context 'and ipv4 option is true', ipv4: true do
|
221
|
+
context 'and array option is true', array: true do
|
222
|
+
it { should be_truthy }
|
223
|
+
end
|
224
|
+
|
225
|
+
context 'and array option is false', array: false do
|
226
|
+
it { should be_falsey }
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context 'and ipv4 option is false', ipv4: false do
|
231
|
+
it { should be_falsey }
|
232
|
+
end
|
132
233
|
end
|
133
234
|
|
134
|
-
context '
|
135
|
-
|
235
|
+
context 'when a valid IPv6 array of IPAddrs is given', given: :ipv6_array_ipaddr do
|
236
|
+
context 'and ipv6 option is true', ipv6: true do
|
237
|
+
context 'and array option is true', array: true do
|
238
|
+
it { should be_truthy }
|
239
|
+
end
|
240
|
+
|
241
|
+
context 'and array option is false', array: false do
|
242
|
+
it { should be_falsey }
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
context 'and ipv6 option is false', ipv6: false do
|
247
|
+
it { should be_falsey }
|
248
|
+
end
|
136
249
|
end
|
137
|
-
end
|
138
250
|
|
139
|
-
|
140
|
-
|
141
|
-
|
251
|
+
context 'when an invalid IPAddr is given', given: :invalid_ipaddr do
|
252
|
+
context 'and array option is true', array: true do
|
253
|
+
it { should be_falsey }
|
254
|
+
end
|
255
|
+
|
256
|
+
context 'and array option is false', array: false do
|
257
|
+
it { should be_falsey }
|
258
|
+
end
|
142
259
|
end
|
143
260
|
|
144
|
-
context '
|
145
|
-
|
261
|
+
context 'when an invalid array of IPAddrs is given', given: :invalid_array_ipaddr do
|
262
|
+
context 'and array option is true', array: true do
|
263
|
+
it { should be_falsey }
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'and array option is false', array: false do
|
267
|
+
it { should be_falsey }
|
268
|
+
end
|
146
269
|
end
|
147
270
|
end
|
148
271
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel-ipaddr_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuku Takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -73,6 +73,7 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- ".coverrails.yml"
|
76
77
|
- ".gitignore"
|
77
78
|
- ".travis.yml"
|
78
79
|
- Gemfile
|
@@ -87,7 +88,7 @@ files:
|
|
87
88
|
- lib/ipaddr_validator/version.rb
|
88
89
|
- spec/ipaddr_validator_spec.rb
|
89
90
|
- spec/spec_helper.rb
|
90
|
-
homepage: https://github.com/
|
91
|
+
homepage: https://github.com/yuku-t/activemodel-ipaddr_validator
|
91
92
|
licenses:
|
92
93
|
- MIT
|
93
94
|
metadata: {}
|
@@ -107,8 +108,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
108
|
version: '0'
|
108
109
|
requirements: []
|
109
110
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.4.5.1
|
111
112
|
signing_key:
|
112
113
|
specification_version: 4
|
113
114
|
summary: A IPv4 and IPv6 validator for Rails 3 and 4.
|
114
115
|
test_files: []
|
116
|
+
has_rdoc:
|