cold_shoulder 1.1.0 → 1.2.0

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: a119cbf79420849fd9744b5604596fa10cc2e2e8
4
- data.tar.gz: 7dd5ea4211aa6aaa3b2cf9342d480cb259ecdad6
3
+ metadata.gz: 6b5c1034d690bd1bf262b7d491ff281e44b9d714
4
+ data.tar.gz: 655709b9dc3d1678480f920784312fffac10a1ac
5
5
  SHA512:
6
- metadata.gz: 860f36380a17265e4aba35ee08fd06b9f2e22921b45ab8fdbb62f4b39964ce38c8e453d75b85014f8aae1e337dabe08313b4e2d6c55cbc8cbb36e27c6bf291f4
7
- data.tar.gz: 01d83e4f04c5872d2b14e4e33e86174e3ad92529429d7a204e2924f52ab1311219257d07d410da057e3a65dd90589ec216457512b9be4cce797cf0ec27fcb0cc
6
+ metadata.gz: f5648f2978c4ac0a1eae514fc2d14d409a9897b0b1ba3b21869ec76bdc32000a8ad3004b7aa5523e8b03ef5609a92470b3c9af7f4aad97ce9790e8116e67a46d
7
+ data.tar.gz: 8aed809493caa45f827128278e383af6435201f75aaf202b706fc98d6da38824ee41fbce6160d11897c4ccfbd3874556139df23eba7d3e049fb3403a1f208209
data/lib/cold_shoulder.rb CHANGED
@@ -1,8 +1,4 @@
1
- require 'active_model'
2
- require 'active_model/validations'
3
-
4
- require 'action_view'
5
- require 'action_view/helpers'
1
+ require 'validator'
6
2
 
7
3
  module ColdShoulder
8
4
  if defined?(Rails)
@@ -12,70 +8,4 @@ module ColdShoulder
12
8
  end
13
9
  end
14
10
  end
