web_resource_bundler 0.0.14 → 0.0.15
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/.gitignore +1 -0
- data/Rakefile +1 -1
- data/Readme.md +141 -0
- data/VERSION +1 -1
- data/lib/web_resource_bundler/content_management/block_data.rb +3 -3
- data/lib/web_resource_bundler/content_management/block_parser.rb +2 -2
- data/lib/web_resource_bundler/content_management/resource_file.rb +11 -8
- data/lib/web_resource_bundler/exceptions.rb +6 -0
- data/lib/web_resource_bundler/filters/bundle_filter/resource_packager.rb +1 -1
- data/lib/web_resource_bundler/filters/bundle_filter.rb +3 -1
- data/lib/web_resource_bundler/filters/image_encode_filter/css_generator.rb +1 -3
- data/lib/web_resource_bundler/filters/image_encode_filter/image_data.rb +3 -3
- data/lib/web_resource_bundler/filters/image_encode_filter.rb +3 -1
- data/lib/web_resource_bundler/rails_app_helpers.rb +5 -3
- data/lib/web_resource_bundler/settings_manager.rb +91 -0
- data/lib/web_resource_bundler/web_resource_bundler_init.rb +2 -16
- data/lib/web_resource_bundler.rb +36 -52
- data/spec/sample_block_helper.rb +6 -6
- data/spec/spec_helper.rb +14 -6
- data/spec/test_data/config/web_resource_bundler.yml +21 -0
- data/spec/{public → test_data/public}/foo.css +0 -0
- data/spec/{public → test_data/public}/images/good.jpg +0 -0
- data/spec/{public → test_data/public}/images/logo.jpg +0 -0
- data/spec/{public → test_data/public}/images/sdfo.jpg +0 -0
- data/spec/{public → test_data/public}/images/too_big_image.jpg +0 -0
- data/spec/{public → test_data/public}/marketing.js +0 -0
- data/spec/{public → test_data/public}/salog20.js +0 -0
- data/spec/{public → test_data/public}/sample.css +0 -0
- data/spec/{public → test_data/public}/seal.js +0 -0
- data/spec/{public → test_data/public}/set_cookies.js +0 -0
- data/spec/{public → test_data/public}/styles/boo.css +0 -0
- data/spec/{public → test_data/public}/styles/for_import.css +0 -0
- data/spec/{public → test_data/public}/temp.css +0 -0
- data/spec/{public → test_data/public}/test.css +0 -0
- data/spec/web_resource_bundler/content_management/resource_file_spec.rb +11 -3
- data/spec/web_resource_bundler/filters/bundle_filter/filter_spec.rb +4 -2
- data/spec/web_resource_bundler/filters/image_encode_filter/filter_spec.rb +17 -4
- data/spec/web_resource_bundler/settings_manager_spec.rb +88 -0
- data/spec/web_resource_bundler/web_resource_bundler_spec.rb +96 -94
- data/web_resource_bundler.gemspec +23 -23
- metadata +25 -22
- data/.bundle/config +0 -2
- data/README +0 -143
@@ -1,17 +1,3 @@
|
|
1
|
-
|
2
|
-
require 'yaml'
|
3
|
-
root_dir = Rails.root #or RAILS_ROOT if you are using older rails version than 3
|
4
|
-
environment = Rails.env
|
5
|
-
settings = { }
|
6
|
-
settings_file_path = File.join(root_dir,'config', 'web_resource_bundler.yml')
|
7
|
-
if File.exist?(settings_file_path)
|
8
|
-
settings_file = File.open(settings_file_path)
|
9
|
-
all_settings = YAML::load(settings_file)
|
10
|
-
if all_settings[environment]
|
11
|
-
settings = all_settings[environment]
|
12
|
-
settings[:resource_dir] = File.join(root_dir, 'public')
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
WebResourceBundler::Bundler.instance.set_settings(settings)
|
1
|
+
WebResourceBundler::Bundler.instance.setup(Rails.root, Rails.env)
|
17
2
|
ActionView::Base.send(:include, WebResourceBundler::RailsAppHelpers)
|
3
|
+
|
data/lib/web_resource_bundler.rb
CHANGED
@@ -5,6 +5,7 @@ require 'content_management/css_url_rewriter'
|
|
5
5
|
require 'content_management/resource_file'
|
6
6
|
|
7
7
|
require 'file_manager'
|
8
|
+
require 'settings_manager'
|
8
9
|
require 'logger'
|
9
10
|
require 'filters'
|
10
11
|
require 'exceptions'
|
@@ -16,59 +17,45 @@ module WebResourceBundler
|
|
16
17
|
class Bundler
|
17
18
|
include Singleton
|
18
19
|
|
19
|
-
attr_reader :settings, :settings_correct, :filters
|
20
|
-
@@logger = nil
|
21
|
-
def self.logger
|
22
|
-
@@logger
|
23
|
-
end
|
20
|
+
attr_reader :settings, :settings_correct, :filters, :logger
|
24
21
|
|
25
22
|
def initialize
|
26
23
|
@filters = {}
|
27
24
|
@settings = nil
|
28
25
|
@file_manager = FileManager.new('','')
|
29
26
|
@parser = BlockParser.new
|
30
|
-
|
27
|
+
@logger = nil
|
31
28
|
@settings_correct = false
|
32
29
|
end
|
33
30
|
|
34
|
-
#
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
unless @settings[:log_path]
|
43
|
-
log_dir = File.expand_path('../log', @settings[:resource_dir])
|
44
|
-
log_name = 'web_resource_bundler.log'
|
45
|
-
@settings[:log_path] = File.join(log_dir, log_name)
|
46
|
-
Dir.mkdir(log_dir) unless File.exist?(log_dir)
|
47
|
-
end
|
48
|
-
@@logger = create_logger(@settings[:log_path]) unless @@logger
|
49
|
-
|
50
|
-
unless @settings[:cache_dir]
|
51
|
-
@settings[:cache_dir] = 'cache'
|
52
|
-
end
|
53
|
-
|
54
|
-
@file_manager.resource_dir, @file_manager.cache_dir = @settings[:resource_dir], @settings[:cache_dir]
|
55
|
-
set_filters(@settings, @file_manager)
|
56
|
-
#used to determine if bundler in correct state and could be used
|
57
|
-
@settings_correct = true
|
58
|
-
else
|
59
|
-
@settings_correct = false
|
60
|
-
end
|
61
|
-
rescue Exception => e
|
62
|
-
@@logger.error("Incorrect settings initialization, #{settings}\n#{e.to_s}") if @@logger
|
63
|
-
@settings_correct = false
|
31
|
+
#this method should called in initializer
|
32
|
+
def setup(rails_root, rails_env)
|
33
|
+
@settings = SettingsManager.create_settings(rails_root, rails_env)
|
34
|
+
@settings_correct = SettingsManager.settings_correct?(@settings)
|
35
|
+
if @settings_correct
|
36
|
+
@logger = create_logger(@settings[:log_path]) unless @logger
|
37
|
+
@file_manager.resource_dir, @file_manager.cache_dir = @settings[:resource_dir], @settings[:cache_dir]
|
38
|
+
set_filters(@settings, @file_manager)
|
64
39
|
end
|
65
40
|
end
|
66
41
|
|
42
|
+
#this method should be used to turn on\off filters
|
43
|
+
#on particular request
|
44
|
+
def set_settings(settings)
|
45
|
+
@settings.merge!(settings)
|
46
|
+
@settings_correct = SettingsManager.settings_correct?(@settings)
|
47
|
+
set_filters(@settings, @file_manager)
|
48
|
+
end
|
49
|
+
|
67
50
|
#main method to process html text block
|
68
|
-
def process(block)
|
51
|
+
def process(block, domain, protocol)
|
69
52
|
if @settings_correct
|
70
53
|
begin
|
71
54
|
filters = filters_array
|
55
|
+
#passing current request domain and protocol to each filter
|
56
|
+
filters.each do |filter|
|
57
|
+
SettingsManager.set_request_specific_settings!(filter.settings, domain, protocol)
|
58
|
+
end
|
72
59
|
#parsing html text block, creating BlockData instance
|
73
60
|
block_data = @parser.parse(block)
|
74
61
|
#if filters set and no bundle files exists we should process block data
|
@@ -79,17 +66,17 @@ module WebResourceBundler
|
|
79
66
|
block_data.apply_filters(filters)
|
80
67
|
#writing resulted files with filtered content on disk
|
81
68
|
write_files_on_disk(block_data)
|
82
|
-
|
69
|
+
@logger.info("files written on disk")
|
83
70
|
return block_data
|
84
71
|
end
|
85
72
|
#bundle up to date, returning existing block with modified file names
|
86
73
|
block_data.apply_filters(filters)
|
87
74
|
return block_data
|
88
75
|
rescue Exceptions::WebResourceBundlerError => e
|
89
|
-
|
76
|
+
@logger.error(e.to_s)
|
90
77
|
return nil
|
91
78
|
rescue Exception => e
|
92
|
-
|
79
|
+
@logger.error(e.backtrace.join("\n") + "Unknown error occured: " + e.to_s)
|
93
80
|
return nil
|
94
81
|
end
|
95
82
|
end
|
@@ -108,20 +95,15 @@ module WebResourceBundler
|
|
108
95
|
|
109
96
|
#creates filters or change their settings
|
110
97
|
def set_filters(settings, file_manager)
|
111
|
-
#common settings same for all filters
|
112
|
-
common_sets = {
|
113
|
-
:resource_dir => settings[:resource_dir],
|
114
|
-
:cache_dir => settings[:cache_dir]
|
115
|
-
}
|
116
|
-
#used to create filters
|
117
98
|
filters_data = {
|
118
99
|
:bundle_filter => 'BundleFilter',
|
119
100
|
:base64_filter => 'ImageEncodeFilter',
|
120
101
|
:cdn_filter => 'CdnFilter'
|
121
102
|
}
|
122
103
|
filters_data.each_pair do |key, filter_class|
|
104
|
+
#if filter settings are present and filter turned on
|
123
105
|
if settings[key] and settings[key][:use]
|
124
|
-
filter_settings =
|
106
|
+
filter_settings = SettingsManager.send("#{key}_settings", settings)
|
125
107
|
if @filters[key]
|
126
108
|
@filters[key].set_settings(filter_settings)
|
127
109
|
else
|
@@ -133,13 +115,15 @@ module WebResourceBundler
|
|
133
115
|
@filters
|
134
116
|
end
|
135
117
|
|
136
|
-
def create_logger(
|
118
|
+
def create_logger(log_path)
|
137
119
|
begin
|
138
|
-
|
120
|
+
log_dir = File.dirname(log_path)
|
121
|
+
#we should create log dir in rails root if it doesn't exist
|
122
|
+
Dir.mkdir(log_dir) unless File.exist?(log_dir)
|
123
|
+
file = File.open(log_path, File::WRONLY | File::APPEND | File::CREAT)
|
139
124
|
logger = Logger.new(file)
|
140
125
|
rescue Exception => e
|
141
|
-
|
142
|
-
logger.error("Can't create log file, check log path: #{logfile_path}\n#{e.to_s}")
|
126
|
+
raise WebResourceBundler::Exceptions::LogCreationError.new(log_path, e.to_s)
|
143
127
|
end
|
144
128
|
logger
|
145
129
|
end
|
@@ -166,7 +150,7 @@ module WebResourceBundler
|
|
166
150
|
block_data.files.each do |file|
|
167
151
|
content = @file_manager.get_content(file.path)
|
168
152
|
#rewriting url to absolute if content is css
|
169
|
-
WebResourceBundler::CssUrlRewriter.rewrite_content_urls!(file.path, content) if file.
|
153
|
+
WebResourceBundler::CssUrlRewriter.rewrite_content_urls!(file.path, content) if file.types.first[:ext] == 'css'
|
170
154
|
file.content = content
|
171
155
|
end
|
172
156
|
#making the same for each child blocks, recursively
|
data/spec/sample_block_helper.rb
CHANGED
@@ -29,8 +29,8 @@ class SampleBlockHelper
|
|
29
29
|
block
|
30
30
|
end
|
31
31
|
|
32
|
-
def construct_resource_file(
|
33
|
-
resource_file = WebResourceBundler::ResourceFile.new(
|
32
|
+
def construct_resource_file(file, content, *types)
|
33
|
+
resource_file = WebResourceBundler::ResourceFile.new(file, '', types)
|
34
34
|
if content
|
35
35
|
resource_file.content = content
|
36
36
|
else
|
@@ -55,10 +55,10 @@ class SampleBlockHelper
|
|
55
55
|
def sample_block_data
|
56
56
|
data = BlockData.new
|
57
57
|
@styles[0..(@styles.size / 2 - 1)].each do |file|
|
58
|
-
data.files << construct_resource_file(ResourceFileType::CSS,
|
58
|
+
data.files << construct_resource_file(file, nil, ResourceFileType::CSS, ResourceFileType::IE_CSS)
|
59
59
|
end
|
60
60
|
@scripts[0..(@scripts.size / 2 - 1)].each do |file|
|
61
|
-
data.files << construct_resource_file(ResourceFileType::JS
|
61
|
+
data.files << construct_resource_file(file, nil, ResourceFileType::JS)
|
62
62
|
end
|
63
63
|
data.inline_block = sample_inline_block
|
64
64
|
data.child_blocks << child_block_data
|
@@ -68,10 +68,10 @@ class SampleBlockHelper
|
|
68
68
|
def child_block_data
|
69
69
|
child = BlockData.new("[if IE 7]")
|
70
70
|
@styles[(@styles.size / 2)..(@styles.size - 1)].each do |file|
|
71
|
-
child.files << construct_resource_file(ResourceFileType::CSS
|
71
|
+
child.files << construct_resource_file(file, nil, ResourceFileType::CSS)
|
72
72
|
end
|
73
73
|
@scripts[(@scripts.size / 2)..(@scripts.size - 1)].each do |file|
|
74
|
-
child.files << construct_resource_file(ResourceFileType::JS
|
74
|
+
child.files << construct_resource_file(file, nil, ResourceFileType::JS)
|
75
75
|
end
|
76
76
|
child.inline_block = sample_inline_block
|
77
77
|
child
|
data/spec/spec_helper.rb
CHANGED
@@ -19,20 +19,23 @@ def scripts
|
|
19
19
|
["/set_cookies.js", "/seal.js", "/salog20.js", "/marketing.js"]
|
20
20
|
end
|
21
21
|
|
22
|
+
def root_dir
|
23
|
+
File.join(File.dirname(__FILE__), 'test_data')
|
24
|
+
end
|
25
|
+
|
22
26
|
def settings
|
23
27
|
{
|
24
|
-
:resource_dir => File.join(
|
28
|
+
:resource_dir => File.join(root_dir, '/public'),
|
25
29
|
:cache_dir => 'cache',
|
26
|
-
:log_path => File.join(
|
30
|
+
:log_path => File.join(root_dir, '/log/bundler.log'),
|
31
|
+
:protocol => 'http',
|
32
|
+
:domain => 'localhost:3000',
|
27
33
|
:base64_filter => {
|
28
34
|
:use => true,
|
29
35
|
:max_image_size => 23, #kbytes
|
30
|
-
:protocol => 'http',
|
31
|
-
:domain => 'localhost:3000'
|
32
36
|
},
|
33
37
|
:bundle_filter => {
|
34
38
|
:use => true,
|
35
|
-
:md5_additional_data => ['localhost:3000', 'http'],
|
36
39
|
:filename_additional_data => ['en']
|
37
40
|
},
|
38
41
|
:cdn_filter => {
|
@@ -56,7 +59,12 @@ def base64_settings
|
|
56
59
|
end
|
57
60
|
|
58
61
|
def common_settings
|
59
|
-
{
|
62
|
+
{
|
63
|
+
:resource_dir => settings[:resource_dir],
|
64
|
+
:cache_dir => settings[:cache_dir],
|
65
|
+
:domain => settings[:domain],
|
66
|
+
:protocol => settings[:protocol]
|
67
|
+
}
|
60
68
|
end
|
61
69
|
|
62
70
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
development:
|
2
|
+
:base64_filter:
|
3
|
+
:use: true
|
4
|
+
:max_image_size: 23
|
5
|
+
:bundle_filter:
|
6
|
+
:use: true
|
7
|
+
:cdn_filter:
|
8
|
+
:use: true
|
9
|
+
:http_hosts: ['http://localhost:3000']
|
10
|
+
:https_hosts: ['https://localhost:3000']
|
11
|
+
|
12
|
+
production:
|
13
|
+
:base64_filter:
|
14
|
+
:use: true
|
15
|
+
:max_image_size: 23
|
16
|
+
:bundle_filter:
|
17
|
+
:use: true
|
18
|
+
:cdn_filter:
|
19
|
+
:use: true
|
20
|
+
:http_hosts: ['http://s1.wrb.railsware.com', 'http://s2.wrb.railsware.com', 'http://s3.wrb.railsware.com', 'http://s4.wrb.railsware.com']
|
21
|
+
:https_hosts: ['http://s1.wrb.railsware.com', 'http://s2.wrb.railsware.com', 'http://s3.wrb.railsware.com', 'http://s4.wrb.railsware.com']
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -5,7 +5,7 @@ describe WebResourceBundler::ResourceFile do
|
|
5
5
|
f = WebResourceBundler::ResourceFile.new_css_file('a', 'b')
|
6
6
|
f.path.should == 'a'
|
7
7
|
f.content.should == 'b'
|
8
|
-
f.
|
8
|
+
f.types.first.should == WebResourceBundler::ResourceFileType::CSS
|
9
9
|
end
|
10
10
|
end
|
11
11
|
describe "#new_js_file" do
|
@@ -13,7 +13,7 @@ describe WebResourceBundler::ResourceFile do
|
|
13
13
|
f = WebResourceBundler::ResourceFile.new_js_file('a', 'b')
|
14
14
|
f.path.should == 'a'
|
15
15
|
f.content.should == 'b'
|
16
|
-
f.
|
16
|
+
f.types.first.should == WebResourceBundler::ResourceFileType::JS
|
17
17
|
end
|
18
18
|
end
|
19
19
|
describe "#new_mhtml_file" do
|
@@ -21,7 +21,15 @@ describe WebResourceBundler::ResourceFile do
|
|
21
21
|
f = WebResourceBundler::ResourceFile.new_mhtml_file('a', 'b')
|
22
22
|
f.path.should == 'a'
|
23
23
|
f.content.should == 'b'
|
24
|
-
f.
|
24
|
+
f.types.first.should == WebResourceBundler::ResourceFileType::MHTML
|
25
|
+
end
|
26
|
+
end
|
27
|
+
describe "#new_style_file" do
|
28
|
+
it "creates new resource file of CSS and IE_CSS type" do
|
29
|
+
f = WebResourceBundler::ResourceFile.new_style_file('a', 'b')
|
30
|
+
f.path.should == 'a'
|
31
|
+
f.content.should == 'b'
|
32
|
+
f.types.should == [WebResourceBundler::ResourceFileType::CSS, WebResourceBundler::ResourceFileType::IE_CSS]
|
25
33
|
end
|
26
34
|
end
|
27
35
|
describe "#clone" do
|
@@ -8,10 +8,12 @@ describe WebResourceBundler::Filters::BundleFilter::Filter do
|
|
8
8
|
@block_data = @sample_block_helper.sample_block_data
|
9
9
|
css_type = ResourceFileType::CSS
|
10
10
|
js_type = ResourceFileType::JS
|
11
|
-
items = [@block_data.styles.map {|f| f.path}.sort] + @bundle_settings[:
|
11
|
+
items = [@block_data.styles.map {|f| f.path}.sort] + [@bundle_settings[:protocol], @bundle_settings[:domain]]
|
12
|
+
items += @bundle_settings[:md5_additional_data] if @bundle_settings[:md5_additional_data]
|
12
13
|
@css_md5_value = Digest::MD5.hexdigest(items.flatten.join('|'))
|
13
14
|
@css_bundle_file = File.join(@settings[:cache_dir], [css_type[:name] + '_' + @css_md5_value, 'en', css_type[:ext]].join('.'))
|
14
|
-
items = [@block_data.scripts.map {|f| f.path}.sort]
|
15
|
+
items = [@block_data.scripts.map {|f| f.path}.sort] + [@bundle_settings[:protocol], @bundle_settings[:domain]]
|
16
|
+
items += @bundle_settings[:md5_additional_data] if @bundle_settings[:md5_additional_data]
|
15
17
|
js_md5_value = Digest::MD5.hexdigest(items.flatten.join('|'))
|
16
18
|
@js_bundle_file = File.join(@settings[:cache_dir], [js_type[:name] + '_' + js_md5_value, 'en', js_type[:ext]].join('.'))
|
17
19
|
end
|
@@ -26,7 +26,7 @@ describe WebResourceBundler::Filters::ImageEncodeFilter::Filter do
|
|
26
26
|
|
27
27
|
describe "#mhtml_filepath" do
|
28
28
|
it "returns mhtml file path" do
|
29
|
-
@filter.mhtml_filepath('styles/1.css').should == 'cache/mhtml_1.
|
29
|
+
@filter.mhtml_filepath('styles/1.css').should == 'cache/mhtml_1.mhtml'
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -34,17 +34,30 @@ describe WebResourceBundler::Filters::ImageEncodeFilter::Filter do
|
|
34
34
|
context "block was bundled" do
|
35
35
|
before(:each) do
|
36
36
|
@bundler_filter = Filters::BundleFilter::Filter.new(@base64_settings, @file_manager)
|
37
|
-
|
37
|
+
end
|
38
38
|
it "encodes images in css and change filename" do
|
39
39
|
block_data = @sample_block_helper.sample_block_data
|
40
40
|
bundle_filepath = @bundler_filter.bundle_filepath(WebResourceBundler::ResourceFileType::CSS, block_data.styles)
|
41
41
|
@bundler_filter.apply!(block_data)
|
42
42
|
@filter.apply!(block_data)
|
43
|
-
generated_files = block_data.files.map {|f| f.path}
|
43
|
+
generated_files = block_data.files.map {|f| f.path}
|
44
44
|
generated_files.include?(File.join(@settings[:cache_dir], @file_prefix + File.basename(bundle_filepath))).should be_true
|
45
45
|
generated_files.include?(File.join(@settings[:cache_dir], @ie_file_prefix + File.basename(bundle_filepath))).should be_true
|
46
46
|
end
|
47
47
|
|
48
|
+
it "changes type of styles files to CSS only" do
|
49
|
+
block_data = @sample_block_helper.sample_block_data
|
50
|
+
resource_file = WebResourceBundler::ResourceFile.new_style_file(styles.first)
|
51
|
+
block_data.files = [resource_file]
|
52
|
+
block_data.child_blocks = []
|
53
|
+
@bundler_filter.apply!(block_data)
|
54
|
+
@filter.apply!(block_data)
|
55
|
+
block_data.files.size.should == 3
|
56
|
+
%w{CSS IE_CSS MHTML}.each do |type_name|
|
57
|
+
type = eval("WebResourceBundler::ResourceFileType::" + type_name)
|
58
|
+
block_data.files.select {|f| f.types == [type]}.size.should == 1
|
59
|
+
end
|
60
|
+
end
|
48
61
|
end
|
49
62
|
context "block wasn't bundled" do
|
50
63
|
before(:each) do
|
@@ -53,7 +66,7 @@ describe WebResourceBundler::Filters::ImageEncodeFilter::Filter do
|
|
53
66
|
@block_data.files = []
|
54
67
|
@files = %w{sample.css foo.css}
|
55
68
|
@files.each do |f|
|
56
|
-
@block_data.files << @sample_block_helper.construct_resource_file(WebResourceBundler::ResourceFileType::CSS
|
69
|
+
@block_data.files << @sample_block_helper.construct_resource_file(f,'', WebResourceBundler::ResourceFileType::CSS)
|
57
70
|
end
|
58
71
|
@filter.apply!(@block_data)
|
59
72
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../spec_helper"))
|
2
|
+
describe WebResourceBundler::SettingsManager do
|
3
|
+
|
4
|
+
before(:each) do
|
5
|
+
@s = settings
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#create_default_settings" do
|
9
|
+
it "returns default required settings for bundler" do
|
10
|
+
root = '/root'
|
11
|
+
settings = SettingsManager.create_default_settings(root)
|
12
|
+
settings[:resource_dir].should == File.join(root, SettingsManager::DEFAULT_RESOURCE_DIR)
|
13
|
+
settings[:log_path].should == File.join(root, SettingsManager::DEFAULT_LOG_PATH)
|
14
|
+
settings[:cache_dir].should == SettingsManager::DEFAULT_CACHE_DIR
|
15
|
+
settings[:bundle_filter][:use].should == true
|
16
|
+
settings[:cdn_filter][:use].should == false
|
17
|
+
settings[:base64_filter][:use].should == true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#common_settings" do
|
22
|
+
it "returns settings common to all filters" do
|
23
|
+
settings = SettingsManager.common_settings(@s)
|
24
|
+
settings.keys.size.should == 2
|
25
|
+
settings[:resource_dir].should == @s[:resource_dir]
|
26
|
+
settings[:cache_dir].should == @s[:cache_dir]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#settings_from_file" do
|
31
|
+
it "loads settings from yaml file" do
|
32
|
+
rails_root = root_dir
|
33
|
+
content = File.read(File.join(root_dir, 'config/web_resource_bundler.yml'))
|
34
|
+
rails_env = 'development'
|
35
|
+
original = YAML::load(content)[rails_env]
|
36
|
+
settings = SettingsManager.settings_from_file(rails_root, rails_env)
|
37
|
+
original.each_key do |key|
|
38
|
+
settings[key].should == original[key]
|
39
|
+
end
|
40
|
+
settings[:resource_dir].should == File.join(root_dir, SettingsManager::DEFAULT_RESOURCE_DIR)
|
41
|
+
end
|
42
|
+
it "returns empty hash if file isn't exist" do
|
43
|
+
SettingsManager.settings_from_file('', '').should == {}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#settings_correct?" do
|
48
|
+
it "should return true if all required keys present" do
|
49
|
+
SettingsManager.settings_correct?(@s).should be_true
|
50
|
+
end
|
51
|
+
it "returns false if one of the keys isn't present" do
|
52
|
+
@s.delete(:resource_dir)
|
53
|
+
SettingsManager.settings_correct?(@s).should be_false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "dynamically created filter settings methods" do
|
58
|
+
it "properly creates methods for each filter settings" do
|
59
|
+
%w{cdn_filter_settings base64_filter_settings bundle_filter_settings}.each do |method|
|
60
|
+
if RUBY_VERSION > '1.9.0'
|
61
|
+
key = method.to_sym
|
62
|
+
else
|
63
|
+
key = method
|
64
|
+
end
|
65
|
+
SettingsManager.public_methods.include?(key).should be_true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
describe "#cdn_filter_settings" do
|
69
|
+
it "returns cdn settings merged with commons" do
|
70
|
+
cdn = SettingsManager.cdn_filter_settings(@s)
|
71
|
+
cdn[:resource_dir].should == @s[:resource_dir]
|
72
|
+
cdn[:cache_dir].should == @s[:cache_dir]
|
73
|
+
cdn[:use].should == @s[:cdn_filter][:use]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#set_request_specific_settings!" do
|
79
|
+
it "sets domain and protocol for settings" do
|
80
|
+
s = {}
|
81
|
+
SettingsManager.set_request_specific_settings!(s, 'google.com', 'http')
|
82
|
+
s[:domain].should == 'google.com'
|
83
|
+
s[:protocol].should == 'http'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|