sinatra-static-assets 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,10 @@ require 'sinatra/base'
3
3
  module Sinatra
4
4
  module StaticAssets
5
5
  module Helpers
6
+
7
+ @@asset_timestamps_cache = {}
8
+ @@asset_mutex ||= Mutex.new
9
+
6
10
  # In HTML <link> and <img> tags have no end tag.
7
11
  # In XHTML, on the contrary, these tags must be properly closed.
8
12
  #
@@ -40,6 +44,35 @@ module Sinatra
40
44
  def url_for(addr, absolute = false)
41
45
  uri(addr, absolute == :full ? true : false, true)
42
46
  end
47
+
48
+ def is_uri?(path)
49
+ path =~ %r{^[-a-z]+://|^cid:|^//}
50
+ end
51
+
52
+ def asset_url_for(source)
53
+ url = url_for(source)
54
+ return url if is_uri?(source)
55
+
56
+ timestamp = asset_timestamp(source)
57
+ url += "?#{timestamp}" unless timestamp.empty?
58
+ return url
59
+ end
60
+
61
+ def asset_timestamp(source)
62
+ if timestamp = @@asset_timestamps_cache[source]
63
+ return timestamp
64
+ else
65
+ path = asset_file_path(source)
66
+ timestamp = File.exists?(path) ? File.mtime(path).to_i.to_s : ''
67
+ @@asset_mutex.synchronize {
68
+ @@asset_timestamps_cache[source] = timestamp
69
+ }
70
+ end
71
+ end
72
+
73
+ def asset_file_path(path)
74
+ File.join(settings.root, 'public', path)
75
+ end
43
76
 
44
77
  def tag(name, local_options = {})
45
78
  start_tag = "<#{name}#{tag_options(local_options) if local_options}"
@@ -63,12 +96,12 @@ module Sinatra
63
96
  def stylesheet_tag(source, options = {})
64
97
  tag("link", { :type => "text/css",
65
98
  :charset => "utf-8", :media => "screen", :rel => "stylesheet",
66
- :href => url_for(source) }.merge(options))
99
+ :href => asset_url_for(source) }.merge(options))
67
100
  end
68
101
 
69
102
  def javascript_tag(source, options = {})
70
103
  tag("script", { :type => "text/javascript", :charset => "utf-8",
71
- :src => url_for(source) }.merge(options)) do
104
+ :src => asset_url_for(source) }.merge(options)) do
72
105
  end
73
106
  end
74
107
 
@@ -4,7 +4,7 @@ $:.push File.expand_path("../lib", __FILE__)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "sinatra-static-assets"
7
- s.version = "1.0.2"
7
+ s.version = "1.0.3"
8
8
  s.authors = ["Włodek Bzyl"]
9
9
  s.email = ["matwb@ug.edu.pl"]
10
10
  s.homepage = ""
@@ -0,0 +1,2 @@
1
+ /* Dummy CSS file for tests */
2
+ html { background: white }
@@ -1,6 +1,3 @@
1
- path = File.expand_path("../lib" + File.dirname(__FILE__))
2
- $:.unshift(path) unless $:.include?(path)
3
-
4
1
  require 'rubygems'
5
2
 
6
3
  require 'sinatra'
@@ -15,6 +12,14 @@ get "/url_for" do
15
12
  EOD
16
13
  end
17
14
 
15
+ get "/asset_url_for" do
16
+ content_type "text/plain"
17
+ <<"EOD"
18
+ #{asset_url_for("test.css")}
19
+ #{asset_url_for("http://example.org/test.css")}
20
+ EOD
21
+ end
22
+
18
23
  get "/image_tag" do
19
24
  content_type "text/plain"
20
25
  <<"EOD"
@@ -25,7 +30,7 @@ end
25
30
  get "/stylesheet_link_tag" do
26
31
  content_type "text/plain"
27
32
  <<"EOD"
28
- #{stylesheet_link_tag("/stylesheets/winter.css", "/stylesheets/summer.css", :media => "projection")}
33
+ #{stylesheet_link_tag("/stylesheets/winter.css", "/stylesheets/summer.css", "test.css", :media => "projection")}
29
34
  EOD
30
35
  end
31
36
 
@@ -1,6 +1,3 @@
1
- path = File.expand_path("../lib" + File.dirname(__FILE__))
2
- $:.unshift(path) unless $:.include?(path)
3
-
4
1
  require 'rubygems'
5
2
  require 'sinatra/base'
6
3
 
@@ -1,6 +1,8 @@
1
+ require './test/test_helper'
1
2
  require './test/sinatra_app'
2
3
  require 'test/unit'
3
4
  require 'rack/test'
5
+ require 'sinatra/static_assets'
4
6
 
5
7
  set :environment, :test
6
8
 
@@ -18,6 +20,15 @@ class SintraStaticAssetsTest < Test::Unit::TestCase
18
20
  /bar/
19
21
  /bar/foo
20
22
  http://example.org/bar/foo
23
+ EOD
24
+ end
25
+
26
+ def test_asset_url_for_returns_timestamp_only_on_existing_paths
27
+ get '/asset_url_for', {}, 'SCRIPT_NAME' => '/bar'
28
+ assert last_response.ok?
29
+ assert_equal last_response.body, <<EOD
30
+ /bar/test.css?#{asset_timestamp('test.css')}
31
+ http://example.org/test.css
21
32
  EOD
22
33
  end
23
34
 
@@ -35,6 +46,7 @@ EOD
35
46
  assert_equal last_response.body, <<EOD
36
47
  <link charset="utf-8" href="/bar/stylesheets/winter.css" media="projection" rel="stylesheet" type="text/css">
37
48
  <link charset="utf-8" href="/bar/stylesheets/summer.css" media="projection" rel="stylesheet" type="text/css">
49
+ <link charset="utf-8" href="/bar/test.css?#{asset_timestamp('test.css')}" media="projection" rel="stylesheet" type="text/css">
38
50
  EOD
39
51
  end
40
52
 
@@ -1,3 +1,4 @@
1
+ require './test/test_helper'
1
2
  require './test/sinatra_baseapp'
2
3
  require 'test/unit'
3
4
  require 'rack/test'
@@ -0,0 +1,6 @@
1
+ path = File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
2
+ $:.unshift(path) unless $:.include?(path)
3
+
4
+ def asset_timestamp(source)
5
+ File.mtime("./test/public/#{source}").to_i.to_s
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-static-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-14 00:00:00.000000000Z
12
+ date: 2012-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &14567420 !ruby/object:Gem::Requirement
16
+ requirement: &16409220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14567420
24
+ version_requirements: *16409220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack
27
- requirement: &14566340 !ruby/object:Gem::Requirement
27
+ requirement: &16408580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14566340
35
+ version_requirements: *16408580
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack-test
38
- requirement: &14565080 !ruby/object:Gem::Requirement
38
+ requirement: &16407840 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *14565080
46
+ version_requirements: *16407840
47
47
  description: ! "This Sinatra extensions provides following helper methods:\n -
48
48
  image_tag\n - stylesheet_link_tag\n - javascript_script_tag"
49
49
  email:
@@ -97,10 +97,12 @@ files:
97
97
  - examples/winter/winter.rb
98
98
  - lib/sinatra/static_assets.rb
99
99
  - sinatra-static-assets.gemspec
100
+ - test/public/test.css
100
101
  - test/sinatra_app.rb
101
102
  - test/sinatra_baseapp.rb
102
103
  - test/sinatra_static_assets_test.rb
103
104
  - test/sinatra_static_assets_xhtml_test.rb
105
+ - test/test_helper.rb
104
106
  homepage: ''
105
107
  licenses: []
106
108
  post_install_message:
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  version: '0'
122
124
  requirements: []
123
125
  rubyforge_project: sinatra-static-assets
124
- rubygems_version: 1.8.10
126
+ rubygems_version: 1.8.15
125
127
  signing_key:
126
128
  specification_version: 3
127
129
  summary: A Sinatra extension.