cold_shoulder 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cold_shoulder.rb +47 -13
  3. metadata +15 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efab1138b6ecea26248018be6a3305c651baf921
4
- data.tar.gz: e0ff443163907137a03f601bfab640437cb00158
3
+ metadata.gz: 0cbec9bba312d384496203bbeb9946d896eafd07
4
+ data.tar.gz: 9b7b35ac23b8c35ed887d9243158bfee7853abeb
5
5
  SHA512:
6
- metadata.gz: 683e992fcb063d1ca72c615b2deb0c40509f32e293914052e2b8deab14f4ead981ae538db7a17a3b442ab9805d2533bd0de512891c3ab5bd935c32a6e9992968
7
- data.tar.gz: 48478725e4be4bb00a9a23db244ef4ef433ef32b75de0f91bf13523d8adb52585ae830bb68a5eb23bfa87a9f16ff0a99939c927f1d2666adee1da95ada85a1c9
6
+ metadata.gz: 5fd912a4e3a1c5f828f2c3646fbaf6315c27465ee19bb8562ae162d1f9890fe538e4dab8729cdec292f5cf702e33894c75dda2c9df527080fec18db10424c221
7
+ data.tar.gz: bbd5360fb97cf23eb6695a0d64a415d4f48f13e671801633a9d983eaa00a4c91c378c9fd1d557aceff6a437bf1b7c25befa1e50ed2690707e0534431e2239620
data/lib/cold_shoulder.rb CHANGED
@@ -1,37 +1,71 @@
1
1
  require 'active_model'
2
2
  require 'active_model/validations'
3
+ require 'active_support'
4
+ require 'active_support/number_helper'
3
5
 
6
+ module ColdShoulder
7
+ if defined?(Rails)
8
+ class Railtie < Rails::Railtie
9
+ config.before_configuration do
10
+ I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ # The validations
17
+ # TODO require in another file? This was failing when actually deployed through rubygems
4
18
  module ActiveModel
5
19
  module Validations
6
20
 
7
21
  # Extend the rails each validator so that this can be used like any Rails validator
8
22
  class ColdShoulderValidator < EachValidator
23
+ include ActiveSupport::NumberHelper if defined?(Rails)
24
+
9
25
  def validate_each(record, attr_name, value)
10
26
 
11
27
  # These are somewhat simplistic
12
28
  # Main problem being that all of them can be sidestepped by including commas
13
29
  # TODO: Move this outside the validate_each method so we don't define it over and over
14
- twitter_regex = /@([A-Za-z0-9_]{1,15})/i
15
- formatted_phone_regex = /(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b/i
16
- email_regex = /.+(\@|a\s*t).+\..+/i
30
+ twitter_regex = /(@[A-Za-z0-9_]{1,15})/i
31
+ formatted_phone_regex = /((?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b)/i
32
+ email_regex = /(\b[^\s]+?\s*(@|at)\s*[^\s]+?\.[^\s]+?\b)/i # Very general so as to catch BS
17
33
 
34
+ # Remove spaces (the most basic way to avoid these detections)
18
35
  globbed_value = value.gsub ' ', ''
19
36
  bullshit_free_phone = value.gsub /[^0-9,]|\n/i, ''
20
37
 
21
- # Twitter handles
22
- if twitter_regex.match(globbed_value) && !options[:ignore_twitter]
23
- record.errors.add(attr_name, :contains_twitter_handle, options)
24
- end
38
+ # Look for matches
39
+ twitter_handles = globbed_value.scan twitter_regex
40
+ email_addresses = value.scan email_regex
41
+ phone_numbers = globbed_value.scan(formatted_phone_regex).concat(
42
+ bullshit_free_phone.scan(formatted_phone_regex)
43
+ ).uniq
25
44
 
26
- # Phone numbers with bullshit
27
- if (formatted_phone_regex.match(globbed_value) && !options[:ignore_phone]) or
28
- (formatted_phone_regex.match(bullshit_free_phone) && !options[:ignore_phone])
29
- record.errors.add(attr_name, :contains_phone_number, options)
45
+ # Phone numbers
46
+ unless phone_numbers.empty? or options[:ignore_phone]
47
+ record.errors.add(attr_name, :contains_phone_number, options.merge(
48
+ phone_numbers: phone_numbers.map{ |p|
49
+ defined?(Rails) ? number_to_phone(p[0]) : p[0]
50
+ }.join(', ')
51
+ ))
30
52
  end
31
53
 
32
54
  # Email addys
33
- if email_regex.match(value) && !options[:ignore_email]
34
- record.errors.add(attr_name, :contains_email_address)
55
+ unless email_addresses.empty? or options[:ignore_email]
56
+ record.errors.add(attr_name, :contains_email_address, options.merge(
57
+ email_addresses: email_addresses.map{|p| p[0] }.join(', ')
58
+ ))
59
+ else
60
+
61
+ # Twitter handles
62
+ # Any email address is going to register twitter handles as well
63
+ unless twitter_handles.empty? or options[:ignore_twitter]
64
+ record.errors.add(attr_name, :contains_twitter_handle, options.merge(
65
+ handles: twitter_handles.map{|p| p[0] }.join(', ')
66
+ ))
67
+ end
68
+
35
69
  end
36
70
  end
37
71
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cold_shoulder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Hoffman
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement