email_address 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2dd7c65f4932e46e3a03d92c5c28d23862bb679
4
- data.tar.gz: cd81d66ddb341143ac160351d99623823e2d1e20
3
+ metadata.gz: 865ef9fea556b3de09bffbfc977f751ef806da92
4
+ data.tar.gz: c647502c488d49962b962090c5b3623bc89c774c
5
5
  SHA512:
6
- metadata.gz: fcd8720fd24322f9b8a0f9959ef308e8281a5f2a5969d3b4679639dc99e769fa95b90a6d376e03edd6bc8812f5c27bdefb9595a4315bfcd84718a90a2573ec51
7
- data.tar.gz: 17aeaf30ccd3ce8ca29b17f129821960e60148b10d91d3d33778e67cb554947f107961610c81c4cb05c0f38d12c67c50eca32a83d09d576da61e175653bac4be
6
+ metadata.gz: 85c1508f8e3e73960157d00306db1d0d692e7e9f33db00d8a7ebce75bf1ce56626b670e5fb0f322b54fe78154d79815cefa0350d2f2187c049c13ede4341a828
7
+ data.tar.gz: 6811f8ec8f1ca8e5253df4cbc58e90d4e6e27ff35014ce56ac59bc70d9a4ed1044d3f09ad0ce3a451238e7328c7eb2721d36840d5f6197616e38bda63c53b2ff
@@ -217,7 +217,7 @@ module EmailAddress
217
217
  return set_error :invalid_mailbox
218
218
  end
219
219
  unless self.host.valid?
220
- return set_error :invalid_host
220
+ return set_error self.host.error_message
221
221
  end
222
222
  if @config[:address_size] && !@config[:address_size].include?(self.to_s.size)
223
223
  return set_error :exceeds_size
@@ -237,7 +237,7 @@ module EmailAddress
237
237
  end
238
238
 
239
239
  def set_error(err)
240
- @error = EmailAddress::Config.error_messages[err] || err
240
+ @error = EmailAddress::Config.error_messages.fetch(err) { err }
241
241
  false
242
242
  end
243
243
 
@@ -93,7 +93,7 @@ module EmailAddress
93
93
  munge_string: "*****",
94
94
 
95
95
  local_downcase: true,
96
- local_fix: true,
96
+ local_fix: false,
97
97
  local_encoding: :ascii, # :ascii, :unicode,
98
98
  local_parse: nil, # nil, Proc
99
99
  local_format: :conventional, # :conventional, :relaxed, :redacted, :standard, Proc
@@ -33,7 +33,7 @@ module EmailAddress
33
33
  attr_reader :host_name
34
34
  attr_accessor :dns_name, :domain_name, :registration_name,
35
35
  :tld, :tld2, :subdomains, :ip_address, :config, :provider,
36
- :comment
36
+ :comment, :error_message
37
37
  MAX_HOST_LENGTH = 255
38
38
 
39
39
  # Sometimes, you just need a Regexp...
@@ -313,11 +313,6 @@ module EmailAddress
313
313
  EmailAddress::Config.setting(:dns_lookup).equal?(:off) ? false : true
314
314
  end
315
315
 
316
- # True if the host name has a DNS A Record
317
- def has_dns_a_record?
318
- dns_a_record.size > 0 ? true : false
319
- end
320
-
321
316
  # Returns: [official_hostname, alias_hostnames, address_family, *address_list]
322
317
  def dns_a_record
323
318
  @_dns_a_record ||= Socket.gethostbyname(self.dns_name)
@@ -366,16 +361,49 @@ module EmailAddress
366
361
 
367
362
  # Returns true if the host name is valid according to the current configuration
368
363
  def valid?(rule=@config[:dns_lookup]||:mx)
364
+ self.error_message = nil
369
365
  if self.ip_address
370
- @config[:host_allow_ip] && self.valid_ip?
366
+ valid_ip?
367
+ elsif ! valid_format?
368
+ false
371
369
  elsif rule == :mx
