dolzenko 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,3 +9,4 @@ autoload :OpenStruct, "ostruct"
9
9
  module Net
10
10
  autoload :HTTP, "net/http"
11
11
  end
12
+ autoload :StringIO, "stringio"
@@ -1,3 +1,5 @@
1
+ require "stringio"
2
+
1
3
  module Dolzenko
2
4
  # Simple `Net::HTTP` mumbo jumbo we all have to use occasionally.
3
5
  module RemoteDownload
@@ -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>&amp;&quot;&gt;&lt;</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
- - 22
9
- version: 0.0.22
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-06-04 00:00:00 +04:00
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