regex_forms 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3075e6ddd7fa0bbd62e52839e730525d14534e84271fd03c06dc3039a68dcd8a
4
+ data.tar.gz: b9552b5043c35d4ca0fba8e0bef626edc94736ec55c9e462f2bd9312be7af8f4
5
+ SHA512:
6
+ metadata.gz: d9a6039f59d802716e311aa53dc576b13f427948d41f721d54fd6dc6967196a8105b58ae81c47797ea1ddd940f34ab82ac7dc2036934ebbeb259ae1d1b956a04
7
+ data.tar.gz: 5ad6930a10303839d49fc9aeccf6ebc26dd511decccced96422a73115736c9471ce35c4d3c9e9ca2ef9d31b35f839537bdf35046207567122b06bfbf4509c9c5
data/README.md ADDED
Binary file
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RegexForms
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "regex_forms/version"
4
+
5
+ module RegexForms
6
+ class Error < StandardError; end
7
+
8
+ class ValidateCPF
9
+ attr_reader :cpf
10
+
11
+
12
+ def initialize(cpf)
13
+ @cpf = cpf
14
+ end
15
+
16
+ def validate
17
+ regex_cpf(cpf)
18
+ end
19
+
20
+ private
21
+
22
+ def regex_cpf(cpf)
23
+ if @cpf.nil? || cpf.empty?
24
+ puts "Invalid"
25
+ return {cpf: cpf, validation: false, message: Error.new("CPF is nil or empty")}
26
+ end
27
+
28
+ regex = /^(?<t_1>\d{3})\.(?<t_2>\d{3})\.(?<t_3>\d{3})\-(?<t_4>\d{2})$/
29
+ if regex.match(cpf)
30
+ return {cpf: cpf, validation: true, message: "Validated CPF"}
31
+ else
32
+ return {cpf: cpf, validation: false, message: Error.new("Invalid cpf: #{cpf}")}
33
+ end
34
+ end
35
+ end
36
+
37
+ class ValidateCNPJ
38
+ attr_reader :cnpj
39
+
40
+ def initialize(cnpj)
41
+ @cnpj = cnpj
42
+ end
43
+
44
+ def validate
45
+ return invalid_response("CNPJ is nil, empty, or all zeros") if invalid_cnpj?
46
+
47
+ regex = /^(?<t_1>\d{2})\.(?<t_2>\d{3})\.(?<t_3>\d{3})\/0001-(?<t_4>\d{2})$/
48
+
49
+ if regex.match(cnpj)
50
+ valid_response("Validated CNPJ")
51
+ else
52
+ invalid_response("Invalid CNPJ: #{cnpj}")
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def invalid_cnpj?
59
+ cnpj.nil? || cnpj.empty? || cnpj.gsub(/[^\d]/, '') == '00000000000000'
60
+ end
61
+
62
+ def valid_response(message)
63
+ { cnpj: cnpj, validation: true, message: message }
64
+ end
65
+
66
+ def invalid_response(message)
67
+ puts "Invalid"
68
+ { cnpj: cnpj, validation: false, message: message }
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/regex_forms/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "regex_forms"
7
+ spec.version = RegexForms::VERSION
8
+ spec.authors = ["Guiherme Dantas"]
9
+ spec.email = ["gui.dg1@hotmail.com"]
10
+
11
+ spec.summary = "lib content regex for forms validation"
12
+ spec.description = "Regex is a versatile Ruby library designed to simplify form validation by providing a collection of regular expressions tailored for common form fields. Whether you're building a web application, validating user input, or processing form submissions, this library offers a set of regex patterns that can be easily integrated into your validation logic."
13
+ spec.homepage = "https://github.com/Dants0/regex_forms"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/Dants0/regex_forms"
20
+ spec.metadata["changelog_uri"] = "https://github.com/Dants0/regex_forms"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,4 @@
1
+ module RegexForms
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: regex_forms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guiherme Dantas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Regex is a versatile Ruby library designed to simplify form validation
14
+ by providing a collection of regular expressions tailored for common form fields.
15
+ Whether you're building a web application, validating user input, or processing
16
+ form submissions, this library offers a set of regex patterns that can be easily
17
+ integrated into your validation logic.
18
+ email:
19
+ - gui.dg1@hotmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - README.md
25
+ - Rakefile
26
+ - lib/regex_forms.rb
27
+ - lib/regex_forms/version.rb
28
+ - regex_forms.gemspec
29
+ - sig/regex_forms.rbs
30
+ homepage: https://github.com/Dants0/regex_forms
31
+ licenses: []
32
+ metadata:
33
+ allowed_push_host: https://rubygems.org
34
+ homepage_uri: https://github.com/Dants0/regex_forms
35
+ source_code_uri: https://github.com/Dants0/regex_forms
36
+ changelog_uri: https://github.com/Dants0/regex_forms
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.5.3
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: lib content regex for forms validation
56
+ test_files: []