clean_room 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/clean_room.gemspec CHANGED
@@ -13,5 +13,8 @@ Gem::Specification.new do |gem|
13
13
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
14
  gem.name = "clean_room"
15
15
  gem.require_paths = ["lib"]
16
+
17
+ gem.add_runtime_dependency "activesupport", ">= 3.0.0"
18
+
16
19
  gem.version = CleanRoom::VERSION
17
20
  end
@@ -1,3 +1,3 @@
1
1
  module CleanRoom
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/clean_room.rb CHANGED
@@ -1,5 +1,83 @@
1
+ require "active_support"
2
+
1
3
  require "clean_room/version"
2
4
 
3
5
  module CleanRoom
4
- # Your code goes here...
6
+ module DSL
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ self.sanitizable_attributes = {}
11
+
12
+ if respond_to? :before_save
13
+ before_save :sanitize_attributes
14
+ end
15
+ end
16
+
17
+ def sanitize_attributes
18
+ self.class.sanitizable_attributes.each do |name, options|
19
+ sanitize_attribute name, options
20
+ end
21
+ end
22
+
23
+ def sanitize_attribute(name, options = {})
24
+ current_value = self.send(name)
25
+ cleaned_value = air_lock.shower(current_value, options)
26
+
27
+ raise Exceptions::Contaminated.new("#{name} contained unacceptable data") if options[:raise] && (current_value != cleaned_value)
28
+ self.send("#{name}=".to_sym, cleaned_value)
29
+ end
30
+
31
+ def air_lock
32
+ @air_lock ||= AirLock.new
33
+ end
34
+
35
+ module ClassMethods
36
+ attr_accessor :sanitizable_attributes
37
+
38
+ def sanitize_attribute(name, options = {})
39
+ name = name.to_sym
40
+ if instance_methods.include?(name) && instance_methods.include?("#{name}=".to_sym)
41
+ sanitizable_attributes[name] = options
42
+ else
43
+ raise Exceptions::InvalidAttribute.new("#{self.class} does not respond to '#{name}(=)'")
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ module Exceptions
50
+ class Contaminated < StandardError
51
+ end
52
+ class InvalidAttribute < StandardError
53
+ end
54
+ end
55
+
56
+ class AirLock
57
+
58
+ def shower(value, options)
59
+ if value
60
+ allow = options[:allow] || :plain_text
61
+
62
+ cleaned_value = case allow
63
+ when :html
64
+
65
+ when :simple_html
66
+
67
+ else
68
+ value.gsub(/[^a-zA-Z0-9 ]/, "")
69
+ end
70
+ else
71
+ nil
72
+ end
73
+ end
74
+
75
+ def sanitize(text)
76
+ #sanitizer.sanitize(text)
77
+ end
78
+
79
+ def sanitizer
80
+ #@sanitizer ||= HTML::WhiteListSanitizer.new
81
+ end
82
+ end
5
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clean_room
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,18 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-06-19 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70341193917880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70341193917880
14
25
  description: Work in progress, this will be a generic attribute sanitizer which can
15
26
  be used for sanitizing models and other objects holding data
16
27
  email: