premailer-rails 1.5.0 → 1.5.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: faaccc100fbcc92e0c79ee426a2a3b4b2ae16ecd
4
- data.tar.gz: 4e183a0856f2bcd01e5955efc1d7bd750e19edf4
3
+ metadata.gz: c14b05b4d57365b47366c6c56d7080ed7610bb0e
4
+ data.tar.gz: 3bb5ba65597cc2fe6d40fc053785683ad0e08d69
5
5
  SHA512:
6
- metadata.gz: 83535e5fd7f0b6540c8d9e18769a6b3e0dd0112559f56fa33dd279bbd399e281aee0ecbf3f885aea3bad3e529f2eca0026ef8c1092c6b70844c214caec7ddb6d
7
- data.tar.gz: e195868f2495abe7d4a8b188f389d0a84ce9870d1b66e48793b62b8c72722ed86d3bf73d2fce29d8869c63f692677b035423732e2befe2ea1e98570c95dad282
6
+ metadata.gz: af255aab35b0a2ebbbe9fc554d45bbf190878531ba2faef489cc88dcc4d1b40031713da7a21f8cdbbc1cdc49fdd8e96ca32ac2a3f439f40af4602fc3874c4bbc
7
+ data.tar.gz: 7aed5714d084a540a096da8df6606894d68616dbcfe1de3fb9bdcaee3eeb61624c2f1377064a850c10c6e454926ef8fda3815fc8aaca635f1398b5bd4e8c2046
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.5.1
4
+
5
+ - Prefer precompiled assets over asset pipeline
6
+
7
+ - Improve construction of file URL when requesting from CDN
8
+
9
+ - No longer use open-uri
10
+
11
+ - Remove gzip unzipping after requesting file
12
+
3
13
  ## v1.5.0
4
14
 
5
15
  - No longer support ruby 1.8
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.0
1
+ 1.5.1
@@ -1,6 +1,3 @@
1
- require 'open-uri'
2
- require 'zlib'
3
-
4
1
  class Premailer
5
2
  module Rails
6
3
  module CSSHelper
@@ -11,8 +8,8 @@ class Premailer
11
8
 
12
9
  STRATEGIES = [
13
10
  CSSLoaders::CacheLoader,
14
- CSSLoaders::AssetPipelineLoader,
15
- CSSLoaders::FileSystemLoader
11
+ CSSLoaders::FileSystemLoader,
12
+ CSSLoaders::AssetPipelineLoader
16
13
  ]
17
14
 
18
15
  # Returns all linked CSS files concatenated as string.
@@ -1,3 +1,6 @@
1
+ require 'uri'
2
+ require 'zlib'
3
+
1
4
  class Premailer
2
5
  module Rails
3
6
  module CSSLoaders
@@ -22,7 +25,7 @@ class Premailer
22
25
  if asset = ::Rails.application.assets.find_asset(file)
23
26
  asset.to_s
24
27
  else
25
- request_and_unzip(file)
28
+ Net::HTTP.get(uri_for_path(path))
26
29
  end
27
30
  end
28
31
  end
@@ -37,19 +40,13 @@ class Premailer
37
40
  .sub(/-\h{32}\.css$/, '.css')
38
41
  end
39
42
 
40
- def request_and_unzip(file)
41
- url = [
42
- ::Rails.configuration.action_controller.asset_host,
43
- ::Rails.configuration.assets.prefix.sub(/^\//, ''),
44
- ::Rails.configuration.assets.digests[file]
45
- ].join('/')
46
- response = Kernel.open(url)
47
-
48
- begin
49
- Zlib::GzipReader.new(response).read
50
- rescue Zlib::GzipFile::Error, Zlib::Error
51
- response.rewind
52
- response.read
43
+ def uri_for_path(path)
44
+ URI(path).tap do |uri|
45
+ scheme, host =
46
+ ::Rails.configuration.action_controller.asset_host.split(%r{:?//})
47
+ scheme = 'http' if scheme.blank?
48
+ uri.scheme ||= scheme
49
+ uri.host ||= host
53
50
  end
54
51
  end
55
52
  end
@@ -59,7 +56,8 @@ class Premailer
59
56
  extend self
60
57
 
61
58
  def load(path)
62
- File.read("#{::Rails.root}/public#{path}")
59
+ file_path = "#{::Rails.root}/public#{path}"
60
+ File.read(file_path) if File.exist?(file_path)
63
61
  end
64
62
  end
65
63
  end
@@ -12,6 +12,11 @@ describe Premailer::Rails::CSSHelper do
12
12
  Premailer::Rails::CSSHelper.css_for_doc(doc)
13
13
  end
14
14
 
15
+ def expect_file(path, content=nil)
16
+ File.stubs(:exist?).with(path).returns(true)
17
+ File.expects(:read).with(path).returns(content)
18
+ end
19
+
15
20
  describe '#css_for_doc' do
16
21
  let(:html) { Fixtures::HTML.with_css_links(*files) }
17
22
  let(:doc) { Nokogiri(html) }
@@ -37,7 +42,7 @@ describe Premailer::Rails::CSSHelper do
37
42
  describe '#load_css' do
38
43
  context 'when path is a url' do
39
44
  it 'should load the CSS at the local path' do
40
- File.expects(:read).with('RAILS_ROOT/public/stylesheets/base.css')
45
+ expect_file('RAILS_ROOT/public/stylesheets/base.css')
41
46
 
42
47
  load_css('http://example.com/stylesheets/base.css?test')
43
48
  end
@@ -45,8 +50,7 @@ describe Premailer::Rails::CSSHelper do
45
50
 
46
51
  context 'when path is a relative url' do
47
52
  it 'should load the CSS at the local path' do
48
- File.expects(:read).with('RAILS_ROOT/public/stylesheets/base.css')
49
-
53
+ expect_file('RAILS_ROOT/public/stylesheets/base.css')
50
54
  load_css('/stylesheets/base.css?test')
51
55
  end
52
56
  end
@@ -67,13 +71,11 @@ describe Premailer::Rails::CSSHelper do
67
71
  cache =
68
72
  Premailer::Rails::CSSHelper.send(:instance_variable_get, '@cache')
69
73
  cache['/stylesheets/base.css'] = 'cached content of base.css'
70
- File.expects(:read)
71
- .with('RAILS_ROOT/public/stylesheets/base.css')
72
- .returns('new content of base.css')
74
+ content = 'new content of base.css'
75
+ expect_file('RAILS_ROOT/public/stylesheets/base.css', content)
73
76
  Rails.env.stubs(:development?).returns(true)
74
77
 
75
- load_css('http://example.com/stylesheets/base.css')
76
- .should == 'new content of base.css'
78
+ load_css('http://example.com/stylesheets/base.css').should == content
77
79
  end
78
80
  end
79
81
 
@@ -87,6 +89,15 @@ describe Premailer::Rails::CSSHelper do
87
89
  )
88
90
  }
89
91
 
92
+ context 'and a precompiled file exists' do
93
+ it 'should return that file' do
94
+ path = '/assets/email-digest.css'
95
+ content = 'read from file'
96
+ expect_file("RAILS_ROOT/public#{path}", content)
97
+ load_css(path).should == content
98
+ end
99
+ end
100
+
90
101
  it 'should return the content of the file compiled by Rails' do
91
102
  Rails.application.assets
92
103
  .expects(:find_asset)
@@ -109,52 +120,44 @@ describe Premailer::Rails::CSSHelper do
109
120
  end
110
121
 
111
122
  context 'when asset can not be found' do
112
- before {
123
+ let(:response) { 'content of base.css' }
124
+ let(:path) { '/assets/base-089e35bd5d84297b8d31ad552e433275.css' }
125
+ let(:url) { "http://assets.example.com#{path}" }
126
+ let(:asset_host) { 'http://assets.example.com' }
127
+
128
+ before do
113
129
  Rails.application.assets.stubs(:find_asset).returns(nil)
114
130
  Rails.configuration.stubs(:action_controller).returns(
115
- stub(asset_host: 'http://example.com')
116
- )
117
- Rails.configuration.stubs(:assets).returns(
118
- stub(
119
- enabled: true,
120
- prefix: '/assets',
121
- digests: {
122
- 'base.css' => 'base-089e35bd5d84297b8d31ad552e433275.css'
123
- }
124
- )
131
+ stub(asset_host: asset_host)
125
132
  )
126
- }
127
- let(:string_io) { StringIO.new('content of base.css') }
128
- let(:url) {
129
- 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
130
- }
133
+ Net::HTTP.stubs(:get).with { |uri| uri.to_s == url }.returns(response)
134
+ end
131
135
 
132
136
  it 'should request the file' do
133
- Kernel.expects(:open).with(url).returns(string_io)
134
-
135
- load_css(
136
- 'http://example.com/assets/base.css'
137
- ).should == 'content of base.css'
137
+ load_css(url).should == 'content of base.css'
138
138
  end
139
139
 
140
- it 'should request the same file when path contains file fingerprint' do
141
- Kernel.expects(:open).with(url).returns(string_io)
140
+ context 'when file url does not include the host' do
141
+ it 'should request the file using the asset host as host' do
142
+ load_css(path).should == 'content of base.css'
143
+ end
144
+
145
+ context 'and the asset host uses protocol relative scheme' do
146
+ let(:asset_host) { '//assets.example.com' }
142
147
 
143
- load_css(
144
- 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
145
- ).should == 'content of base.css'
148
+ it 'should request the file using http as the scheme' do
149
+ load_css(path).should == 'content of base.css'
150
+ end
151
+ end
146
152
  end
147
- end
153
+ end
148
154
  end
149
155
 
150
156
  context 'when static stylesheets are used' do
151
157
  it 'should return the content of the static file' do
152
- File.expects(:read)
153
- .with('RAILS_ROOT/public/stylesheets/base.css')
154
- .returns('content of base.css')
155
-
156
- load_css('http://example.com/stylesheets/base.css')
157
- .should == 'content of base.css'
158
+ content = 'content of base.css'
159
+ expect_file('RAILS_ROOT/public/stylesheets/base.css', content)
160
+ load_css('http://example.com/stylesheets/base.css').should == content
158
161
  end
159
162
  end
160
163
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: premailer-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philipe Fatio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-31 00:00:00.000000000 Z
11
+ date: 2013-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: premailer