kangal 1.2.0 → 1.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6bd8a15b84a66f86f63627a1d21f00fa8024f3f
4
- data.tar.gz: cd4ef8987f3e905d8877b8d5a425b6ac4606e1b8
3
+ metadata.gz: 451e3bb3d08ea408e4a54fded98b428db3c0cdbf
4
+ data.tar.gz: 445d0d87de688fd8f2f52c4b897a1a315363834c
5
5
  SHA512:
6
- metadata.gz: 7794dee17a3dfe11dec2ac7be8db4bc30073ad5aa64af43fdca1df4c5e65465997f7b50276f4aa2b5933724e8a2dfc81b1fb518240143750956727d8f82456aa
7
- data.tar.gz: c316de0ddb2a5d48937380faafa23dfcd6cbe8dada74e97245fe3649e82261337630e1b998c19dac00bc3e2ca6e5f11e7f41027a49879d3524c671ee2c1be439
6
+ metadata.gz: dd2c1ac1fcbceba099f7b06ec480ab3632177f05b63816582cfa9fdcec837423fd314332206acd671618492732ee4aadba534a001ea528f615113debfc276cf0
7
+ data.tar.gz: 62ca23261a9a961d837fbeed31f6dbf1434b21b521053f8f6006192224f9ad39159e2f80d9214c5006bf5f48786c6fdbfab9df702d5ba26dbfa46c4dc3d515a2
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 lab2023 - internet technologies
1
+ Copyright (c) 2013 lab2023 - information technologies
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -80,7 +80,7 @@ p.valid? # => true
80
80
 
81
81
  **Default reserved names:** www, ftp, mail, pop, smtp, admin, ssl, sftp, http, https
82
82
 
83
- ## Turkish Government Identity Number Validator
83
+ ### Turkish Government Identity Number Validator
84
84
  ```ruby
85
85
  require 'kangal'
86
86
  class User
@@ -88,7 +88,7 @@ class User
88
88
  attr_accessor :identity
89
89
 
90
90
  # Identity Number validator
91
- validates :email, presence: :true, identity_number: :true
91
+ validates :email, presence: :true, tcno: :true
92
92
  end
93
93
 
94
94
  p = User.new
@@ -104,7 +104,7 @@ p.identity = "83317527040" # (this isn't a real identity number)
104
104
  p.valid? # => true
105
105
  ```
106
106
 
107
- ## Turkish Government Tax Number Validator
107
+ ### Turkish Government Tax Number Validator
108
108
  ```ruby
109
109
  require 'kangal'
110
110
  class User
@@ -112,7 +112,7 @@ class User
112
112
  attr_accessor :tax_number
113
113
 
114
114
  # Identity Number validator
115
- validates :tax_number, presence: :true, tax_number: :true
115
+ validates :tax_number, presence: :true, tcvkn: :true
116
116
  end
117
117
 
118
118
  p = User.new
@@ -128,6 +128,34 @@ p.tax_number = "6120069217" # (this isn't a real tax number)
128
128
  p.valid? # => true
129
129
  ```
130
130
 
131
+ ### IP Validator
132
+ ```ruby
133
+ require 'kangal'
134
+ class Server
135
+ include ActiveModel::Validations
136
+ attr_accessor :ip
137
+
138
+ # Ip validator
139
+ validates :ip, presence: :true, ip: :true
140
+ end
141
+
142
+ p = Server.new
143
+ p.ip = "10.10.10.256"
144
+ p.valid? # => false
145
+
146
+ p = Server.new
147
+ p.ip = "2222.22.22.22"
148
+ p.valid? # => false
149
+
150
+ p = Server.new
151
+ p.ip = "255.255.255.255"
152
+ p.valid? # => true
153
+
154
+ p = Server.new
155
+ p.ip = "132.254.111.10"
156
+ p.valid? # => true
157
+ ```
158
+
131
159
  ## Bugs and Feedback
132
160
 
133
161
  If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
@@ -1,6 +1,8 @@
1
1
  en:
2
2
  kangal:
3
3
  validations:
4
+ common:
5
+ invalid: 'is invalid'
4
6
  email:
5
7
  invalid: 'is invalid'
6
8
  subdomain:
@@ -10,7 +12,13 @@ en:
10
12
  alphanumeric: 'must be alphanumeric; A-Z, 0-9 or hyphen'
11
13
  identity_number:
12
14
  invalid: 'is invalid.'
13
- tax_number:
15
+ tckn:
14
16
  invalid: 'is invalid.'
15
17
  tax_number:
16
- invalid: 'is invalid.'
18
+ invalid: 'is invalid.'
19
+ tcvkn:
20
+ invalid: 'is invalid.'
21
+ ip:
22
+ invalid: 'is invalid.'
23
+ phone:
24
+ tr: 'should be start with 90'
@@ -1,6 +1,8 @@
1
1
  en:
2
2
  kangal:
3
3
  validations:
4
+ common:
5
+ invalid: 'est invalide'
4
6
  email:
5
7
  invalid: 'est invalide'
6
8
  subdomain:
@@ -10,7 +12,13 @@ en:
10
12
  alphanumeric: 'il faut être alphanumérique; A-Z, 0-9 ou trait'
11
13
  identity_number:
12
14
  invalid: 'est invalide.'
15
+ tckn:
16
+ invalid: 'est invalide.'
13
17
  tax_number:
14
18
  invalid: 'est invalide.'
19
+ tcvkn:
20
+ invalid: 'est invalide.'
15
21
  ip:
16
22
  invalid: 'est invalide.'
23
+ phone:
24
+ tr: '90 ile başlaması lazım.'
@@ -1,6 +1,8 @@
1
1
  tr:
2
2
  kangal:
3
3
  validations:
4
+ common:
5
+ invalid: 'geçersizdir.'
4
6
  email:
5
7
  invalid: 'geçersizdir.'
6
8
  subdomain:
@@ -10,7 +12,13 @@ tr:
10
12
  alphanumeric: 'alfanümerik olmalıdır; A-Z, 0-9 veya tire'
11
13
  identity_number:
12
14
  invalid: 'geçersizdir.'
15
+ tckn:
16
+ invalid: 'geçersizdir.'
13
17
  tax_number:
14
18
  invalid: 'geçersizdir.'
19
+ tcvkn:
20
+ invalid: 'geçersizdir.'
15
21
  ip:
16
22
  invalid: 'geçersizdir.'
23
+ phone:
24
+ tr: '90 ile başlaması lazım.'
@@ -2,7 +2,10 @@ require 'kangal/version'
2
2
  require 'kangal/email'
3
3
  require 'kangal/subdomain'
4
4
  require 'kangal/identity_number'
5
+ require 'kangal/tckn'
5
6
  require 'kangal/tax_number'
7
+ require 'kangal/tcvkn'
6
8
  require 'kangal/ip'
9
+ require 'kangal/phone'
7
10
 
8
11
  I18n.load_path += Dir.glob(File.expand_path('../../config/locales/**/*',__FILE__))
@@ -4,6 +4,8 @@ require 'active_model/validations'
4
4
  class IdentityNumberValidator < ActiveModel::EachValidator
5
5
  def validate_each(record, attribute, value)
6
6
 
7
+ ActiveSupport::Deprecation.warn "`identity_number: :true` is deprecated and may be removed from future releases, use `tckn: true` instead.", caller
8
+
7
9
  return if options[:allow_nil] && value.nil?
8
10
  return if options[:allow_blank] && value.blank?
9
11
 
