cold_shoulder 1.0.1 → 1.0.2
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 +4 -4
- data/lib/cold_shoulder.rb +51 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efab1138b6ecea26248018be6a3305c651baf921
|
4
|
+
data.tar.gz: e0ff443163907137a03f601bfab640437cb00158
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 683e992fcb063d1ca72c615b2deb0c40509f32e293914052e2b8deab14f4ead981ae538db7a17a3b442ab9805d2533bd0de512891c3ab5bd935c32a6e9992968
|
7
|
+
data.tar.gz: 48478725e4be4bb00a9a23db244ef4ef433ef32b75de0f91bf13523d8adb52585ae830bb68a5eb23bfa87a9f16ff0a99939c927f1d2666adee1da95ada85a1c9
|
data/lib/cold_shoulder.rb
CHANGED
@@ -1,4 +1,53 @@
|
|
1
|
-
require 'active_model
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_model/validations'
|
2
3
|
|
3
|
-
module
|
4
|
+
module ActiveModel
|
5
|
+
module Validations
|
6
|
+
|
7
|
+
# Extend the rails each validator so that this can be used like any Rails validator
|
8
|
+
class ColdShoulderValidator < EachValidator
|
9
|
+
def validate_each(record, attr_name, value)
|
10
|
+
|
11
|
+
# These are somewhat simplistic
|
12
|
+
# Main problem being that all of them can be sidestepped by including commas
|
13
|
+
# 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
|
17
|
+
|
18
|
+
globbed_value = value.gsub ' ', ''
|
19
|
+
bullshit_free_phone = value.gsub /[^0-9,]|\n/i, ''
|
20
|
+
|
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
|
25
|
+
|
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)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Email addys
|
33
|
+
if email_regex.match(value) && !options[:ignore_email]
|
34
|
+
record.errors.add(attr_name, :contains_email_address)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module HelperMethods
|
39
|
+
# Validates that the specified attributes do not contain contact information.
|
40
|
+
# Happens by default on save.
|
41
|
+
#
|
42
|
+
# class Message < ActiveRecord::Base
|
43
|
+
# validates_no_contact_in :body
|
44
|
+
# end
|
45
|
+
|
46
|
+
def validates_with_cold_shouldr(*attr_names)
|
47
|
+
validates_with ColdShoulderValidator, _merge_attributes(attr_names)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
4
53
|
end
|