web_resource_bundler 0.0.20 → 0.0.21

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.
Files changed (34) hide show
  1. data/VERSION +1 -1
  2. data/lib/web_resource_bundler/content_management/block_data.rb +18 -21
  3. data/lib/web_resource_bundler/content_management/block_parser.rb +58 -52
  4. data/lib/web_resource_bundler/content_management/css_url_rewriter.rb +3 -3
  5. data/lib/web_resource_bundler/content_management/resource_file.rb +38 -25
  6. data/lib/web_resource_bundler/exceptions.rb +1 -3
  7. data/lib/web_resource_bundler/file_manager.rb +14 -3
  8. data/lib/web_resource_bundler/filters/bundle_filter/resource_packager.rb +45 -27
  9. data/lib/web_resource_bundler/filters/bundle_filter.rb +27 -22
  10. data/lib/web_resource_bundler/filters/cdn_filter.rb +27 -23
  11. data/lib/web_resource_bundler/filters/image_encode_filter/css_generator.rb +53 -40
  12. data/lib/web_resource_bundler/filters/image_encode_filter/image_data.rb +22 -17
  13. data/lib/web_resource_bundler/filters/image_encode_filter.rb +49 -23
  14. data/lib/web_resource_bundler/filters.rb +6 -1
  15. data/lib/web_resource_bundler/rails_app_helpers.rb +56 -40
  16. data/lib/web_resource_bundler/settings.rb +99 -0
  17. data/lib/web_resource_bundler/web_resource_bundler_init.rb +1 -1
  18. data/lib/web_resource_bundler.rb +100 -125
  19. data/spec/sample_block_helper.rb +3 -3
  20. data/spec/web_resource_bundler/content_management/block_data_spec.rb +56 -11
  21. data/spec/web_resource_bundler/content_management/block_parser_spec.rb +24 -10
  22. data/spec/web_resource_bundler/content_management/resource_file_spec.rb +7 -15
  23. data/spec/web_resource_bundler/file_manager_spec.rb +12 -4
  24. data/spec/web_resource_bundler/filters/bundle_filter/filter_spec.rb +5 -5
  25. data/spec/web_resource_bundler/filters/bundle_filter/resource_packager_spec.rb +15 -2
  26. data/spec/web_resource_bundler/filters/cdn_filter_spec.rb +15 -7
  27. data/spec/web_resource_bundler/filters/image_encode_filter/css_generator_spec.rb +44 -4
  28. data/spec/web_resource_bundler/filters/image_encode_filter/filter_spec.rb +20 -15
  29. data/spec/web_resource_bundler/settings_spec.rb +77 -0
  30. data/spec/web_resource_bundler/web_resource_bundler_spec.rb +19 -26
  31. data/web_resource_bundler.gemspec +4 -4
  32. metadata +5 -5
  33. data/lib/web_resource_bundler/settings_manager.rb +0 -91
  34. data/spec/web_resource_bundler/settings_manager_spec.rb +0 -88
@@ -5,173 +5,148 @@ 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
+ require 'settings'
9
9
  require 'logger'
10
10
  require 'filters'
11
11
  require 'exceptions'
12
12
  require 'rails_app_helpers'
13
13
  require 'yaml'
14
- require 'singleton'
15
14
  require 'digest/md5'
15
+ require 'uri'
16
16
 
17
17
  module WebResourceBundler
18
18
  class Bundler
19
- include Singleton
20
19
 
21
- attr_reader :settings, :settings_correct, :filters, :logger
20
+ class << self
22
21
 
23
- def initialize
24
- @filters = {}
25
- @settings = nil
26
- @file_manager = FileManager.new('','')
27
- @parser = BlockParser.new
28
- @logger = nil
29
- @settings_correct = false
30
- end
22
+ attr_reader :filters, :logger
31
23
 
32
- #this method should called in initializer
33
- def setup(rails_root, rails_env)
34
- @settings = SettingsManager.create_settings(rails_root, rails_env)
35
- @settings_correct = SettingsManager.settings_correct?(@settings)
36
- if @settings_correct
37
- @logger = create_logger(@settings[:log_path]) unless @logger
38
- @file_manager.resource_dir, @file_manager.cache_dir = @settings[:resource_dir], @settings[:cache_dir]
39
- set_filters(@settings, @file_manager)
24
+ #this method should called in initializer
25
+ def setup(rails_root, rails_env)
26
+ settings = Settings.create_settings(rails_root, rails_env)
27
+ return false unless Settings.correct?
28
+ @logger = create_logger(settings[:log_path]) unless @logger
29
+ @file_manager = FileManager.new(settings)
30
+ @filters = set_filters({}, @file_manager)
40
31
  end
41
- end
42
32
 
43
- #this method should be used to turn on\off filters
44
- #on particular request
45
- def set_settings(settings)
46
- @settings.merge!(settings)
47
- @settings_correct = SettingsManager.settings_correct?(@settings)
48
- set_filters(@settings, @file_manager)
49
- end
33
+ #this method should be used to turn on\off filters
34
+ #on particular specific request
35
+ def set_settings(settings)
36
+ begin
37
+ return false unless Settings.correct?(settings)
38
+ Settings.set(settings)
39
+ set_filters(@filters, @file_manager)
40
+ @file_manager.set_settings(settings)
41
+ true
42
+ rescue Exception => e
43
+ @logger.error("Error occured while trying to change settings")
44
+ false
45
+ end
46
+ end
50
47
 
51
- #main method to process html text block
52
- def process(block, domain, protocol)
53
- if @settings_correct
48
+ #main method to process html text block
49
+ #filters settings changed with request specific data
50
+ #html block parsed, and resulted bundle file names calculated
51
+ #we don't want to read files from disk on each request
52
+ #if bundle is not up to date block_data populated with files content
53
+ #all filters applied and resulted block_data returnd
54
+ #all exceptions resqued and logged so that no exceptions are raised in rails app
55
+ def process(block, domain, protocol)
56
+ return nil unless Settings.correct?
54
57
  begin
55
- filters = filters_array
56
- #passing current request domain and protocol to each filter
57
- filters.each do |filter|
58
- SettingsManager.set_request_specific_settings!(filter.settings, domain, protocol)
59
- end
60
- #parsing html text block, creating BlockData instance
61
- block_data = @parser.parse(block)
62
- #if filters set and no bundle files exists we should process block data
63
- unless filters.empty? or bundle_upto_date?(block_data)
64
- #reading files content and populating block_data
58
+ block_data = BlockParser.parse(block)
59
+ filters = filters_array(@filters)
60
+ set_filters_request_specific_data!(filters, domain, protocol)
61
+ if filters.any? && !bundle_upto_date?(block_data, @filters)
65
62
  read_resources!(block_data)
66
- #applying filters to block_data
67
63
  block_data.apply_filters(filters)
68
- #writing resulted files with filtered content on disk
69
64
  write_files_on_disk(block_data)
70
65
  @logger.info("files written on disk")
71
66
  return block_data
72
67
  end
73
- #bundle up to date, returning existing block with modified file names
74
68
  block_data.apply_filters(filters)
75
- return block_data
76
- rescue Exceptions::WebResourceBundlerError => e
77
- @logger.error(e.to_s)
78
- return nil
69
+ block_data
79
70
  rescue Exception => e
80
- @logger.error(e.backtrace.join("\n") + "Unknown error occured: " + e.to_s)
81
- return nil
71
+ @logger.error("Error occured: " + e.to_s)
72
+ nil
82
73
  end
83
74
  end
84
- end
85
75
 
86
- private
76
+ private
87
77
 
88
- #giving filters array in right sequence (bundle filter should be first)
89
- def filters_array
90
- filters = []
91
- %w{bundle_filter base64_filter cdn_filter}.each do |key|
92
- filters << @filters[key.to_sym] if @settings[key.to_sym][:use] and @filters[key.to_sym]
78
+ def set_filters_request_specific_data!(filters, domain, protocol)
79
+ filters.each do |filter|
80
+ Settings.set_request_specific_data!(filter.settings, domain, protocol)
81
+ end
93
82
  end
94
- filters
95
- end
96
83
 
97
- #creates filters or change their settings
98
- def set_filters(settings, file_manager)
99
- filters_data = {
100
- :bundle_filter => 'BundleFilter',
101
- :base64_filter => 'ImageEncodeFilter',
102
- :cdn_filter => 'CdnFilter'
103
- }
104
- filters_data.each_pair do |key, filter_class|
105
- #if filter settings are present and filter turned on
106
- if settings[key] and settings[key][:use]
107
- filter_settings = SettingsManager.send("#{key}_settings", settings)
108
- if @filters[key]
109
- @filters[key].set_settings(filter_settings)
110
- else
111
- #creating filter instance with settings
112
- @filters[key] = eval("Filters::" + filter_class + "::Filter").new(filter_settings, file_manager)
113
- end
84
+ #giving filters array in right sequence (bundle filter should be first)
85
+ def filters_array(filters_hash)
86
+ filters = []
87
+ [:bundle_filter, :base64_filter, :cdn_filter].each do |name|
88
+ filters << filters_hash[name] if Settings.filter_used?(name) && filters_hash[name]
114
89
  end
90
+ filters
115
91
  end
116
- @filters
117
- end
118
92
 
119
- def create_logger(log_path)
120
- begin
121
- log_dir = File.dirname(log_path)
122
- #we should create log dir in rails root if it doesn't exist
123
- Dir.mkdir(log_dir) unless File.exist?(log_dir)
124
- file = File.open(log_path, File::WRONLY | File::APPEND | File::CREAT)
125
- logger = Logger.new(file)
126
- rescue Exception => e
127
- raise WebResourceBundler::Exceptions::LogCreationError.new(log_path, e.to_s)
93
+ #creates filters or change existing filter settings
94
+ def set_filters(filters, file_manager)
95
+ Filters::FILTER_NAMES.each_pair do |name, klass|
96
+ if Settings.filter_used?(name)
97
+ filter_settings = Settings.filter_settings(name)
98
+ filters[name] ? filters[name].set_settings(filter_settings) : filters[name] = klass::Filter.new(filter_settings, file_manager)
99
+ end
100
+ end
101
+ filters
128
102
  end
129
- logger
130
- end
131
103
 
132
- #checks if resulted files exist for current @filters and block data
133
- def bundle_upto_date?(block_data)
134
- #we don't want to change original parsed block data
135
- #so just making a clone, using overriden clone method in BlockData
136
- block_data_copy = block_data.clone
137
- #modifying clone to obtain resulted files
138
- #apply_filters will just compute resulted file paths
139
- #because block_data isn't populated with files content yet
140
- block_data_copy.apply_filters(filters_array)
141
- #cheking if resulted files exist on disk in cache folder
142
- block_data_copy.all_files.each do |file|
143
- return false unless File.exist?(File.join(@settings[:resource_dir], file.path))
104
+ #creates logger object with new log file in rails_app/log
105
+ #or appends to existing log file, log dir also created
106
+ #all exception catched
107
+ def create_logger(log_path)
108
+ begin
109
+ log_dir = File.dirname(log_path)
110
+ Dir.mkdir(log_dir) unless File.exist?(log_dir)
111
+ file = File.open(log_path, File::WRONLY | File::APPEND | File::CREAT)
112
+ logger = Logger.new(file)
113
+ rescue Exception => e
114
+ raise WebResourceBundler::Exceptions::LogCreationError.new(log_path, e.to_s)
115
+ end
116
+ logger
144
117
  end
145
- true
146
- end
147
118
 
148
- #reads block data resource files content from disk and populating block_data
149
- def read_resources!(block_data)
150
- #iterating through each resource files
151
- block_data.files.each do |file|
152
- content = @file_manager.get_content(file.path)
153
- #rewriting url to absolute if content is css
154
- WebResourceBundler::CssUrlRewriter.rewrite_content_urls!(file.path, content) if file.types.first[:ext] == 'css'
155
- file.content = content
156
- end
157
- #making the same for each child blocks, recursively
158
- block_data.child_blocks.each do |block|
159
- read_resources!(block)
119
+ #creates a clone of block_data to calculate resulted file names
120
+ #all filters applied to block_data
121
+ #if resulted bundle files exists - we considering bundle up to date
122
+ def bundle_upto_date?(block_data, filters)
123
+ block_data_copy = block_data.clone
124
+ filters = filters_array(filters)
125
+ block_data_copy.apply_filters(filters)
126
+ block_data_copy.all_files.each do |file|
127
+ return false unless File.exist?(File.join(Settings.settings[:resource_dir], file.path))
128
+ end
129
+ true
160
130
  end
161
- end
162
131
 
