ip_format 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5aa55b2ec5520341e38b643a53e7de97443b4be7
4
+ data.tar.gz: 3b1984f7aab4dfdfd65a306964bfd9cb9b09e05b
5
+ SHA512:
6
+ metadata.gz: f2cbbf0a51bfdcf048edb67ff4bd4323383ccfcfa485b827091f5e94bb02ee850327d800400742d8f49de8cec7322286c7a32395372aa6810f7ac52bfc1a17d7
7
+ data.tar.gz: 6b2779eba235e65a699cd461d6db2b4bacc882fe1784c9c98a8342e079d29b57872153cda29942d86c98160bd3aaec5a0fe8d86ea8e765d5ba3e48ca7a3b2c26
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ip_format.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 A10 Networks, John Otander
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ # IpFormat
2
+
3
+ This is a gem that leverages the Resolv library to validate IP addresses. This can handle both, IPV4 and IPV6.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ip_format'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install ip_format
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ In order to validate an attribute, use the `validates` keyword:
28
+
29
+ ```ruby
30
+ class Device < ActiveRecord::Base
31
+
32
+ # ...
33
+
34
+ validates :ip, ip_format: true
35
+
36
+ # ...
37
+
38
+ end
39
+ ```
40
+
41
+ Now the ip attribute will be validated:
42
+
43
+ ```ruby
44
+ Device.new(ip: 'invalidip').valid? # => false
45
+ Device.new(ip: '192.68.0.1').valid? # => true
46
+ ```
47
+
48
+ Also, the model in question doesn't need to inherit from ActiveRecord::Base, you only need to `include ActiveModel::Validations` in your class:
49
+
50
+ ```
51
+ require 'ip_format'
52
+
53
+ class Awesome
54
+ include ActiveModel::Validations
55
+ attr_accessor :ip
56
+ validates :ip, ip_format: true
57
+ end
58
+
59
+ awesome = Awesome.new
60
+
61
+ awesome.ip = "fde4:8dba:82e1::"
62
+ awesome.valid? # => true
63
+
64
+ awesome.ip = "invalidip"
65
+ awesome.valid? # => false
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it ( https://github.com/a10networks/ip_format/fork )
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ip_format/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ip_format"
8
+ spec.version = IpFormat::VERSION
9
+ spec.authors = ["John Otander"]
10
+ spec.email = ["johnotander@gmail.com"]
11
+ spec.summary = %q{Validates IP address format.}
12
+ spec.description = %q{Validates IP address format using the resolv library.}
13
+ spec.homepage = "https://github.com/a10networks/ip_format"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+
25
+ spec.add_dependency "activemodel"
26
+ end
@@ -0,0 +1,7 @@
1
+ require "resolv"
2
+ require "active_model"
3
+ require "ip_format/version"
4
+ require "ip_format/ip_format_validator"
5
+
6
+ module IpFormat
7
+ end
@@ -0,0 +1,7 @@
1
+ class IpFormatValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ unless value =~ Resolv::AddressRegex
4
+ record.errors[attribute] << (options[:message] || "is invalid")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module IpFormat
2
+ VERSION = "0.0.1"
3
+ end
data/mv ADDED
File without changes
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe IpFormatValidator do
4
+
5
+ let(:fake_model) { FakeModel.new }
6
+
7
+ context "with valid macs" do
8
+
9
+ let(:valid_ips) { %w(192.68.1.1 fde4:8dba:82e1::) }
10
+
11
+ it "should be happy" do
12
+ valid_ips.each do |ip|
13
+ fake_model.ip = ip
14
+ expect(fake_model.valid?).to be_truthy
15
+ end
16
+ end
17
+ end
18
+
19
+ context "with invalid ips" do
20
+
21
+ let(:invalid_ips) { %w(1231231231231231 1922.68.1.1 fde4:8dba:82z1::) }
22
+
23
+ it "shouldn't be happy" do
24
+ invalid_ips.each do |ip|
25
+ fake_model.ip = ip
26
+ expect(fake_model.valid?).to be_falsey
27
+ end
28
+ end
29
+ end
30
+
31
+ context "with allow_blank: true" do
32
+
33
+ let(:fake_model) { FakeModelWithBlankIp.new }
34
+ let(:blank_ips) { ['', nil, ' '] }
35
+
36
+ it "should allow blank ips" do
37
+ blank_ips.each do |blank_ip|
38
+ fake_model.ip = blank_ip
39
+ expect(fake_model.valid?).to be_truthy
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+ require 'active_model'
5
+ require 'ip_format'
6
+
7
+ class FakeModel
8
+ include ActiveModel::Validations
9
+
10
+ attr_accessor :ip
11
+
12
+ validates :ip, ip_format: true
13
+ end
14
+
15
+ class FakeModelWithBlankIp
16
+ include ActiveModel::Validations
17
+
18
+ attr_accessor :ip
19
+
20
+ validates :ip, ip_format: true, allow_blank: true
21
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Otander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Validates IP address format using the resolv library.
70
+ email:
71
+ - johnotander@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - ip_format.gemspec
82
+ - lib/ip_format.rb
83
+ - lib/ip_format/ip_format_validator.rb
84
+ - lib/ip_format/version.rb
85
+ - mv
86
+ - spec/ip_format_validator_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/a10networks/ip_format
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Validates IP address format.
112
+ test_files:
113
+ - spec/ip_format_validator_spec.rb
114
+ - spec/spec_helper.rb