dolzenko 0.0.22 → 0.0.23
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.
- data/lib/dolzenko/light.rb +1 -0
- data/lib/dolzenko/remote_download.rb +2 -0
- data/lib/dolzenko/safe_interpolate.rb +80 -0
- metadata +4 -3
data/lib/dolzenko/light.rb
CHANGED
@@ -0,0 +1,80 @@
|
|
1
|
+
require "active_support/all"
|
2
|
+
require "active_record"
|
3
|
+
require "cgi"
|
4
|
+
|
5
|
+
module SafeInterpolate
|
6
|
+
def generic_interpolate(string_block, interpolator)
|
7
|
+
string_with_interpolations = string_block.call
|
8
|
+
string_with_interpolations.gsub(/\#\{([^}]*)\}/) do
|
9
|
+
result = eval($1, string_block.binding)
|
10
|
+
interpolator[result]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def sql_interpolate(&string_block)
|
15
|
+
generic_interpolate(string_block, ActiveRecord::Base.connection.method(:quote))
|
16
|
+
end
|
17
|
+
|
18
|
+
def html_interpolate(&string_block)
|
19
|
+
generic_interpolate(string_block, ERB::Util.method(:html_escape))
|
20
|
+
end
|
21
|
+
|
22
|
+
def uri_interpolate(&string_block)
|
23
|
+
generic_interpolate(string_block, CGI.method(:escape))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if $PROGRAM_NAME == __FILE__
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/expectations'
|
30
|
+
require 'rspec/matchers'
|
31
|
+
|
32
|
+
describe "SafeInterpolate#sql_interpolate" do
|
33
|
+
include SafeInterpolate
|
34
|
+
|
35
|
+
tmp_db_file = '/tmp/test.sqlite'
|
36
|
+
|
37
|
+
before(:all) do
|
38
|
+
ActiveRecord::Base.configurations = { 'test' => { :adapter => 'sqlite3', :database => tmp_db_file, :timeout => 5000 } }
|
39
|
+
ActiveRecord::Base.establish_connection('test')
|
40
|
+
end
|
41
|
+
|
42
|
+
after(:all) do
|
43
|
+
ActiveRecord::Base.remove_connection
|
44
|
+
File.delete(tmp_db_file) rescue nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns string passed in block" do
|
48
|
+
sql_interpolate { '42' }.should == "42"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "interpolates expressions" do
|
52
|
+
num = 1
|
53
|
+
str = '123'
|
54
|
+
sql_interpolate { 'before #{ num } #{ str } after' }.should == 'before 1 \'123\' after'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "properly quotes SQL sensitive characters" do
|
58
|
+
str = "'asd'; DROP TABLE users"
|
59
|
+
sql_interpolate { '#{ str }' }.should == "'''asd''; DROP TABLE users'"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "SafeInterpolate#html_interpolate" do
|
64
|
+
include SafeInterpolate
|
65
|
+
|
66
|
+
it "properly quotes HTML sensitive characters" do
|
67
|
+
str = '&"><'
|
68
|
+
html_interpolate { '<p>#{ str }</p>' }.should == "<p>&"><</p>"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "SafeInterpolate#uri_interpolate" do
|
73
|
+
include SafeInterpolate
|
74
|
+
|
75
|
+
it "properly quotes URI sensitive characters" do
|
76
|
+
str = ':&? ='
|
77
|
+
uri_interpolate { 'http://example.com?q=#{ str }' }.should == "http://example.com?q=%3A%26%3F+%3D"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 23
|
9
|
+
version: 0.0.23
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Evgeniy Dolzhenko
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-07-02 00:00:00 +04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/dolzenko/io_interceptor.rb
|
73
73
|
- lib/dolzenko/light.rb
|
74
74
|
- lib/dolzenko/remote_download.rb
|
75
|
+
- lib/dolzenko/safe_interpolate.rb
|
75
76
|
- lib/dolzenko/shell_out.rb
|
76
77
|
- lib/dolzenko/try_block.rb
|
77
78
|
- lib/dolzenko.rb
|