163
- #recursive method to write all resulted files on disk
164
- def write_files_on_disk(block_data)
165
- @file_manager.create_cache_dir
166
- block_data.files.each do |file|
167
- File.open(File.join(@settings[:resource_dir], file.path), "w") do |f|
168
- f.print(file.content)
132
+ #block_data and its childs (whole tree) populated with files content recursively
133
+ #relative url in css files rewritten to absolute
134
+ def read_resources!(block_data)
135
+ block_data.files.each do |file|
136
+ content = @file_manager.get_content(file.path)
137
+ WebResourceBundler::CssUrlRewriter.rewrite_content_urls!(file.path, content) if file.type[:ext] == 'css'
138
+ file.content = content
169
139
  end
140
+ block_data.child_blocks.each { |block| read_resources!(block) }
170
141
  end
171
- block_data.child_blocks.each do |block|
172
- write_files_on_disk(block)
142
+
143
+ #recursive method to write all resulted files on disk
144
+ def write_files_on_disk(block_data)
145
+ @file_manager.create_cache_dir
146
+ block_data.files.each { |file| @file_manager.write_file(file.path, file.content) }
147
+ block_data.child_blocks.each { |block| write_files_on_disk(block) }
173
148
  end
174
- end
175
149
 
150
+ end
176
151
  end
177
152
  end
@@ -29,8 +29,8 @@ class SampleBlockHelper
29
29
  block
30
30
  end
31
31
 
32
- def construct_resource_file(file, content, *types)
33
- resource_file = WebResourceBundler::ResourceFile.new(file, '', types)
32
+ def construct_resource_file(file, content, type)
33
+ resource_file = WebResourceBundler::ResourceFile.new(file, '', type)
34
34
  if content
35
35
  resource_file.content = content
36
36
  else
@@ -55,7 +55,7 @@ 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(file, nil, ResourceFileType::CSS, ResourceFileType::IE_CSS)
58
+ data.files << construct_resource_file(file, nil, ResourceFileType::CSS)
59
59
  end
60
60
  @scripts[0..(@scripts.size / 2 - 1)].each do |file|
61
61
  data.files << construct_resource_file(file, nil, ResourceFileType::JS)
@@ -1,30 +1,75 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "../../spec_helper"))
2
2
  describe WebResourceBundler::BlockData do
3
+
4
+ before(:each) do
5
+ @block_data = @sample_block_helper.sample_block_data
6
+ end
7
+
8
+ describe "#styles" do
9
+ it "should return all files with 'css' extension" do
10
+ @block_data.styles.each do |file|
11
+ File.basename(file.path).split('.')[-1].should == 'css'
12
+ end
13
+ end
14
+ end
15
+
16
+ describe "#scripts" do
17
+ it "should return all files with 'js' extension" do
18
+ @block_data.scripts.each do |file|
19
+ File.basename(file.path).split('.')[-1].should == 'js'
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "#mhtml_styles" do
25
+ it "should return all files with 'js' extension" do
26
+ @block_data.files = @block_data.files[0..2]
27
+ @block_data.files.first.type = WebResourceBundler::ResourceFileType::MHTML_CSS
28
+ @block_data.files[1].type = WebResourceBundler::ResourceFileType::CSS
29
+ @block_data.files.last.type = WebResourceBundler::ResourceFileType::MHTML
30
+ @block_data.mhtml_styles.size.should == 3
31
+ end
32
+ end
33
+
34
+ describe "#base64_styles" do
35
+ it "should return all files with 'js' extension" do
36
+ @block_data.files = @block_data.files[0..1]
37
+ @block_data.files.first.type = WebResourceBundler::ResourceFileType::BASE64_CSS
38
+ @block_data.files.last.type = WebResourceBundler::ResourceFileType::CSS
39
+ @block_data.base64_styles.size.should == 2
40
+ end
41
+ end
42
+
43
+ describe "#scripts" do
44
+ it "should return all files with 'js' extension" do
45
+ @block_data.scripts.each do |file|
46
+ File.basename(file.path).split('.')[-1].should == 'js'
47
+ end
48
+ end
49
+ end
50
+
3
51
  describe "#apply_filter" do
4
52
  it "applies filter to block_data, its childs, and theirs childs etc." do
5
53
  filter = mock("filter")
6
- block_data = @sample_block_helper.sample_block_data
7
- filter.should_receive(:apply!).with(block_data)
8
- filter.should_receive(:apply!).with(block_data.child_blocks.first)
54
+ filter.should_receive(:apply!).with(@block_data)
55
+ filter.should_receive(:apply!).with(@block_data.child_blocks.first)
9
56
  filters = [filter]
10
- block_data.apply_filters(filters)
57
+ @block_data.apply_filters(filters)
11
58
  end
12
59
  end
13
60
 
14
61
  describe "#all_childs" do
15
62
  it "creates array of block data and all its childs recursively" do
16
- block_data = @sample_block_helper.sample_block_data
17
- BlockData.all_childs(block_data).size.should == 2
63
+ BlockData.all_childs(@block_data).size.should == 2
18
64
  end
19
65
  end
20
66
 
21
67
  describe "#clone" do
22
68
  it "creates deep clone of block data" do
23
- block_data = @sample_block_helper.sample_block_data
24
- clon = block_data.clone
25
- block_data.object_id.should_not == clon.object_id
26
- ((block_data.files.map { |f| f.object_id })& clon.files.map {|f| f.object_id}).should be_empty
27
- child = block_data.child_blocks[0]
69
+ clon = @block_data.clone
70
+ @block_data.object_id.should_not == clon.object_id
71
+ ((@block_data.files.map { |f| f.object_id })& clon.files.map {|f| f.object_id}).should be_empty
72
+ child = @block_data.child_blocks[0]
28
73
  child_copy = clon.child_blocks[0]
29
74
  child.object_id.should_not == child_copy.object_id
30
75
  ((child.files.map { |f| f.object_id }) & child_copy.files.map {|f| f.object_id}).should be_empty
@@ -4,8 +4,8 @@ module WebResourceBundler
4
4
 
5
5
  before(:each) do
6
6
  @settings = settings
7
- @file_manager = FileManager.new(@settings[:resource_dir], @settings[:cache_dir])
8
- @block_parser = BlockParser.new
7
+ @file_manager = FileManager.new(@settings)
8
+ @block_parser = BlockParser
9
9
  end
10
10
 
11
11
  #conditional comment spec
@@ -31,17 +31,17 @@ module WebResourceBundler
31
31
  block += @sample_block_helper.sample_inline_block
32
32
  block += @sample_block_helper.construct_links_block(styles, scripts)
33
33
  block += @sample_block_helper.sample_inline_block
34
- @block_parser.remove_links(block).should == @sample_block_helper.sample_inline_block + @sample_block_helper.sample_inline_block
34
+ @block_parser.send(:remove_links, block).should == @sample_block_helper.sample_inline_block + @sample_block_helper.sample_inline_block
35
35
  end
36
36
 
37
37
  it "doesn't delete links to non js or css resource, like favicon for example" do
38
38
  text = "<link href='1.css' /><link rel='shortcut icon' href='/favicon.ico' />"
39
- @block_parser.remove_links(text).should == "<link rel='shortcut icon' href='/favicon.ico' />"
39
+ @block_parser.send(:remove_links, text).should == "<link rel='shortcut icon' href='/favicon.ico' />"
40
40
  end
41
41
 
42
42
  it "doesn't touch remote resources" do
43
43
  text = "<link href='http://google.com/1.css' type='text/css' rel='stylesheet' />"
44
- @block_parser.remove_links(text).include?(text).should be_true
44
+ @block_parser.send(:remove_links, text).include?(text).should be_true
45
45
  end
46
46
  end
47
47
 
@@ -51,8 +51,6 @@ module WebResourceBundler
51
51
  @block_parser.parse("").is_a?(BlockData).should be_true
52
52
  end
53
53
 
54
-
55
-
56
54
  it "returns empty BlockData when block is empty" do
57
55
  data = @block_parser.parse("")
58
56
  data.files.should be_empty
@@ -76,7 +74,7 @@ module WebResourceBundler
76
74
  describe "#find_files" do
77
75
 
78
76
  it "returns array of css and js files linked in block" do
79
- result = @block_parser.find_files(@sample_block_helper.construct_links_block(styles, scripts))
77
+ result = @block_parser.send(:find_files, @sample_block_helper.construct_links_block(styles, scripts))
80
78
  files = styles + scripts
81
79
  result.each do |f|
82
80
  files.include?(f.path).should be_true
@@ -85,17 +83,33 @@ module WebResourceBundler
85
83
 
86
84
  it "recognize only css and js files" do
87
85
  block = "<link href='/rss.atom' /> <link href='valid.css' />"
88
- result = @block_parser.find_files(block)
86
+ result = @block_parser.send(:find_files, block)
89
87
  result.first.path.should == 'valid.css'
90
88
  end
91
89
 
92
90
  it "recognize files only on disk, not full urls" do
93
91
  block = "<link href='http://glogle.com/web.css' /> <link href='valid.css' />"
94
- result = @block_parser.find_files(block)
92
+ result = @block_parser.send(:find_files, block)
95
93
  result.first.path.should == 'valid.css'
96
94
  end
97
95
 
98
96
  end
99
97
 
98
+ describe "#create_resource_file" do
99
+ it "returns nil if attributes not src or href" do
100
+ @block_parser.send(:create_resource_file, 'invalid', '1.jpg').should == nil
101
+ end
102
+ it "returns nil if file isn't css or js" do
103
+ @block_parser.send(:create_resource_file, 'href', '1.cssI').should == nil
104
+ @block_parser.send(:create_resource_file, 'src', '1.jsI').should == nil
105
+ end
106
+ it "returns correct ResourceFile if conditions met" do
107
+ @block_parser.send(:create_resource_file, 'href', '1.css').path.should == '1.css'
108
+ @block_parser.send(:create_resource_file, 'src', '1.js').path.should == '1.js'
109
+ end
110
+ end
111
+
100
112
  end
113
+
114
+
101
115
  end
@@ -5,31 +5,23 @@ 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.types.first.should == WebResourceBundler::ResourceFileType::CSS
8
+ f.type.should == WebResourceBundler::ResourceFileType::CSS
9
9
  end
10
10
  end
11
11
  describe "#new_js_file" do
12
- it "creates new resource file of css type" do
12
+ it "creates new resource file of js type" 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.types.first.should == WebResourceBundler::ResourceFileType::JS
17
- end
18
- end
19
- describe "#new_mhtml_file" do
20
- it "creates new resource file of mhtml type" do
21
- f = WebResourceBundler::ResourceFile.new_mhtml_file('a', 'b')
22
- f.path.should == 'a'
23
- f.content.should == 'b'
24
- f.types.first.should == WebResourceBundler::ResourceFileType::MHTML
16
+ f.type.should == WebResourceBundler::ResourceFileType::JS
25
17
  end
26
18
  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')
19
+ describe "#new_mhtml_css_file" do
20
+ it "creates new resource file of mhtml css type" do
21
+ f = WebResourceBundler::ResourceFile.new_mhtml_css_file('a', 'b')
30
22
  f.path.should == 'a'
31
23
  f.content.should == 'b'
32
- f.types.should == [WebResourceBundler::ResourceFileType::CSS, WebResourceBundler::ResourceFileType::IE_CSS]
24
+ f.type.should == WebResourceBundler::ResourceFileType::MHTML_CSS
33
25
  end
34
26
  end
35
27
  describe "#clone" do
@@ -1,8 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "../spec_helper"))
2
2
  describe WebResourceBundler::FileManager do
3
- before(:each) do
4
- @settings = settings
5
- end
6
3
 
7
4
  def create_stub_file(name)
8
5
  File.open(File.join(@settings[:resource_dir], name), "w") do |file|
@@ -11,12 +8,13 @@ describe WebResourceBundler::FileManager do
11
8
  end
12
9
 
13
10
  before(:each) do
11
+ @settings = settings
14
12
  temp_dir = File.join(@settings[:resource_dir], 'temp')
15
13
  Dir.mkdir(temp_dir) unless File.exist?(temp_dir)
16
14
  @bundle_url = 'temp/bundle.dat'
17
15
  @bundle_path = File.join(@settings[:resource_dir], @bundle_url)
18
16
  create_stub_file(@bundle_url)
19
- @manager = FileManager.new(@settings[:resource_dir], @settings[:cache_dir])
17
+ @manager = FileManager.new(@settings)
20
18
  end
21
19
 
22
20
  after(:each) do
@@ -32,6 +30,16 @@ describe WebResourceBundler::FileManager do
32
30
  end
33
31
  end
34
32
 
33
+ describe "#set_settings" do
34
+ it "should set new settings in file manager" do
35
+ @manager.resource_dir.should == @settings[:resource_dir]
36
+ @manager.cache_dir.should == @settings[:cache_dir]
37
+ @manager.set_settings({:resource_dir => 'A', :cache_dir => 'B'})
38
+ @manager.resource_dir.should == 'A'
39
+ @manager.cache_dir.should == 'B'
40
+ end
41
+ end
42
+
35
43
  describe "#get_content" do
36
44
  it "reads file and returns its content" do
37
45
  @manager.get_content(@bundle_url).should == 'hi there'