372
- self.exchangers.mx_ips.size > 0
370
+ valid_mx?
373
371
  elsif rule == :a
374
- self.has_dns_a_record?
372
+ valid_dns?
375
373
  elsif rule == :off
376
- self.to_s.size <= MAX_HOST_LENGTH
374
+ true
377
375
  else
378
- false
376
+ set_error(:domain_bad_validation_rule)
377
+ end
378
+ end
379
+
380
+ # The inverse of valid? -- Returns nil (falsey) if valid, otherwise error message
381
+ def error
382
+ valid? ? nil : @error_message
383
+ end
384
+
385
+ # True if the host name has a DNS A Record
386
+ def valid_dns?
387
+ dns_a_record.size > 0 || set_error(:domain_unknown)
388
+ end
389
+
390
+ # True if the host name has valid MX servers configured in DNS
391
+ def valid_mx?
392
+ if self.exchangers.mx_ips.size > 0
393
+ true
394
+ elsif valid_dns?
395
+ set_error(:domain_does_not_accept_email)
396
+ else
397
+ set_error(:domain_unknown)
398
+ end
399
+ end
400
+
401
+ # True if the host_name passes Regular Expression match and size limits.
402
+ def valid_format?
403
+ if self.host_name =~ CANONICAL_HOST_REGEX && self.to_s.size <= MAX_HOST_LENGTH
404
+ true
405
+ else
406
+ set_error(:domain_invalid)
379
407
  end
380
408
  end
381
409
 
@@ -383,14 +411,19 @@ module EmailAddress
383
411
  # is a potentially valid IP address. It does not check if the address
384
412
  # is reachable.
385
413
  def valid_ip?
386
- if self.ip_address.nil?
387
- false
414
+ if ! @config[:host_allow_ip]
415
+ set_error(:ip_address_forbidden)
388
416
  elsif self.ip_address.include?(":")
389
- self.ip_address =~ Resolv::IPv6::Regex ? true : false
417
+ self.ip_address =~ Resolv::IPv6::Regex ? true : set_error(:ipv6_address_invalid)
390
418
  elsif self.ip_address.include?(".")
391
- self.ip_address =~ Resolv::IPv4::Regex ? true : false
419
+ self.ip_address =~ Resolv::IPv4::Regex ? true : set_error(:ipv4_address_invalid)
392
420
  end
393
421
  end
394
422
 
423
+ def set_error(err)
424
+ @error_message = EmailAddress::Config.error_messages.fetch(err) { err }
425
+ false
426
+ end
427
+
395
428
  end
396
429
  end
@@ -1,3 +1,3 @@
1
1
  module EmailAddress
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -111,4 +111,15 @@ class TestHost < MiniTest::Test
111
111
  assert ! EmailAddress::Host.new('ya hoo.com').valid?
112
112
  assert EmailAddress::Host.new('ya hoo.com', host_remove_spaces:true).valid?
113
113
  end
114
+
115
+ def test_errors
116
+ assert_equal EmailAddress::Host.new("yahoo.com").error, nil
117
+ assert_equal EmailAddress::Host.new("example.com").error, :domain_does_not_accept_email
118
+ assert_equal EmailAddress::Host.new("yahoo.wtf").error, :domain_unknown
119
+ assert_equal EmailAddress::Host.new("ajsdfhajshdfklasjhd.wtf", dns_lookup: :off).error, nil
120
+ assert_equal EmailAddress::Host.new("ya hoo.com", dns_lookup: :off).error, :domain_invalid
121
+ assert_equal EmailAddress::Host.new("[127.0.0.1]").error, :ip_address_forbidden
122
+ assert_equal EmailAddress::Host.new("[127.0.0.666]", host_allow_ip:true).error, :ipv4_address_invalid
123
+ assert_equal EmailAddress::Host.new("[IPv6::12t]", host_allow_ip:true).error, :ipv6_address_invalid
124
+ end
114
125
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_address
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allen Fair
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-06 00:00:00.000000000 Z
11
+ date: 2017-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake