font_assets 0.1.12 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6ca8b6811004929e8b01e4db352fe83a52c62b5
4
+ data.tar.gz: f36a914fcf8032afd8af38c9710bc0fb2eac698e
5
+ SHA512:
6
+ metadata.gz: 3937a6ffd6bee8671c8da274db6aa829e146274ef10903adaece6be04691b41e668ae7c4ee98ca162ed51f1a7f5c61826a1228e8e0710cc84906c927e59ca1cc
7
+ data.tar.gz: ea058403dbcaa41b4f0b3f624994833de75e6d6f4d9457862b0bd72e8e183643c26819855024276f5011062e22092a98beb597e14409fa61320a7c536a13506e
@@ -0,0 +1 @@
1
+ 2.3.1
data/README.md CHANGED
@@ -10,6 +10,11 @@ two things:
10
10
  In addition, it will also respond to the pre-flight OPTIONS requests made by
11
11
  supporting browsers (Firefox).
12
12
 
13
+ Important
14
+ ---------
15
+
16
+ This gem only works if you are **NOT** precompiling your assets in production, because it requires asset requests to go through the Rails app, and if you precompile your assets then you are most likely serving them through a web server like NGINX. If this is the case, don't use this gem but instead look into configuring the CORS headers for font requests. For example, here is a way to [do that in NGINX](http://enable-cors.org/server_nginx.html).
17
+
13
18
  Install
14
19
  -------
15
20
 
@@ -3,6 +3,7 @@ require 'font_assets/mime_types'
3
3
 
4
4
  module FontAssets
5
5
  class Middleware
6
+ attr_reader :origin, :options, :mime_types
6
7
 
7
8
  def initialize(app, origin, options={})
8
9
  @app = app
@@ -11,73 +12,90 @@ module FontAssets
11
12
  @mime_types = FontAssets::MimeTypes.new(Rack::Mime::MIME_TYPES)
12
13
  end
13
14
 
14
- def access_control_headers
15
- {
16
- "Access-Control-Allow-Origin" => origin,
17
- "Access-Control-Allow-Methods" => "GET",
18
- "Access-Control-Allow-Headers" => "x-requested-with",
19
- "Access-Control-Max-Age" => "3628800"
20
- }
21
- end
22
-
23
15
  def call(env)
24
- @ssl_request = Rack::Request.new(env).scheme == "https"
25
- # intercept the "preflight" request
26
- if env["REQUEST_METHOD"] == "OPTIONS"
27
- return [200, access_control_headers, []]
28
- else
29
- code, headers, body = @app.call(env)
30
- set_headers! headers, body, env["PATH_INFO"]
31
- [code, headers, body]
16
+ FontAssetsRequest.new(self, env).do_request do
17
+ @app.call(env)
32
18
  end
33
19
  end
34
20
 
21
+ class FontAssetsRequest
22
+ def initialize(middleware, env)
23
+ @middleware = middleware
24
+ @request = Rack::Request.new(env)
25
+ end
26
+
27
+ def do_request
28
+ if font_asset?
29
+ if @request.options?
30
+ return [200, access_control_headers, []]
31
+ else
32
+ code, headers, body = yield
35
33
 
36
- private
34
+ headers.merge!(access_control_headers)
35
+ headers.merge!('Content-Type' => mime_type) if headers['Content-Type']
37
36
 
38
- def origin
39
- if !wildcard_origin? and allow_ssl? and ssl_request?
40
- uri = URI(@origin)
41
- uri.scheme = "https"
42
- uri.to_s
43
- else
44
- @origin
37
+ [code, headers, body]
38
+ end
39
+ else
40
+ yield
41
+ end
45
42
  end
46
- end
47
43
 
48
- def wildcard_origin?
49
- @origin == '*'
50
- end
44
+ private
45
+ def access_control_headers
46
+ {
47
+ "Access-Control-Allow-Origin" => origin,
48
+ "Access-Control-Allow-Methods" => "GET",
49
+ "Access-Control-Allow-Headers" => "x-requested-with",
50
+ "Access-Control-Max-Age" => "3628800"
51
+ }
52
+ end
51
53
 
52
- def ssl_request?
53
- @ssl_request
54
- end
54
+ def origin
55
+ if !wildcard_origin? and allow_ssl? and ssl_request?
56
+ uri = URI(@middleware.origin)
57
+ uri.scheme = "https"
58
+ uri.to_s
59
+ else
60
+ @middleware.origin
61
+ end
62
+ end
55
63
 
56
- def allow_ssl?
57
- @options[:allow_ssl]
58
- end
64
+ def wildcard_origin?
65
+ @middleware.origin == '*'
66
+ end
59
67
 
60
- def extension(path)
61
- if path.nil? || path.length == 0
62
- nil
63
- else
64
- "." + path.split("?").first.split(".").last
68
+ def ssl_request?
69
+ @request.scheme == "https"
65
70
  end
66
- end
67
71
 
68
- def font_asset?(path)
69
- @mime_types.font? extension(path)
70
- end
72
+ def allow_ssl?
73
+ @middleware.options[:allow_ssl]
74
+ end
71
75
 
72
- def set_headers!(headers, body, path)
73
- if ext = extension(path) and font_asset?(ext)
74
- headers.merge!(access_control_headers)
75
- headers.merge!('Content-Type' => mime_type(ext)) if headers['Content-Type']
76
+ def path
77
+ @request.path_info
78
+ end
79
+
80
+ def font_asset?
81
+ mime_types.font? extension
76
82
  end
77
- end
78
83
 
79
- def mime_type(extension)
80
- @mime_types[extension]
84
+ def mime_type
85
+ mime_types[extension]
86
+ end
87
+
88
+ def mime_types
89
+ @middleware.mime_types
90
+ end
91
+
92
+ def extension
93
+ if path.nil? || path.length == 0
94
+ nil
95
+ else
96
+ "." + path.split("?").first.split(".").last
97
+ end
98
+ end
81
99
  end
82
100
  end
83
101
  end
@@ -9,9 +9,9 @@ module FontAssets
9
9
  config.font_assets.options ||= { allow_ssl: true }
10
10
 
11
11
  insert_target = if defined?(ActionDispatch::Static)
12
- 'ActionDispatch::Static'
12
+ ActionDispatch::Static
13
13
  else
14
- 'Rack::Runtime'
14
+ Rack::Runtime
15
15
  end
16
16
 
17
17
  app.middleware.insert_before insert_target, FontAssets::Middleware, config.font_assets.origin, config.font_assets.options
@@ -1,3 +1,3 @@
1
1
  module FontAssets
2
- VERSION = "0.1.12"
2
+ VERSION = "0.1.14"
3
3
  end
@@ -138,24 +138,41 @@ describe FontAssets::Middleware do
138
138
 
139
139
  context 'for OPTIONS requests' do
140
140
  let(:app) { load_app 'http://test.options' }
141
- let(:response) { request app, '/test.ttf', :method => 'OPTIONS' }
142
141
 
143
- context 'the response headers' do
144
- subject { response[1] }
142
+ context 'to font assets' do
143
+ let(:response) { request app, '/test.ttf', :method => 'OPTIONS' }
145
144
 
146
- its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" }
147
- its(["Access-Control-Max-Age"]) { should == "3628800" }
148
- its(['Access-Control-Allow-Methods']) { should == 'GET' }
149
- its(['Access-Control-Allow-Origin']) { should == 'http://test.options' }
145
+ context 'the response headers' do
146
+ subject { response[1] }
150
147
 
151
- it 'should not contain a Content-Type' do
152
- subject['Content-Type'].should be_nil
148
+ its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" }
149
+ its(["Access-Control-Max-Age"]) { should == "3628800" }
150
+ its(['Access-Control-Allow-Methods']) { should == 'GET' }
151
+ its(['Access-Control-Allow-Origin']) { should == 'http://test.options' }
152
+
153
+ it 'should not contain a Content-Type' do
154
+ subject['Content-Type'].should be_nil
155
+ end
156
+ end
157
+
158
+ context 'the response body' do
159
+ subject { response[2] }
160
+ it { should be_empty }
153
161
  end
154
162
  end
155
163
 
156
- context 'the response body' do
157
- subject { response[2] }
158
- it { should be_empty }
164
+ context 'to non-font assets' do
165
+ let(:response) { request app, '/test', method: 'OPTIONS' }
166
+
167
+ context 'the response headers' do
168
+ subject { response[1] }
169
+
170
+ its(["Access-Control-Allow-Headers"]) { should be_nil }
171
+ its(["Access-Control-Max-Age"]) { should be_nil }
172
+ its(['Access-Control-Allow-Methods']) { should be_nil }
173
+ its(['Access-Control-Allow-Origin']) { should be_nil }
174
+ its(['Content-Type']) { should == 'text/plain' }
175
+ end
159
176
  end
160
177
  end
161
178
 
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: font_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
5
- prerelease:
4
+ version: 0.1.14
6
5
  platform: ruby
7
6
  authors:
8
7
  - Eric Allam
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2016-09-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rack
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '2.0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '2.0'
46
41
  description: Improve font serving in Rails 3.1
@@ -50,8 +45,9 @@ executables: []
50
45
  extensions: []
51
46
  extra_rdoc_files: []
52
47
  files:
53
- - .gitignore
54
- - .rspec
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".ruby-version"
55
51
  - CHANGELOG.md
56
52
  - Gemfile
57
53
  - README.md
@@ -67,27 +63,26 @@ files:
67
63
  - spec/spec_helper.rb
68
64
  homepage: https://github.com/rubymaverick/font_assets
69
65
  licenses: []
66
+ metadata: {}
70
67
  post_install_message:
71
68
  rdoc_options: []
72
69
  require_paths:
73
70
  - lib
74
71
  required_ruby_version: !ruby/object:Gem::Requirement
75
- none: false
76
72
  requirements:
77
- - - ! '>='
73
+ - - ">="
78
74
  - !ruby/object:Gem::Version
79
75
  version: '0'
80
76
  required_rubygems_version: !ruby/object:Gem::Requirement
81
- none: false
82
77
  requirements:
83
- - - ! '>='
78
+ - - ">="
84
79
  - !ruby/object:Gem::Version
85
80
  version: '0'
86
81
  requirements: []
87
82
  rubyforge_project: font_assets
88
- rubygems_version: 1.8.23
83
+ rubygems_version: 2.5.1
89
84
  signing_key:
90
- specification_version: 3
85
+ specification_version: 4
91
86
  summary: Improve font serving in Rails 3.1
92
87
  test_files:
93
88
  - spec/middleware_spec.rb