@@ -0,0 +1,17 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+
4
+ # All phones number should be store e.164 format standard
5
+ class PhoneValidator < ActiveModel::EachValidator
6
+ def validate_each(object, attribute, value)
7
+
8
+ return if options[:allow_nil] && value.nil?
9
+ return if options[:allow_blank] && value.blank?
10
+
11
+ if options[:tr]
12
+ object.errors[attribute] << I18n.t('kangal.validations.common.invalid') unless value =~ /^(90)+\d{10}$/i
13
+ end
14
+
15
+ object.errors[attribute] << I18n.t('kangal.validations.phone.tr') unless value =~ /^\d{11,14}$/i
16
+ end
17
+ end
@@ -4,6 +4,8 @@ require 'active_model/validations'
4
4
  class TaxNumberValidator < ActiveModel::EachValidator
5
5
  def validate_each(object, attribute, value)
6
6
 
7
+ ActiveSupport::Deprecation.warn "`tax_number: :true` is deprecated and may be removed from future releases, use `tcvkn: true` instead.", caller
8
+
7
9
  return if options[:allow_nil] && value.nil?
8
10
  return if options[:allow_blank] && value.blank?
9
11
 
@@ -0,0 +1,33 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+
4
+ class TcknValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+
7
+ return if options[:allow_nil] && value.nil?
8
+ return if options[:allow_blank] && value.blank?
9
+
10
+ digits = value[0..-3].each_char.map(&:to_i).each_with_index
11
+ first, last =
12
+ digits.reduce([0, 0]) do |memo, (digit, idx)|
13
+ add = digit * (idx.even? ? 7 : -1)
14
+ [
15
+ (memo.first + add) % 10,
16
+ (memo.last + digit + add) % 10
17
+ ]
18
+ end
19
+
20
+ valid = (!invalid_value?(value, 11) && value[-2] == first.to_s && value[-1] == last.to_s)
21
+ record.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => 'kangal.validations.tckn')) unless valid
22
+ end
23
+
24
+ private
25
+
26
+ def invalid_value?(str, length)
27
+ str.nil? || str.length != length || !numeric?(str)
28
+ end
29
+
30
+ def numeric?(str)
31
+ !!(str =~ /\A[[:digit:]]+\Z/)
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+
4
+ class TcvknValidator < ActiveModel::EachValidator
5
+ def validate_each(object, attribute, value)
6
+
7
+ return if options[:allow_nil] && value.nil?
8
+ return if options[:allow_blank] && value.blank?
9
+
10
+
11
+ digits = value[0..-2].each_char.map(&:to_i).each_with_index
12
+
13
+ checksum =
14
+ digits.reduce(0) do |memo, (digit, idx)|
15
+ rev_idx = 9 - idx
16
+ coeff = [1, 2, 4, 8, 7, 5][rev_idx % 6]
17
+ result = (digit + rev_idx) % 10
18
+
19
+ if result.nonzero?
20
+ result = (coeff * result) % 9
21
+ result = 9 if result.zero?
22
+ end
23
+ memo += result
24
+ end
25
+
26
+ valid = !invalid_value?(value, 10) && ((checksum + value[-1].to_i) % 10 == 0)
27
+
28
+ object.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => 'kangal.validations.tax_number')) unless valid
29
+ end
30
+
31
+ private
32
+
33
+ def invalid_value?(str, length)
34
+ str.nil? || str.length != length || !numeric?(str)
35
+ end
36
+
37
+ def numeric?(str)
38
+ !!(str =~ /\A[[:digit:]]+\Z/)
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Kangal
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.3'.freeze
3
3
  end
@@ -6,13 +6,8 @@ end
6
6
 
7
7
  describe 'Email format' do
8
8
 
9
- let(:invalid_emails) do
10
- %w(invalid email)
11
- end
12
-
13
- let(:valid_emails) do
14
- %w(info@lab2023.com abc@ab.com)
15
- end
9
+ let(:invalid_emails) { %w(invalid email) }
10
+ let(:valid_emails) { %w(info@lab2023.com abc@ab.com) }
16
11
 
17
12
  it 'should be invalid' do
18
13
  invalid_emails.each { |email| User.new(email: email).valid?.should be_false }
@@ -6,13 +6,8 @@ end
6
6
 
7
7
  describe 'Identity number format' do
8
8
 
9
- let(:invalid_identity_numbers) do
10
- %w(1 3 434542345678)
11
- end
12
-
13
- let(:valid_identity_numbers) do
14
- %w(21432692144 55679901158 83317527040)
15
- end
9
+ let(:invalid_identity_numbers) { %w(1 3 434542345678) }
10
+ let(:valid_identity_numbers) { %w(21432692144 55679901158 83317527040) }
16
11
 
17
12
  it 'should be invalid' do
18
13
  invalid_identity_numbers.each { |identity| Profile.new(identity: identity).valid?.should be_false }
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ class Phone < SpecModel
4
+ validates :number, phone: true
5
+ end
6
+
7
+ class PhoneTr < SpecModel
8
+ validates :number, phone: {tr: true}
9
+ end
10
+
11
+ class PhoneTrMobile < SpecModel
12
+ validates :number, phone: {tr_and_mobile: true}
13
+ end
14
+
15
+ class PhoneTrNormal < SpecModel
16
+ validates :number, phone: {tr_and_normal: true}
17
+ end
18
+
19
+ describe 'Phone format' do
20
+
21
+ let(:valid_phone) do
22
+ %w(
23
+ 905532620000
24
+ 902122120000
25
+ )
26
+ end
27
+
28
+ let(:invalid_phone) do
29
+ %w(
30
+ abde
31
+ 1234
32
+ 12341234123443434
33
+ )
34
+ end
35
+
36
+ it 'should be valid' do
37
+ valid_phone.each { |number| Phone.new(number: number).valid?.should be_true }
38
+ end
39
+
40
+ it 'should be invalid' do
41
+ invalid_phone.each { |number| Phone.new(number: number).valid?.should be_false }
42
+ end
43
+
44
+ let(:valid_tr) do
45
+ %w(
46
+ 905326200000
47
+ 908508850000
48
+ )
49
+ end
50
+
51
+ let(:invalid_tr) do
52
+ %w(
53
+ +1231231233200
54
+ 00100000000000
55
+ )
56
+ end
57
+
58
+ it 'should be valid with tr option' do
59
+ valid_tr.each { |tr_phone| PhoneTr.new(number: tr_phone).valid?.should be_true }
60
+ end
61
+
62
+ it 'should be invalid with tr option' do
63
+ invalid_tr.each { |tr_phone| PhoneTr.new(number: tr_phone).valid?.should be_false }
64
+ end
65
+
66
+ it 'should be valid with tr and mobile option' do
67
+
68
+ end
69
+
70
+ it 'should be invalid with tr and mobile option' do
71
+
72
+ end
73
+
74
+ it 'should be valid with tr and normal option' do
75
+
76
+ end
77
+
78
+ it 'should be invalid with tr and normal option' do
79
+
80
+ end
81
+
82
+ end
@@ -15,8 +15,4 @@ class SpecModel
15
15
  @attributes[key]
16
16
  end
17
17
 
18
- end
19
-
20
- #RSpec.configure do |config|
21
- #
22
- #end
18
+ end
@@ -4,15 +4,14 @@ class Company < SpecModel
4
4
  validates :subdomain, subdomain: true
5
5
  end
6
6
 
7
- describe 'Subdomain format' do
7
+ class Account < SpecModel
8
+ validates :subdomain, subdomain: {reserved: %w{help reserved_word}}
9
+ end
8
10
 
9
- let(:invalid_subdomains) do
10
- %w(-lab2023 https ab)
11
- end
11
+ describe 'Subdomain format' do
12
12
 
13
- let(:valid_subdomains) do
14
- %w(lab2023 lab2023-internet)
15
- end
13
+ let(:invalid_subdomains) { %w(-lab2023 https ab) }
14
+ let(:valid_subdomains) { %w(lab2023 lab2023-internet) }
16
15
 
17
16
  it 'should be invalid' do
