universal_validators 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c612cf99cd54b0e0cf0ef558418b150fef066ac
4
+ data.tar.gz: 602eeb6a191930ca9605eb083b026685fd2ea09d
5
+ SHA512:
6
+ metadata.gz: f00ec484a9d0cabfe682cbfbc0d19dbad6f2a686478fef0b0a8b52afbecaa65fb800a476ff49918bb451e1ac748c9dc6c587588bdb5a8a96e53212102c196569
7
+ data.tar.gz: 6d7897c95314d7ef430ab4704ec29155bd272cda8786f550ced6760d945f4a3191207f92f9d10896dc4cbbafbf6733eec82d580f93af7057b273c48390844809
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ LineLength:
5
+ Enabled: true
6
+ Max: 120
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in universal_validators.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Damian Baćkowski
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
+ # UniversalValidators [![Build Status](https://travis-ci.org/dbackowski/universal_validators.svg)](https://travis-ci.org/dbackowski/universal_validators)
2
+
3
+ Library to validate:
4
+
5
+ * date
6
+ * date_time
7
+ * ip address
8
+ * mac address
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'universal_validators'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install universal_validators
25
+
26
+ ## Usage
27
+
28
+ Date validation:
29
+
30
+ ```ruby
31
+ date = UniversalValidators::DateValidator.new('2015-02-25')
32
+ date.valid?
33
+ => true
34
+ ```
35
+
36
+ DateTime validation:
37
+
38
+ ```ruby
39
+ date_time = UniversalValidators::DateTimeValidator.new('2015-02-25 12:33')
40
+ date_time.valid?
41
+ => true
42
+ ```
43
+
44
+ Ip address validation (without netmask):
45
+
46
+ ```ruby
47
+ ip = UniversalValidators::IpValidator.new('127.0.0.1')
48
+ ip.valid?
49
+ => true
50
+ ```
51
+
52
+ Ip address validation (with netmask):
53
+
54
+ ```ruby
55
+ ip = UniversalValidators::IpValidator.new('127.0.0.1/32', true)
56
+ ip.valid?
57
+ => true
58
+ ```
59
+
60
+ Mac address validation:
61
+
62
+ ```ruby
63
+ mac = UniversalValidators::MacAddressValidator.new('00:0A:CD:00:CC:FE')
64
+ mac.valid?
65
+ => true
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
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,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,5 @@
1
+ require 'universal_validators/version'
2
+ require 'universal_validators/date_time_validator'
3
+ require 'universal_validators/date_validator'
4
+ require 'universal_validators/mac_address_validator'
5
+ require 'universal_validators/ip_validator'
@@ -0,0 +1,13 @@
1
+ require 'date'
2
+
3
+ module UniversalValidators
4
+ class DateTimeValidator
5
+ def initialize(datetime)
6
+ @datetime = datetime.to_s
7
+ end
8
+
9
+ def valid?
10
+ DateTime.parse(@datetime).is_a?(DateTime) rescue false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'date'
2
+
3
+ module UniversalValidators
4
+ class DateValidator
5
+ def initialize(date)
6
+ @date = date.to_s
7
+ end
8
+
9
+ def valid?
10
+ Date.parse(@date).is_a?(Date) rescue false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module UniversalValidators
2
+ class IpValidator
3
+ def initialize(ip, mask_presence = false)
4
+ @ip = ip.to_s
5
+ @mask_presence = mask_presence
6
+ end
7
+
8
+ def valid?
9
+ begin
10
+ check_format_with_netmask if @mask_presence
11
+ check_format_without_netmask unless @mask_presence
12
+ rescue
13
+ return
14
+ end
15
+
16
+ @ip.split('.').select { |pair| pair.to_i > 255 }.length == 0
17
+ end
18
+
19
+ private
20
+
21
+ def check_format_without_netmask
22
+ fail ArgumentError unless @ip.match(/\A[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\Z/)
23
+ end
24
+
25
+ def check_format_with_netmask
26
+ fail ArgumentError unless @ip.match(/\A[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}\Z/)
27
+ fail ArgumentError if @ip.split('/')[1].to_i > 32
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ module UniversalValidators
2
+ class MacAddressValidator
3
+ def initialize(mac_address)
4
+ @mac_address = mac_address.to_s
5
+ end
6
+
7
+ def valid?
8
+ mac_address = @mac_address.scan(/[0-9a-f]+/i).join
9
+
10
+ return if mac_address.length != 12
11
+
12
+ mac_address.scan(/.{2}/).select { |pair| pair.to_i(16) > 255 }.length == 0
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module UniversalValidators
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe UniversalValidators::DateTimeValidator do
4
+ describe '.valid?' do
5
+ context 'with valid date time' do
6
+ it "return true" do
7
+ date_time = UniversalValidators::DateTimeValidator.new('2015-02-25 12:33')
8
+ expect(date_time).to be_valid
9
+ end
10
+ end
11
+
12
+ context 'with invalid date time' do
13
+ it "return false" do
14
+ date_time = UniversalValidators::DateTimeValidator.new('2015-02-25 12:133')
15
+ expect(date_time).to_not be_valid
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe UniversalValidators::DateValidator do
4
+ describe '.valid?' do
5
+ context 'with valid date' do
6
+ it "return true" do
7
+ date = UniversalValidators::DateValidator.new('2015-02-25')
8
+ expect(date).to be_valid
9
+ end
10
+ end
11
+
12
+ context 'with invalid date' do
13
+ it "return false" do
14
+ date = UniversalValidators::DateValidator.new('2015-02-29')
15
+ expect(date).to_not be_valid
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe UniversalValidators::IpValidator do
4
+ describe '.valid?' do
5
+ context 'with valid ip address' do
6
+ it "return true" do
7
+ ip = UniversalValidators::IpValidator.new('127.0.0.1')
8
+ expect(ip).to be_valid
9
+
10
+ ip = UniversalValidators::IpValidator.new('127.0.0.1/32', true)
11
+ expect(ip).to be_valid
12
+ end
13
+ end
14
+
15
+ context 'with invalid ip address' do
16
+ it "return false" do
17
+ ip = UniversalValidators::IpValidator.new('256.0.0.1')
18
+ expect(ip).to_not be_valid
19
+
20
+ ip = UniversalValidators::IpValidator.new('127.0.0.1', true)
21
+ expect(ip).to_not be_valid
22
+
23
+ ip = UniversalValidators::IpValidator.new('127.0.0.1/33', true)
24
+ expect(ip).to_not be_valid
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe UniversalValidators::MacAddressValidator do
4
+ describe '.valid?' do
5
+ context 'with valid mac address' do
6
+ it "return true" do
7
+ mac = UniversalValidators::MacAddressValidator.new('00:0A:CD:00:CC:FE')
8
+ expect(mac).to be_valid
9
+ end
10
+ end
11
+
12
+ context 'with invalid mac address' do
13
+ it "return false" do
14
+ mac = UniversalValidators::MacAddressValidator.new('00:0A:CD:00:CC:FG')
15
+ expect(mac).to_not be_valid
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ require 'universal_validators'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'universal_validators/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "universal_validators"
8
+ spec.version = UniversalValidators::VERSION
9
+ spec.authors = ["Damian Baćkowski"]
10
+ spec.email = ["damianbackowski@gmail.com"]
11
+ spec.summary = %q{Library to validate: date, date_time, ip address, mac address.}
12
+ spec.description = %q{Library to validate: date, date_time, ip address, mac address.}
13
+ spec.homepage = "https://github.com/dbackowski/universal_validators"
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 "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2.0"
24
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: universal_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Damian Baćkowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ description: 'Library to validate: date, date_time, ip address, mac address.'
56
+ email:
57
+ - damianbackowski@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .rubocop.yml
65
+ - .travis.yml
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/universal_validators.rb
71
+ - lib/universal_validators/date_time_validator.rb
72
+ - lib/universal_validators/date_validator.rb
73
+ - lib/universal_validators/ip_validator.rb
74
+ - lib/universal_validators/mac_address_validator.rb
75
+ - lib/universal_validators/version.rb
76
+ - spec/date_time_validator_spec.rb
77
+ - spec/date_validator_spec.rb
78
+ - spec/ip_validator_spec.rb
79
+ - spec/mac_address_validator_spec.rb
80
+ - spec/spec_helper.rb
81
+ - universal_validators.gemspec
82
+ homepage: https://github.com/dbackowski/universal_validators
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.0.14
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: 'Library to validate: date, date_time, ip address, mac address.'
106
+ test_files:
107
+ - spec/date_time_validator_spec.rb
108
+ - spec/date_validator_spec.rb
109
+ - spec/ip_validator_spec.rb
110
+ - spec/mac_address_validator_spec.rb
111
+ - spec/spec_helper.rb