15
- end
16
-
17
- # The validations
18
- # TODO require in another file? This was failing when actually deployed through rubygems
19
- module ActiveModel
20
- module Validations
21
-
22
- # Extend the rails each validator so that this can be used like any Rails validator
23
- class ColdShoulderValidator < EachValidator
24
- include ActionView::Helpers::NumberHelper if defined?(Rails)
25
-
26
- def validate_each(record, attr_name, value)
27
-
28
- # These are somewhat simplistic
29
- # Main problem being that all of them can be sidestepped by including commas
30
- # TODO: Move this outside the validate_each method so we don't define it over and over
31
- twitter_regex = /(@[A-Za-z0-9_]{1,15})/i
32
- formatted_phone_regex = /((?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b)/i
33
- email_regex = /(\b[^\s]+?\s*(@|at)\s*[^\s]+?\.[^\s]+?\b)/i # Very general so as to catch BS
34
-
35
- # Remove spaces (the most basic way to avoid these detections)
36
- globbed_value = value.gsub ' ', ''
37
- bullshit_free_phone = value.gsub /[^0-9,]|\n/i, ''
38
-
39
- # Look for matches
40
- detected = {
41
- twitter: globbed_value.scan(twitter_regex),
42
- email: value.scan(email_regex),
43
- phone: globbed_value.scan(formatted_phone_regex).concat(
44
- bullshit_free_phone.scan(formatted_phone_regex)
45
- ).uniq
46
- }
47
-
48
- # Catchall for base
49
- errors = false
50
-
51
- [:twitter, :email, :phone].each do |type|
52
-
53
- unless detected[type].empty? or options["ignore_#{type}".to_sym]
54
- errors = true
55
- next if options[:message]
56
-
57
- record.errors.add(attr_name, "contains_#{type}".to_sym, options.merge(
58
- detected: detected[type].map{ |p|
59
- type == :phone && defined?(Rails) ? number_to_phone(p[0], area_code: true) : p[0]
60
- }.join(', ')
61
- ))
62
- end
63
-
64
- end
65
-
66
- # A base mesage might be used
67
- if errors and options[:message]
68
- record.errors.add(:base, options[:message])
69
- end
70
-
71
- end
72
-
73
- module HelperMethods
74
- def validates_with_cold_shouldr(*attr_names)
75
- validates_with ColdShoulderValidator, _merge_attributes(attr_names)
76
- end
77
- end
78
-
79
- end
80
- end
81
11
  end
data/lib/validator.rb ADDED
@@ -0,0 +1,102 @@
1
+ # For monkeypatch
2
+ require 'active_model'
3
+ require 'active_model/validations'
4
+
5
+ # For formatting phone numbers in errors
6
+ require 'action_view'
7
+ require 'action_view/helpers'
8
+
9
+ # The validations
10
+ # TODO require in another file? This was failing when actually deployed through rubygems
11
+ module ActiveModel
12
+ module Validations
13
+
14
+ # Extend the rails each validator so that this can be used like any Rails validator
15
+ class ColdShoulderValidator < EachValidator
16
+ include ActionView::Helpers::NumberHelper if defined?(Rails)
17
+
18
+ # These are somewhat simplistic
19
+ # Main problem being that all of them can be sidestepped by including commas
20
+ TWITTER_REGEX = /(@[A-Za-z0-9_]{1,15})/i
21
+ PHONE_REGEX = /((?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b)/i
22
+ EMAIL_REGEX = /(\b[^\s]+?\s*(@|at)\s*[^\s]+?\.[^\s]+?\b)/i
23
+ URL_REGEX = /\b(\S+?\.\S+?)\b/i
24
+
25
+ def initialize(options)
26
+ options[:ignore_link] = true if options[:remove_links]
27
+
28
+ super
29
+ end
30
+
31
+ def validate_each(record, attr_name, value)
32
+
33
+ # Detect any contact methods in the value
34
+ detected = detect_contacts_in value
35
+
36
+ # Add errors for detected methods, If there is a message, skip adding errors
37
+ errors = add_errors_to_record(record, attr_name, detected, {skip: options[:message]})
38
+
39
+ # A base mesage might be used
40
+ if errors and options[:message]
41
+ record.errors.add(:base, options[:message])
42
+ end
43
+
44
+ # Strip URLs from the actual saved value
45
+ if options[:remove_links]
46
+ record[attr_name] = value.gsub URL_REGEX, ''
47
+ end
48
+ end
49
+
50
+ def detect_contacts_in value
51
+ # Remove spaces (the most basic way to avoid these detections)
52
+ globbed_value = value.gsub ' ', ''
53
+ # Remove any characters not numbers or commas (commas are for $1,000 formatted numbers
54
+ bullshit_free_phone = value.gsub /[^0-9,]|\n/i, ''
55
+
56
+ # Look for matches
57
+ detected = {
58
+ twitter: globbed_value.scan(TWITTER_REGEX),
59
+ email: value.scan(EMAIL_REGEX),
60
+ phone: globbed_value.scan(PHONE_REGEX).concat(
61
+ bullshit_free_phone.scan(PHONE_REGEX)
62
+ ).uniq,
63
+ link: value.scan(URL_REGEX)
64
+ }
65
+ end
66
+
67
+ # Returns true or fase, if there were any errors at all
68
+ # ops[:skip] can be used to skip adding the actual errors to the record
69
+ # If we just want the boolean return value
70
+ def add_errors_to_record record, attr_name, detected, ops={}
71
+ # Catchall for base
72
+ errors = false
73
+
74
+ # Search for
75
+ [:twitter, :email, :phone, :link].each do |type|
76
+
77
+ # Were there any matches, or are we ignoring this type
78
+ unless detected[type].empty? or options["ignore_#{type}".to_sym]
79
+ errors = true
80
+ next if ops[:skip] # If a base message is set, don't add attr errors
81
+
82
+ record.errors.add(attr_name, "contains_#{type}".to_sym, options.merge(
83
+ detected: detected[type].map{ |p|
84
+ # If this is a phone it gets formatted, otherwise no
85
+ type == :phone && defined?(Rails) ? number_to_phone(p[0], area_code: true) : p[0]
86
+ }.join(', ')
87
+ ))
88
+ end
89
+
90
+ end
91
+ return errors
92
+ end
93
+
94
+ module HelperMethods
95
+ def validates_with_cold_shouldr(*attr_names)
96
+ validates_with ColdShoulderValidator, _merge_attributes(attr_names)
97
+ end
98
+ end
99
+
100
+ end
101
+ end
102
+ end
data/locales/en.yml CHANGED
@@ -3,4 +3,5 @@ en:
3
3
  messages:
4
4
  contains_twitter: "contains the twitter handle: %{detected}"
5
5
  contains_phone: "contains the phone number: %{detected}"
6
- contains_email: "contains the email address: %{detected}"
6
+ contains_email: "contains the email address: %{detected}"
7
+ contains_link: "contains the link: %{detected}"
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Hoffman
@@ -61,6 +61,7 @@ extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
63
  - lib/cold_shoulder.rb
64
+ - lib/validator.rb
64
65
  - locales/en.yml
65
66
  homepage: https://github.com/Breefield/cold_shoulder
66
67
  licenses: