sanitized_attributes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 CrowdCompass, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = sanitized_attributes
2
+
3
+ A wrapper for the sanitize gem which provides automatic sanitization of attributes.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 CrowdCompass, Inc.. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sanitized_attributes"
8
+ gem.summary = %Q{HTML-sanitizing attribute accessors for Ruby and Rails}
9
+ gem.description = %Q{A wrapper to make automatic sanitization of incoming data easier. Uses the sanitize gem and works in both plain Ruby and Rails projects.}
10
+ gem.email = "engineering@crowdcompass.com"
11
+ gem.homepage = "http://github.com/mboeh/sanitized_attributes"
12
+ gem.authors = ["CrowdCompass, Inc."]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "sanitize", "> 0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ spec.rcov_opts = ['--exclude', File.expand_path("~/.rvm"), "--exclude", "spec"]
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "sanitized_attributes #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,40 @@
1
+ module SanitizedAttributes; class SanitizedAttribute
2
+
3
+ def initialize(attr_name, options = {})
4
+ @attr_name = attr_name
5
+ @options = options
6
+ end
7
+
8
+ def sanitize(content)
9
+ Sanitize.clean(content, sanitize_config)
10
+ end
11
+
12
+ def define_writer_method(klass)
13
+ this = self
14
+ attr_name = @attr_name
15
+ klass.send(:define_method, "#{@attr_name}_with_sanitization=") {|value|
16
+ send("#{attr_name}_without_sanitization=", this.sanitize(value))
17
+ }
18
+ end
19
+
20
+ protected
21
+
22
+ def sanitize_config
23
+ SanitizedAttributes.sanitize_options(@options)
24
+ end
25
+
26
+ class << self
27
+
28
+ def add(klass, attr_name, options = {})
29
+ attrib = new(attr_name, options)
30
+ attrib.define_writer_method(klass)
31
+ if klass.respond_to?(:alias_method_chain)
32
+ klass.send(:alias_method_chain, "#{attr_name}=", :sanitization)
33
+ else
34
+ klass.send(:alias_method, "#{attr_name}_without_sanitization=", "#{attr_name}=")
35
+ klass.send(:alias_method, "#{attr_name}=", "#{attr_name}_with_sanitization=")
36
+ end
37
+ end
38
+
39
+ end
40
+ end; end
@@ -0,0 +1,86 @@
1
+ require 'rubygems'
2
+ require 'sanitize'
3
+ require 'sanitized_attributes/sanitized_attribute'
4
+
5
+ module SanitizedAttributes
6
+
7
+ def self.included(into)
8
+ into.extend(ClassMethods)
9
+ end
10
+
11
+ class << self
12
+
13
+ def add_option(name, &blk)
14
+ @option_transforms = nil
15
+ @options ||= {}
16
+ @options[name] = blk
17
+ end
18
+
19
+ def add_profile(name, options = {})
20
+ @profiles ||= {}
21
+ @profiles[name] = options
22
+ end
23
+
24
+ def profile(name)
25
+ @profiles ||= {}
26
+ @profiles[name] || {}
27
+ end
28
+
29
+ def sanitize_options(options)
30
+ pr =
31
+ if options.kind_of?(Symbol)
32
+ profile(options)
33
+ else
34
+ options
35
+ end
36
+ o = merge_options(default_profile, pr)
37
+ o
38
+ end
39
+
40
+ protected
41
+
42
+ def default_profile
43
+ merge_options(profile(:default), obligatory_options)
44
+ end
45
+
46
+ def merge_options(ops, new_ops)
47
+ final_ops = ops.dup
48
+ new_ops.each do |key,val|
49
+ old = final_ops[key]
50
+ if key == :transformers
51
+ final_ops[key] ||= []
52
+ final_ops[key] = ([old] + [val]).flatten.uniq.compact
53
+ else
54
+ final_ops[key] = val
55
+ end
56
+ final_ops.delete(key) if final_ops[key].nil?
57
+ end
58
+ return final_ops
59
+ end
60
+
61
+ def obligatory_options
62
+ { :transformers => option_transforms }
63
+ end
64
+
65
+ def option_transforms
66
+ @option_transforms ||=
67
+ begin
68
+ @options.map do |name, tproc|
69
+ lambda do |env|
70
+ tproc.call(env, env[:config][name]) if env[:config][name]
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+
78
+ module ClassMethods
79
+
80
+ def sanitize_attribute(attr_name, options = {})
81
+ SanitizedAttribute.add(self, attr_name, options)
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SanitizedAttributes" do
4
+
5
+ before do
6
+ @klass = Class.new do
7
+ include SanitizedAttributes
8
+ attr_accessor :orz
9
+ attr_accessor :vux
10
+ end
11
+ SanitizedAttributes.add_option(:no_empties) do |env, forbidden_empties|
12
+ if env[:node].content.empty?
13
+ if forbidden_empties.include?(env[:node_name])
14
+ {:node => Nokogiri::XML::Text.new("", env[:node].document)}
15
+ end
16
+ end
17
+ end
18
+ SanitizedAttributes.add_profile(:quotes_only, :elements => %w[blockquote])
19
+ end
20
+
21
+ it "does stuff" do
22
+ @klass.module_eval do
23
+ sanitize_attribute :orz, :elements => %w[p], :no_empties => %w[p]
24
+ sanitize_attribute :vux, :quotes_only
25
+ end
26
+ obj = @klass.new
27
+ obj.vux = "<blockquote>Our special today is <b>particle fragmentation!</b></blockquote> - VUX"
28
+ obj.vux.should == "<blockquote>Our special today is particle fragmentation!</blockquote> - VUX"
29
+ obj.orz = "Orz are not *many bubbles* like <p/>*campers*. <p></p>Orz <b>are just</b> Orz. <p>- Orz</p>"
30
+ obj.orz.should == "Orz are not *many bubbles* like *campers*. Orz are just Orz. <p>- Orz</p>"
31
+ end
32
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'sanitized_attributes'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sanitized_attributes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - CrowdCompass, Inc.
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-11 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: sanitize
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">"
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ description: A wrapper to make automatic sanitization of incoming data easier. Uses the sanitize gem and works in both plain Ruby and Rails projects.
52
+ email: engineering@crowdcompass.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.rdoc
65
+ - Rakefile
66
+ - VERSION
67
+ - lib/sanitized_attributes.rb
68
+ - lib/sanitized_attributes/sanitized_attribute.rb
69
+ - spec/sanitized_attributes_spec.rb
70
+ - spec/spec.opts
71
+ - spec/spec_helper.rb
72
+ has_rdoc: true
73
+ homepage: http://github.com/mboeh/sanitized_attributes
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: HTML-sanitizing attribute accessors for Ruby and Rails
106
+ test_files:
107
+ - spec/spec_helper.rb
108
+ - spec/sanitized_attributes_spec.rb