@@ -4,11 +4,11 @@ describe WebResourceBundler::Filters::BundleFilter::Filter do
4
4
  clean_cache_dir
5
5
  @settings = settings
6
6
  @bundle_settings = bundle_settings
7
- @filter = Filters::BundleFilter::Filter.new(@bundle_settings, FileManager.new(@settings[:resource_dir], @settings[:cache_dir]))
7
+ @filter = Filters::BundleFilter::Filter.new(@bundle_settings, FileManager.new(@settings))
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[:protocol], @bundle_settings[:domain]]
11
+ items = [@block_data.files.select{|f| f.type[:ext] == 'css'}.map {|f| f.path}.sort] + [@bundle_settings[:protocol], @bundle_settings[:domain]]
12
12
  items += @bundle_settings[:md5_additional_data] if @bundle_settings[:md5_additional_data]
13
13
  @css_md5_value = Digest::MD5.hexdigest(items.flatten.join('|'))
14
14
  @css_bundle_file = File.join(@settings[:cache_dir], [css_type[:name] + '_' + @css_md5_value, 'en', css_type[:ext]].join('.'))
@@ -21,20 +21,20 @@ describe WebResourceBundler::Filters::BundleFilter::Filter do
21
21
  describe "#apply" do
22
22
  it "bundles each block_data resources in single file" do
23
23
  @filter.apply!(@block_data)
24
- @block_data.styles.first.path.should == @css_bundle_file
24
+ @block_data.files.select{|f| f.type[:ext] == 'css'}.first.path.should == @css_bundle_file
25
25
  @block_data.scripts.first.path.should == @js_bundle_file
26
26
  end
27
27
  end
28
28
 
29
29
  describe "#get_md5" do
30
30
  it "returns md5 from sorted filepaths and another additional data" do
31
- @filter.get_md5(@block_data.styles).should == @css_md5_value
31
+ @filter.send(:get_md5, @block_data.files.select{|f| f.type[:ext] == 'css'}).should == @css_md5_value
32
32
  end
33
33
  end
34
34
 
35
35
  describe "#bundle_filepath" do
36
36
  it "returns filename of bundle constructed from passed files" do
37
- @filter.bundle_filepath(WebResourceBundler::ResourceFileType::CSS, @block_data.styles).should == @css_bundle_file
37
+ @filter.send(:bundle_filepath, WebResourceBundler::ResourceFileType::CSS, @block_data.files.select{|f| f.type[:ext] == 'css'}).should == @css_bundle_file
38
38
  end
39
39
  end
40
40
 
@@ -3,7 +3,7 @@ require 'digest/md5'
3
3
  describe WebResourceBundler::Filters::BundleFilter::ResourcePackager do
4
4
  before(:each) do
5
5
  @settings = settings
6
- file_manager = FileManager.new(@settings[:resource_dir], @settings[:cache_dir])
6
+ file_manager = FileManager.new(@settings)
7
7
  @file_packager = Filters::BundleFilter::ResourcePackager.new(@settings, file_manager)
8
8
  @file_paths = styles.map do |url|
9
9
  File.join(@settings[:resource_dir], url)
@@ -13,12 +13,25 @@ describe WebResourceBundler::Filters::BundleFilter::ResourcePackager do
13
13
  describe "#extract_imported_files!" do
14
14
  it "returns array of imported css files" do
15
15
  content = "@import 'import/first.css';\n@import 'import/second.css';"
16
- imported_files = @file_packager.extract_imported_files!(content, 'styles/base.css')
16
+ imported_files = @file_packager.send(:extract_imported_files!, content, 'styles/base.css')
17
17
  content.should == "\n"
18
18
  imported_files.should == ['styles/import/first.css', 'styles/import/second.css']
19
19
  end
20
20
  end
21
21
 
22
+ describe "#build_imported_files" do
23
+ it "should return css resource files" do
24
+ files = @file_packager.send(:build_imported_files, styles)
25
+ files.each do |file|
26
+ styles.include?(file.path).should be_true
27
+ end
28
+ end
29
+ it "should return empty array if paths are not css" do
30
+ files = @file_packager.send(:build_imported_files, scripts)
31
+ files.should be_empty
32
+ end
33
+ end
34
+
22
35
  describe "#bundle_files" do
23
36
  it "throws ResourceNotFoundError exception if one of imported files not found" do
24
37
  #creating file with content with imported unexistent files