validator_ie 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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +42 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/validator_ie.rb +42 -0
  13. data/lib/validator_ie/core/mod_functions.rb +20 -0
  14. data/lib/validator_ie/core/state_ac.rb +47 -0
  15. data/lib/validator_ie/core/state_al.rb +47 -0
  16. data/lib/validator_ie/core/state_am.rb +37 -0
  17. data/lib/validator_ie/core/state_ap.rb +51 -0
  18. data/lib/validator_ie/core/state_ba.rb +47 -0
  19. data/lib/validator_ie/core/state_ce.rb +34 -0
  20. data/lib/validator_ie/core/state_df.rb +47 -0
  21. data/lib/validator_ie/core/state_es.rb +38 -0
  22. data/lib/validator_ie/core/state_go.rb +55 -0
  23. data/lib/validator_ie/core/state_ma.rb +39 -0
  24. data/lib/validator_ie/core/state_mg.rb +49 -0
  25. data/lib/validator_ie/core/state_ms.rb +38 -0
  26. data/lib/validator_ie/core/state_mt.rb +38 -0
  27. data/lib/validator_ie/core/state_pa.rb +38 -0
  28. data/lib/validator_ie/core/state_pb.rb +38 -0
  29. data/lib/validator_ie/core/state_pe.rb +35 -0
  30. data/lib/validator_ie/core/state_pi.rb +38 -0
  31. data/lib/validator_ie/core/state_rj.rb +34 -0
  32. data/lib/validator_ie/core/state_rn.rb +39 -0
  33. data/lib/validator_ie/core/state_ro.rb +38 -0
  34. data/lib/validator_ie/core/state_rr.rb +33 -0
  35. data/lib/validator_ie/core/state_rs.rb +34 -0
  36. data/lib/validator_ie/core/state_sc.rb +35 -0
  37. data/lib/validator_ie/core/state_se.rb +35 -0
  38. data/lib/validator_ie/core/state_sp.rb +41 -0
  39. data/lib/validator_ie/core/state_to.rb +35 -0
  40. data/lib/validator_ie/version.rb +3 -0
  41. data/validator_ie.gemspec +32 -0
  42. metadata +127 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae510249a9bd25de84baa0dba9e51462c418c4d8
