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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cold_shoulder.rb +51 -2
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ddd8d0285df62d170d18964bd8e0480c56ce18b4
4
- data.tar.gz: 44d0e9999ffee09092adb8fa2eaac599f67763e9
3
+ metadata.gz: efab1138b6ecea26248018be6a3305c651baf921
4
+ data.tar.gz: e0ff443163907137a03f601bfab640437cb00158
5
5
  SHA512:
6
- metadata.gz: 194d6b6d00e11cb697c97999620595e213f865b0e98f7913d1d59fa03038a7ac9df3295b5620976a6f1edc7d743d6e078d328ba790481b6a9974f164d162a99a
7
- data.tar.gz: a52ab07840f5ade96ceec5b5fce93cbb2bc908055f1230af1e653e4e387769b95208b2e14faa6304d57639d9ddbbb60fdb78746a6020c4de158980b434180c11
6
+ metadata.gz: 683e992fcb063d1ca72c615b2deb0c40509f32e293914052e2b8deab14f4ead981ae538db7a17a3b442ab9805d2533bd0de512891c3ab5bd935c32a6e9992968
7
+ data.tar.gz: 48478725e4be4bb00a9a23db244ef4ef433ef32b75de0f91bf13523d8adb52585ae830bb68a5eb23bfa87a9f16ff0a99939c927f1d2666adee1da95ada85a1c9
data/lib/cold_shoulder.rb CHANGED
@@ -1,4 +1,53 @@
1
- require 'active_model/validations/cold_shoulder_validator'
1
+ require 'active_model'
2
+ require 'active_model/validations'
2
3
 
3
- module ColdShoulder
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
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.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Hoffman