18
17
  invalid_subdomains.each { |subdomain| Company.new(subdomain: subdomain).valid?.should be_false }
@@ -22,4 +21,14 @@ describe 'Subdomain format' do
22
21
  valid_subdomains.each { |subdomain| Company.new(subdomain: subdomain).valid?.should be_true }
23
22
  end
24
23
 
24
+ it 'should be invalid because of reserved' do
25
+ account = Account.new(subdomain: 'reserved_word')
26
+ account.valid?
27
+ account.errors.messages[:subdomain].should include(I18n.t(:reserved, scope: 'kangal.validations.subdomain'))
28
+ end
29
+
30
+ it 'should be valid because of reserved' do
31
+ Account.new(subdomain: 'unreservedword').valid?.should be_true
32
+ end
33
+
25
34
  end
@@ -6,13 +6,8 @@ end
6
6
 
7
7
  describe 'tax number format' do
8
8
 
9
- let(:invalid_tax_numbers) do
10
- %w(1 3 434542345678 43454234567)
11
- end
12
-
13
- let(:valid_tax_numbers) do
14
- %w(2640020928 6120069217)
15
- end
9
+ let(:invalid_tax_numbers) { %w(1 3 434542345678 43454234567) }
10
+ let('valid_tax_numbers') { %w(2640020928 6120069217) }
16
11
 
17
12
  it 'should be invalid' do
18
13
  invalid_tax_numbers.each { |tax| CompanyDetail.new(tax: tax).valid?.should be_false }
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ class Profile < SpecModel
4
+ validates :identity, tckn: true
5
+ end
6
+
7
+ describe 'Tckn number format' do
8
+
9
+ let(:invalid_tckn) { %w(1 3 434542345678) }
10
+ let(:valid_tckn) { %w(21432692144 55679901158 83317527040) }
11
+
12
+ it 'should be invalid' do
13
+ invalid_tckn.each { |identity| Profile.new(identity: identity).valid?.should be_false }
14
+ end
15
+
16
+ it 'should be valid' do
17
+ valid_tckn.each { |identity| Profile.new(identity: identity).valid?.should be_true }
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ class CompanyDetail < SpecModel
4
+ validates :tax, tcvkn: true
5
+ end
6
+
7
+ describe 'tax number format' do
8
+
9
+ let(:invalid_tcvkn) { %w(1 3 434542345678 43454234567) }
10
+ let(:valid_tcvkn) { %w(2640020928 6120069217) }
11
+
12
+ it 'should be invalid' do
13
+ invalid_tcvkn.each { |tax| CompanyDetail.new(tax: tax).valid?.should be_false }
14
+ end
15
+
16
+ it 'should be valid' do
17
+ valid_tcvkn.each { |tax| CompanyDetail.new(tax: tax).valid?.should be_true }
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kangal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - lab2023
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-08 00:00:00.000000000 Z
12
+ date: 2014-01-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -105,15 +105,21 @@ files:
105
105
  - lib/kangal/email.rb
106
106
  - lib/kangal/identity_number.rb
107
107
  - lib/kangal/ip.rb
108
+ - lib/kangal/phone.rb
108
109
  - lib/kangal/subdomain.rb
109
110
  - lib/kangal/tax_number.rb
111
+ - lib/kangal/tckn.rb
112
+ - lib/kangal/tcvkn.rb
110
113
  - lib/kangal/version.rb
111
114
  - spec/email_spec.rb
112
115
  - spec/identity_number_spec.rb
113
116
  - spec/ip_spec.rb
117
+ - spec/phone_spec.rb
114
118
  - spec/spec_helper.rb
115
119
  - spec/subdomain_spec.rb
116
120
  - spec/tax_number_spec.rb
121
+ - spec/tckn_spec.rb
122
+ - spec/tcvkn_spec.rb
117
123
  homepage: https://github.com/kebab-project/kangal
118
124
  licenses:
119
125
  - MIT