4
+ data.tar.gz: 6b7fcecdf47e835fc7669357baccb2f36d17db96
5
+ SHA512:
6
+ metadata.gz: 8bb97cbf3331243abae15b68dd3c95c2fa566a200427923820c3e48d8690560723c6c6adc5b5c7fc2f84f5711ee9df3310563f3f4ca3b488ca76f054f6d63a8d
7
+ data.tar.gz: f03fb985f91492f0f7444fe24d00e562421a6248614157abb45efa6c6057fe4d3543f148f4f7ecb70cba1b9cdb676ad0958c0c3f9d4029c0c8e4337077665832
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at rmomogi@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validator_ie.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rodrigo Moraes de Oliveira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # ValidatorIE
2
+
3
+ Gem to validation IE from factories in Brazil
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'validator_ie'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install validator_ie
20
+
21
+ ## Usage
22
+ ```ruby
23
+ result = ValidatorIE::valid?('XXXXXXXXXXXX','AC') # Only numbers
24
+ unless result
25
+ puts result.errors.full_messages
26
+ end
27
+ ```
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rmomogi/validator_ie. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
37
+
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "validator_ie"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ require "active_model"
2
+ require "validator_ie/version"
3
+ require "validator_ie/core/state_ac"
4
+ require "validator_ie/core/state_al"
5
+ require "validator_ie/core/state_ap"
6
+ require "validator_ie/core/state_am"
7
+ require "validator_ie/core/state_ba"
8
+ require "validator_ie/core/state_ce"
9
+ require "validator_ie/core/state_df"
10
+ require "validator_ie/core/state_es"
11
+ require "validator_ie/core/state_go"
12
+ require "validator_ie/core/state_ma"
13
+ require "validator_ie/core/state_mt"
14
+ require "validator_ie/core/state_ms"
15
+ require "validator_ie/core/state_mg"
16
+ require "validator_ie/core/state_pa"
17
+ require "validator_ie/core/state_pb"
18
+ require "validator_ie/core/state_pe"
19
+ require "validator_ie/core/state_pi"
20
+ require "validator_ie/core/state_rj"
21
+ require "validator_ie/core/state_rn"
22
+ require "validator_ie/core/state_rs"
23
+ require "validator_ie/core/state_ro"
24
+ require "validator_ie/core/state_rr"
25
+ require "validator_ie/core/state_sc"
26
+ require "validator_ie/core/state_sp"
27
+ require "validator_ie/core/state_se"
28
+ require "validator_ie/core/state_to"
29
+
30
+ module ValidatorIE
31
+
32
+ def self.valid?(number,state)
33
+ name = "State#{state.upcase}"
34
+ begin
35
+ target = Object.const_get("ValidatorIE").const_get(name)
36
+ rescue
37
+ raise "Nome do estado não existente!"
38
+ end
39
+ return target.send("new", {:number => number}).send('valid?')
40
+ end
41
+
42
+ end
@@ -0,0 +1,20 @@
1
+ module ModFunctions
2
+
3
+ def self.matrix_weight(size, final, reverse)
4
+ count = initial = (reverse ? 1 : 0)
5
+ matrix = []
6
+ (size).times do |i|
7
+ count += 1
8
+ count = count > final ? initial + 1 : count
9
+ matrix << count
10
+ end
11
+ return (reverse ? matrix.reverse : matrix)
12
+ end
13
+
14
+ def self.sumX(number, at, final, reverse = true, matrix = nil)
15
+ weight = matrix.nil? ? matrix_weight(at + 1, final, reverse) : matrix
16
+ return 0 if at > weight.size - 1
17
+ sum = number[0..at].map.with_index { |v, k| v * weight[k]}.reduce(:+)
18
+ end
19
+
20
+ end
@@ -0,0 +1,47 @@
1
+ module ValidatorIE
2
+
3
+ class StateAC
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :number
7
+ validates :number, length: { minimum: 13, maximum: 13 }, numericality: true, presence: true
8
+ validate :number_should_mod11
9
+
10
+ def initialize(opts={})
11
+ opts.each do |key, value|
12
+ self.public_send("#{key}=", value)
13
+ end
14
+ end
15
+
16
+ def number_should_mod11
17
+ result1 = number_should_modX(11, number.size - 3, 9)
18
+ result2 = number_should_modX(11, number.size - 2, 9)
19
+ return if result1 && result2
20
+ errors.add(:number, 'não é válido a inscrição estadual.')
21
+ end
22
+
23
+ private
24
+ def number_should_mod11_digit(at, weight)
25
+ number_mod11 = number.split(//).map(&:to_i)
26
+ sum = number_mod11[0..at].map.with_index { |v, k| v * WEIGHT[weight][k] }.reduce(:+)
27
+
28
+ digit = 11 - sum.divmod(11).last
29
+ digit = digit.eql?(10) || digit.eql?(11) ? 0 : digit
30
+ unless number_mod11[at + 1].eql?(digit)
31
+ false
32
+ else
33
+ true
34
+ end
35
+ end
36
+
37
+ def number_should_modX(mod, at, final = number.size)
38
+ numberX = number.split(//).map(&:to_i)
39
+ sum = ModFunctions.sumX(numberX, at, final)
40
+
41
+ digit = 11 - sum.divmod(11).last
42
+ digit = digit.eql?(10) || digit.eql?(11) ? 0 : digit
43
+ return numberX[at + 1].eql?(digit)
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,47 @@
1
+ module ValidatorIE
2
+
3
+ class StateAL
4
+ include ActiveModel::Validations
5
+
6
+ # 0-Normal, 3-Produtor Rural, 5-Substituta, 7- Micro-Empresa Ambulante, 8-Micro-Empresa
7
+ TYPE = [0, 3, 5, 7, 8]
8
+
9
+ attr_accessor :number
10
+ validates :number, length: { minimum: 9, maximum: 9 }, numericality: true, presence: true
11
+ validate :number_should_code_state
12
+ validate :number_should_type_factory
13
+ validate :number_should_mod11
14
+
15
+ def initialize(opts={})
16
+ opts.each do |key, value|
17
+ self.public_send("#{key}=", value)
18
+ end
19
+ end
20
+
21
+ def number_should_code_state
22
+ number[0..1].eql?('24')
23
+ end
24
+
25
+ def number_should_type_factory
26
+ TYPE.include? number[2].to_i
27
+ end
28
+
29
+ def number_should_mod11
30
+ result = number_should_modX(11, number.size - 2)
31
+ return if result
32
+ errors.add(:number, 'não é válido a inscrição estadual.')
33
+ end
34
+
35
+ private
36
+ def number_should_modX(mod, at, final = number.size)
37
+ numberX = number.split(//).map(&:to_i)
38
+ sum = ModFunctions.sumX(numberX, at, final) * 10
39
+
40
+ digit = sum - (sum/11) * 11
41
+ digit = digit.eql?(10) ? 0 : digit
42
+ return numberX[at + 1].eql?(digit)
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,37 @@
1
+ module ValidatorIE
2
+
3
+ class StateAM
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :number
7
+ validates :number, length: { minimum: 9, maximum: 9 }, numericality: true, presence: true
8
+ validate :number_should_mod11
9
+
10
+ def initialize(opts={})
11
+ opts.each do |key, value|
12
+ self.public_send("#{key}=", value)
13
+ end
14
+ end
15
+
16
+ def number_should_mod11
17
+ result = number_should_modX(11, 7)
18
+ return if result
19
+ errors.add(:number, 'não é válido a inscrição estadual.')
20
+ end
21
+
22
+ private
23
+ def number_should_modX(mod, at, final = number.size)
24
+ numberX = number.split(//).map(&:to_i)
25
+ sum = ModFunctions.sumX(numberX, at, final)
26
+
27
+ if sum < mod
28
+ digit = mod - sum
29
+ else
30
+ digit = sum.divmod(mod).last > 1 ? mod - sum.divmod(mod).last : 0
31
+ end
32
+ return numberX[at + 1].eql?(digit)
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,51 @@
1
+ module ValidatorIE
2
+
3
+ class StateAP
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :number
7
+ validates :number, length: { minimum: 9, maximum: 9 }, numericality: true, presence: true
8
+ validate :number_should_code_state
9
+ validate :number_should_range_code
10
+ validate :number_should_mod11
11
+
12
+ def initialize(opts={})
13
+ opts.each do |key, value|
14
+ self.public_send("#{key}=", value)
15
+ end
16
+ @p = @d = 0
17
+ end
18
+
19
+ def number_should_code_state
20
+ number[0..1].eql?('03')
21
+ end
22
+
23
+ def number_should_range_code
24
+ if number.to_i.between?(3000001, 3017000)
25
+ @p = 5
26
+ @d = 0
27
+ elsif number.to_i.between?(3017001, 3019022)
28
+ @p = 9
29
+ @d = 1
30
+ end
31
+ end
32
+
33
+ def number_should_mod11
34
+ result = number_should_modX(11, 7)
35
+ return if result
36
+ errors.add(:number, 'não é válido a inscrição estadual.')
37
+ end
38
+
39
+ private
40
+ def number_should_modX(mod, at, final = number.size)
41
+ numberX = number.split(//).map(&:to_i)
42
+ sum = ModFunctions.sumX(numberX, at, final) + @p
43
+
44
+ digit = mod - sum.divmod(mod).last
45
+ digit = digit.eql?(10) ? 0 : digit
46
+ digit = digit.eql?(11) ? @d : digit
47
+ return numberX[at + 1].eql?(digit)
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,47 @@
1
+ module ValidatorIE
2
+
3
+ class StateBA
4
+ include ActiveModel::Validations
5
+
6
+ MOD10 = [0,1,2,3,4,5,8]
7
+
8
+ attr_accessor :number
9
+ validates :number, length: { minimum: 8, maximum: 9 }, numericality: true, presence: true
10
+ validate :number_should_mod
11
+
12
+ def initialize(opts={})
13
+ opts.each do |key, value|
14
+ self.public_send("#{key}=", value)
15
+ end
16
+ end
17
+
18
+ def number_should_mod
19
+ if MOD10.include? number[0].to_i
20
+ result = number_should_modX(10, number.size == 8 ? 5 : 6)
21
+ else
22
+ result = number_should_modX(11, number.size == 8 ? 5 : 6)
23
+ end
24
+
25
+ return if result
26
+ errors.add(:number, 'não é válido a inscrição estadual.')
27
+ end
28
+
29
+ private
30
+ def number_should_modX(mod, at, final = number.size)
31
+ numberX = number.split(//).map(&:to_i)
32
+ sum = ModFunctions.sumX(numberX, at, final)
33
+
34
+ divmod = sum.divmod(mod).last
35
+ digit2 = divmod.zero? ? 0 : mod - sum.divmod(mod).last
36
+ return unless numberX[at + 2].eql?(digit2) # Second digit
37
+
38
+ numberX2 = numberX[0..at] << digit2
39
+ sum = ModFunctions.sumX(numberX2, at + 1, final)
40
+ digit1 = mod - sum.divmod(mod).last
41
+
42
+ numberX[at + 1].eql?(digit1) # Firs digit
43
+ end
44
+
45
+ end
46
+
47
+ end