rack-gist 1.1.9 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.9
1
+ 1.2.0
@@ -3,14 +3,11 @@ require 'rest-client'
3
3
 
4
4
  module Rack
5
5
  class Gist
6
+ DefaultCacheTime = 3600
7
+
6
8
  def initialize(app, options = {})
7
9
  @app = app
8
- @options = {
9
- :jquery => true,
10
- :cache_time => 3600,
11
- :http_cache_time => 3600,
12
- :encoding => 'utf-8'
13
- }.merge(options)
10
+ @options = options
14
11
  end
15
12
 
16
13
  def call(env)
@@ -37,11 +34,11 @@ module Rack
37
34
  if swap_tags(doc)
38
35
  doc.at('head').add_child(css_html)
39
36
  doc.at('body').tap do |node|
40
- node.add_child(jquery_link) if @options[:jquery]
41
- node.add_child(jquery_helper)
37
+ node.add_child(jquery_link) if @options.fetch(:jquery, true)
38
+ node.add_child(jquery_helper) if @options.fetch(:helper, true)
42
39
  end
43
40
  end
44
- end.to_html(:encoding => @options[:encoding])
41
+ end.to_html(:encoding => @options.fetch(:encoding, 'utf-8'))
45
42
  end
46
43
 
47
44
  def stringify_body(body)
@@ -56,8 +53,16 @@ module Rack
56
53
  extras = true
57
54
  tag['src'].match(%r{gist\.github\.com/([a-f0-9]+)\.js(?:\?file=(.*))?}).tap do |match|
58
55
  id, file = match[1, 2]
59
- suffix, extra = file ? ["#file_#{file}", "rack-gist-file='#{file}'"] : ['', '']
60
- tag.swap("<p class='rack-gist' id='rack-gist-#{id}' gist-id='#{id}' #{extra}>Can't see this Gist? <a rel='nofollow' href='http://gist.github.com/#{id}#{suffix}'>View it on Github!</a></p>")
56
+ url = "/gist.github.com/#{id}"
57
+ if file
58
+ url += "/#{file}"
59
+ suffix = "#file_#{file}"
60
+ extra = %Q{rack-gist-file="#{file}"}
61
+ else
62
+ suffix = nil
63
+ extra = nil
64
+ end
65
+ tag.swap(%Q{<p class="rack-gist" id="rack-gist-#{id}"" gist-id="#{id}" rack-gist-url="#{url}.js" #{extra}>Can't see this Gist? <a rel="nofollow" href="http://gist.github.com/#{id}#{suffix}">View it on Github!</a></p>})
61
66
  end
62
67
  end
63
68
  extras
@@ -65,21 +70,27 @@ module Rack
65
70
 
66
71
  def serve_gist(env)
67
72
  gist_id, file = path(env).match(regex)[1,2]
68
- cache = @options[:cache]
69
- gist = (cache ? cache.fetch(cache_key(gist_id, file), :expires_in => @options[:cache_time]) { get_gist(gist_id, file) } : get_gist(gist_id, file)).to_s
73
+ gist = fetch_gist(gist_id, file).to_s
70
74
  headers = {
71
75
  'Content-Type' => 'application/javascript',
72
76
  'Content-Length' => Rack::Utils.bytesize(gist).to_s,
73
77
  'Vary' => 'Accept-Encoding'
74
78
  }
75
79
 
76
- if @options[:http_cache_time]
77
- headers['Cache-Control'] = "public, must-revalidate, max-age=#{@options[:http_cache_time]}"
78
- end
80
+ headers['Cache-Control'] = "public, must-revalidate, max-age=#{@options.fetch(:http_cache_time, DefaultCacheTime)}"
79
81
 
80
82
  [200, headers, [gist]]
81
83
  end
82
84
 
85
+ def fetch_gist(gist_id, file)
86
+ if cache = @options.fetch(:cache, false)
87
+ expires = @options.fetch(:cache_time, DefaultCacheTime)
88
+ cache.fetch(cache_key(gist_id, file), :expires_in => expires) { get_gist(gist_id, file) }
89
+ else
90
+ get_gist(gist_id, file)
91
+ end
92
+ end
93
+
83
94
  def get_gist(gist_id, file)
84
95
  gist = RestClient.get(gist_url(gist_id, file))
85
96
  gist = gist.split("\n").reject do |part|
@@ -115,7 +126,7 @@ module Rack
115
126
  end
116
127
 
117
128
  def jquery_link
118
- "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js'></script>\n"
129
+ "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>\n"
119
130
  end
120
131
 
121
132
  def jquery_helper
@@ -124,13 +135,8 @@ module Rack
124
135
  //<![CDATA[
125
136
  $(document).ready(function() {
126
137
  $('.rack-gist').each(function() {
127
- var url = '/gist.github.com/' + $(this).attr('gist-id');
128
- var file = false;
129
- if (file = $(this).attr('rack-gist-file')) {
130
- url += '/' + file;
131
- }
132
138
  $.ajax({
133
- url: url + '.js',
139
+ url: $(this).attr('rack-gist-url'),
134
140
  dataType: 'script',
135
141
  cache: true
136
142
  });
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{rack-gist}
8
- s.version = "1.1.9"
7
+ s.name = "rack-gist"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Huckstep"]
12
- s.date = %q{2011-10-30}
13
- s.description = %q{Load gists in the background. KTHXBYE!}
14
- s.email = %q{darkhelmet@darkhelmetlive.com}
12
+ s.date = "2012-05-05"
13
+ s.description = "Load gists in the background. KTHXBYE!"
14
+ s.email = "darkhelmet@darkhelmetlive.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.rdoc"
@@ -39,10 +39,10 @@ Gem::Specification.new do |s|
39
39
  "spec/spec.opts",
40
40
  "spec/spec_helper.rb"
41
41
  ]
42
- s.homepage = %q{http://github.com/darkhelmet/rack-gist}
42
+ s.homepage = "http://github.com/darkhelmet/rack-gist"
43
43
  s.require_paths = ["lib"]
44
- s.rubygems_version = %q{1.6.2}
45
- s.summary = %q{Asynchronous Github Gists. All with rack.}
44
+ s.rubygems_version = "1.8.18"
45
+ s.summary = "Asynchronous Github Gists. All with rack."
46
46
 
47
47
  if s.respond_to? :specification_version then
48
48
  s.specification_version = 3
@@ -66,7 +66,7 @@ describe "Rack::Gist" do
66
66
  status, headers, body = a.call(mock_env('/partial'))
67
67
  status.should == 200
68
68
  headers['Content-Type'].should == 'text/html'
69
- pbody(body).should have_html_tag('p').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist', 'rack-gist-file' => 'example.pig')
69
+ pbody(body).should have_html_tag('p').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist', 'rack-gist-file' => 'example.pig', 'rack-gist-url' => '/gist.github.com/348301/example.pig.js')
70
70
  end
71
71
  end
72
72
 
@@ -115,6 +115,15 @@ describe "Rack::Gist" do
115
115
  end
116
116
  end
117
117
 
118
+ it "shouldn't include the jquery helper if told not to" do
119
+ middleware(:jquery => false, :helper => false).tap do |a|
120
+ status, headers, body = a.call(mock_env)
121
+ status.should == 200
122
+ headers['Content-Type'].should == 'text/html'
123
+ pbody(body).should_not have_html_tag('script').containing('CDATA')
124
+ end
125
+ end
126
+
118
127
  it "shouldn't include the jquery helper if no gist is present" do
119
128
  middleware.tap do |a|
120
129
  status, headers, body = a.call(mock_env)
@@ -129,7 +138,7 @@ describe "Rack::Gist" do
129
138
  status, headers, body = a.call(mock_env)
130
139
  status.should == 200
131
140
  headers['Content-Type'].should == 'text/html'
132
- headers['Content-Length'].should == '1144'
141
+ headers['Content-Length'].should == '992'
133
142
  end
134
143
  end
135
144
 
@@ -208,11 +217,11 @@ describe "Rack::Gist" do
208
217
  end
209
218
  end
210
219
 
211
- it 'should be able to disable caching by setting http_cache_time to nil' do
212
- middleware(:http_cache_time => nil).tap do |a|
220
+ it 'should be able to disable caching by setting http_cache_time to 0' do
221
+ middleware(:http_cache_time => 0).tap do |a|
213
222
  status, headers, body = a.call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js"))
214
223
  headers['Vary'].should == 'Accept-Encoding'
215
- headers['Cache-Control'].should be_nil
224
+ headers['Cache-Control'].should == "public, must-revalidate, max-age=0"
216
225
  end
217
226
  end
218
227
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-gist
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
9
- - 9
10
- version: 1.1.9
8
+ - 2
9
+ - 0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Huckstep
@@ -15,12 +15,13 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-30 00:00:00 -06:00
19
- default_executable:
18
+ date: 2012-05-05 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ name: nokogiri
22
23
  type: :runtime
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
25
  none: false
25
26
  requirements:
26
27
  - - ~>
@@ -30,12 +31,12 @@ dependencies:
30
31
  - 1
31
32
  - 4
32
33
  version: "1.4"
33
- version_requirements: *id001
34
- name: nokogiri
35
- prerelease: false
34
+ requirement: *id001
36
35
  - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ name: rest-client
37
38
  type: :runtime
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
40
  none: false
40
41
  requirements:
41
42
  - - ~>
@@ -45,12 +46,12 @@ dependencies:
45
46
  - 1
46
47
  - 6
47
48
  version: "1.6"
48
- version_requirements: *id002
49
- name: rest-client
50
- prerelease: false
49
+ requirement: *id002
51
50
  - !ruby/object:Gem::Dependency
51
+ prerelease: false
52
+ name: jeweler
52
53
  type: :development
53
- requirement: &id003 !ruby/object:Gem::Requirement
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
55
  none: false
55
56
  requirements:
56
57
  - - ~>
@@ -60,12 +61,12 @@ dependencies:
60
61
  - 1
61
62
  - 6
62
63
  version: "1.6"
63
- version_requirements: *id003
64
- name: jeweler
65
- prerelease: false
64
+ requirement: *id003
66
65
  - !ruby/object:Gem::Dependency
66
+ prerelease: false
67
+ name: fakeweb
67
68
  type: :development
68
- requirement: &id004 !ruby/object:Gem::Requirement
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
69
70
  none: false
70
71
  requirements:
71
72
  - - ~>
@@ -76,12 +77,12 @@ dependencies:
76
77
  - 2
77
78
  - 8
78
79
  version: 1.2.8
79
- version_requirements: *id004
80
- name: fakeweb
81
- prerelease: false
80
+ requirement: *id004
82
81
  - !ruby/object:Gem::Dependency
82
+ prerelease: false
83
+ name: rspec
83
84
  type: :development
84
- requirement: &id005 !ruby/object:Gem::Requirement
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
85
86
  none: false
86
87
  requirements:
87
88
  - - ~>
@@ -92,12 +93,12 @@ dependencies:
92
93
  - 3
93
94
  - 0
94
95
  version: 1.3.0
95
- version_requirements: *id005
96
- name: rspec
97
- prerelease: false
96
+ requirement: *id005
98
97
  - !ruby/object:Gem::Dependency
98
+ prerelease: false
99
+ name: yard
99
100
  type: :development
100
- requirement: &id006 !ruby/object:Gem::Requirement
101
+ version_requirements: &id006 !ruby/object:Gem::Requirement
101
102
  none: false
102
103
  requirements:
103
104
  - - ~>
@@ -108,12 +109,12 @@ dependencies:
108
109
  - 5
109
110
  - 8
110
111
  version: 0.5.8
111
- version_requirements: *id006
112
- name: yard
113
- prerelease: false
112
+ requirement: *id006
114
113
  - !ruby/object:Gem::Dependency
114
+ prerelease: false
115
+ name: rack
115
116
  type: :development
116
- requirement: &id007 !ruby/object:Gem::Requirement
117
+ version_requirements: &id007 !ruby/object:Gem::Requirement
117
118
  none: false
118
119
  requirements:
119
120
  - - ~>
@@ -124,12 +125,12 @@ dependencies:
124
125
  - 2
125
126
  - 1
126
127
  version: 1.2.1
127
- version_requirements: *id007
128
- name: rack
129
- prerelease: false
128
+ requirement: *id007
130
129
  - !ruby/object:Gem::Dependency
130
+ prerelease: false
131
+ name: activesupport
131
132
  type: :development
132
- requirement: &id008 !ruby/object:Gem::Requirement
133
+ version_requirements: &id008 !ruby/object:Gem::Requirement
133
134
  none: false
134
135
  requirements:
135
136
  - - ~>
@@ -140,9 +141,7 @@ dependencies:
140
141
  - 3
141
142
  - 8
142
143
  version: 2.3.8
143
- version_requirements: *id008
144
- name: activesupport
145
- prerelease: false
144
+ requirement: *id008
146
145
  description: Load gists in the background. KTHXBYE!
147
146
  email: darkhelmet@darkhelmetlive.com
148
147
  executables: []
@@ -174,7 +173,6 @@ files:
174
173
  - spec/rack-gist_spec.rb
175
174
  - spec/spec.opts
176
175
  - spec/spec_helper.rb
177
- has_rdoc: true
178
176
  homepage: http://github.com/darkhelmet/rack-gist
179
177
  licenses: []
180
178
 
@@ -204,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
204
202
  requirements: []
205
203
 
206
204
  rubyforge_project:
207
- rubygems_version: 1.6.2
205
+ rubygems_version: 1.8.18
208
206
  signing_key:
209
207
  specification_version: 3
210
208
  summary: Asynchronous Github Gists. All with rack.