roomorama-validates_email 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 896b72ce22bae5c9c9c5d594f8db80c2052e2fcf
4
+ data.tar.gz: aca05fbd0ba5b8b65d553277da91c0e823a465b4
5
+ SHA512:
6
+ metadata.gz: a530b00be582bbb125255bb0ac3457cbc416f8f2220c6eb4bc08cbf2809df08b90c6fe2430da7af902acedfbee52d158a5109ad72be552f4eb671a52d8e87543
7
+ data.tar.gz: d4af74a0306f0cb9f1d7a4c98964a16d80d995da1f4da6a7f489e9655f77d63526d7ba8ed631bfdcaf5735dcd4060ca8cc454749989cc7c74f2a95c0e2a12c55
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # Email validation class which uses Rails 3 ActiveModel
2
+ # validation mechanism.
3
+ #
4
+ class EmailValidator < ActiveModel::EachValidator
5
+ class Encoding
6
+ class CompatibilityError < StandardError; end
7
+ end if RUBY_VERSION.to_f < 1.9
8
+
9
+ LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
10
+ LocalPartUnquoted = '([a-z0-9' + LocalPartSpecialChars + '])?([a-z0-9]+([' + LocalPartSpecialChars + '\.\+])?)*[a-z0-9-_]'
11
+ LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\x00-\xFF]))*)\"'
12
+ Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+[^_])|(\w+\.[^_]))*([a-z0-9-]{1,63})\.[a-z]{2,6}(?:\.[a-z]{2,6})?$)', Regexp::EXTENDED | Regexp::IGNORECASE, 'n')
13
+
14
+ # Validates email address.
15
+ # If MX fallback was also requested, it will check if email is valid
16
+ # first, and only after that will go to MX fallback.
17
+ #
18
+ # @example
19
+ # class User < ActiveRecord::Base
20
+ # validates :primary_email, :email => { :mx => { :a_fallback => true } }
21
+ # end
22
+ #
23
+ def validate_each(record, attribute, value)
24
+ if validates_email_format(value)
25
+ if options[:mailgun] && ENV['MAILGUN_PUBLIC_KEY'].present? && !validates_email_with_mailgun(value)
26
+ record.errors[attribute] << (options[:mailgun_message] || I18n.t(:invalid, :scope => [:activerecord, :errors, :messages]))
27
+ end
28
+ if options[:mx] && !validates_email_domain(value, options[:mx])
29
+ record.errors[attribute] << (options[:mx_message] || I18n.t(:mx_invalid, :scope => [:activerecord, :errors, :messages]))
30
+ end
31
+ else
32
+ record.errors[attribute] << (options[:message] || I18n.t(:invalid, :scope => [:activerecord, :errors, :messages]))
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ # Checks email if it's valid by rules defined in `Regex`.
39
+ #
40
+ # @param [String] A string with email. Local part of email is max 64 chars,
41
+ # domain part is max 255 chars.
42
+ #
43
+ # @return [Boolean] True or false.
44
+ #
45
+ def validates_email_format(email)
46
+ # TODO: should this decode escaped entities before counting?
47
+ begin
48
+ local, domain = email.split('@', 2)
49
+ rescue NoMethodError
50
+ return false
51
+ end
52
+
53
+ begin
54
+ email =~ Regex and not email =~ /\.\./ and domain.length <= 255 and local.length <= 64
55
+ rescue Encoding::CompatibilityError
56
+ # RFC 2822 and RFC 3696 don't support UTF-8 characters in email address,
57
+ # so Regexp is in ASCII-8BIT encoding, which raises this exception when
58
+ # you try email address with unicode characters in it.
59
+ return false
60
+ end
61
+ end
62
+
63
+ # Checks email is its domain is valid. Fallbacks to A record if requested.
64
+ #
65
+ # @param [String] A string with email.
66
+ # @param [Hash] A hash of options, which tells whether to use A fallback or
67
+ # or not. Additional options can be also passed.
68
+ #
69
+ # @return [Integer, nil] In general, it's true or false.
70
+ #
71
+ def validates_email_domain(email, options)
72
+ require 'resolv'
73
+ a_fallback = options.is_a?(Hash) ? options[:a_fallback] : false
74
+ domain = email.match(/\@(.+)/)[1]
75
+ Resolv::DNS.open do |dns|
76
+ @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
77
+ @mx.push(*dns.getresources(domain, Resolv::DNS::Resource::IN::A)) if a_fallback
78
+ end
79
+ @mx.size > 0 ? true : false
80
+ end
81
+
82
+ def validates_email_with_mailgun(email)
83
+ require 'rest_client'
84
+ res = RestClient.get "https://api:#{ENV['MAILGUN_PUBLIC_KEY']}@api.mailgun.net/v2/address/validate", {params: {address: value}}
85
+ parsed = JSON.parse(res)
86
+ is_valid = !parsed["is_valid"].nil? ? parsed["is_valid"] : false
87
+ is_valid
88
+ end
89
+
90
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesEmail
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1 @@
1
+ require "validates_email/email_validator"
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roomorama-validates_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Donald Piret
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Rails plugin to validate email addresses against RFC 2822 and RFC 3696
28
+ email:
29
+ - donald@roomorama.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - lib/validates_email.rb
36
+ - lib/validates_email/email_validator.rb
37
+ - lib/validates_email/version.rb
38
+ homepage: http://github.com/roomorama/validates_email
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.6
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Rails plugin to validate email addresses
61
+ test_files: []
62
+ has_rdoc: