Skip to content

Commit 6c2d000

Browse files
committed
use octokit to fetch gists when token present
1 parent 475035e commit 6c2d000

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

jekyll-gist.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
1919
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
2020
spec.require_paths = ["lib"]
2121

22+
spec.add_dependency "octokit", "~> 4.2"
2223
spec.add_development_dependency "bundler", "~> 1.6"
2324
spec.add_development_dependency "rake"
2425
spec.add_development_dependency "rspec"

lib/jekyll-gist/gist_tag.rb

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,40 @@ def gist_noscript_tag(gist_id, filename = nil)
6262
end
6363

6464
def fetch_raw_code(gist_id, filename = nil)
65+
content = code_from_api(gist_id, filename)
66+
return content if content
67+
6568
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
6669
url = "#{url}/#{filename}" unless filename.empty?
67-
begin
68-
uri = URI(url)
69-
Net::HTTP.start(uri.host, uri.port,
70-
use_ssl: uri.scheme == 'https',
71-
read_timeout: 3, open_timeout: 3) do |http|
72-
request = Net::HTTP::Get.new uri.to_s
73-
response = http.request(request)
74-
response.body
75-
end
76-
rescue SocketError, Net::HTTPError, Net::OpenTimeout, Net::ReadTimeout, TimeoutError
77-
nil
70+
uri = URI(url)
71+
Net::HTTP.start(uri.host, uri.port,
72+
use_ssl: uri.scheme == 'https',
73+
read_timeout: 3, open_timeout: 3) do |http|
74+
request = Net::HTTP::Get.new uri.to_s
75+
response = http.request(request)
76+
response.body
7877
end
78+
rescue SocketError, Net::HTTPError, Net::OpenTimeout, Net::ReadTimeout, TimeoutError
79+
nil
80+
end
81+
82+
private
83+
84+
def code_from_api(gist_id, filename = nil)
85+
return unless ENV["JEKYLL_GITHUB_TOKEN"]
86+
87+
client = Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"]
88+
gist = client.gist gist_id
89+
90+
file = if filename && gist.files[filename]
91+
gist.files.filename
92+
else
93+
files.first
94+
end
95+
96+
file.content unless file.truncated
97+
rescue Octokit => e
98+
nil
7999
end
80100
end
81101
end

spec/gist_tag_spec.rb

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
doc.output = Jekyll::Renderer.new(doc.site, doc).run
1010
end
1111

12+
before_each { ENV["JEKYLL_GITHUB_TOKEN"] = nil }
1213

1314
context "valid gist" do
1415
context "with user prefix" do
@@ -18,7 +19,7 @@
1819
it "produces the correct script tag" do
1920
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
2021
end
21-
it "produces the correct noscript tag" do
22+
it "produces the correct noscript tag" do
2223
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
2324
end
2425
end
@@ -30,7 +31,7 @@
3031
it "produces the correct script tag" do
3132
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
3233
end
33-
it "produces the correct noscript tag" do
34+
it "produces the correct noscript tag" do
3435
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
3536
end
3637
end
@@ -42,7 +43,7 @@
4243
it "produces the correct script tag" do
4344
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
4445
end
45-
it "produces the correct noscript tag" do
46+
it "produces the correct noscript tag" do
4647
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
4748
end
4849
end
@@ -56,7 +57,7 @@
5657
it "produces the correct script tag" do
5758
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>/)
5859
end
59-
it "produces the correct noscript tag" do
60+
it "produces the correct noscript tag" do
6061
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
6162
end
6263
end
@@ -74,7 +75,7 @@
7475
it "produces the correct script tag" do
7576
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>/)
7677
end
77-
it "produces the correct noscript tag" do
78+
it "produces the correct noscript tag" do
7879
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
7980
end
8081
end
@@ -97,12 +98,12 @@
9798
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>/)
9899
end
99100

100-
it "produces the correct noscript tag" do
101+
it "produces the correct noscript tag" do
101102
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
102103
end
103104
end
104105

105-
context "with valid gist id and invalid filename" do
106+
context "with valid gist id and invalid filename" do
106107
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(status: 404) }
107108
let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
108109
let(:gist_filename) { "myfile.ext" }
@@ -112,14 +113,29 @@
112113
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>/)
113114
end
114115

115-
it "does not produce the noscript tag" do
116+
it "does not produce the noscript tag" do
116117
expect(output).to_not match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
117118
end
118119

119120
end
120121

121-
end
122+
context "with token" do
123+
before { ENV["JEKYLL_GITHUB_TOKEN"] = "1234" }
124+
let(:gist_id) { "1342013" }
125+
let(:gist) { "page.gist_id" }
126+
let(:output) do
127+
doc.data['gist_id'] = gist_id
128+
doc.content = content
129+
doc.output = Jekyll::Renderer.new(doc.site, doc).run
130+
end
131+
132+
it "produces the noscript tag" do
133+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
134+
end
122135

136+
end
137+
138+
end
123139

124140
context "invalid gist" do
125141

0 commit comments

Comments
 (0)