Skip to content

Commit 6401936

Browse files
committed
working tests
1 parent 15b2ee9 commit 6401936

File tree

5 files changed

+246
-12
lines changed

5 files changed

+246
-12
lines changed

lib/jekyll-gist/gist_tag.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'cgi'
22
require 'net/http'
3+
require 'octokit'
34

45
Net::OpenTimeout = Class.new(RuntimeError) unless Net.const_defined?(:OpenTimeout)
56
Net::ReadTimeout = Class.new(RuntimeError) unless Net.const_defined?(:ReadTimeout)
@@ -62,8 +63,7 @@ def gist_noscript_tag(gist_id, filename = nil)
6263
end
6364

6465
def fetch_raw_code(gist_id, filename = nil)
65-
content = code_from_api(gist_id, filename)
66-
return content if content
66+
return code_from_api(gist_id, filename) if ENV["JEKYLL_GITHUB_TOKEN"]
6767

6868
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
6969
url = "#{url}/#{filename}" unless filename.empty?
@@ -82,20 +82,21 @@ def fetch_raw_code(gist_id, filename = nil)
8282
private
8383

8484
def code_from_api(gist_id, filename = nil)
85-
return unless ENV["JEKYLL_GITHUB_TOKEN"]
86-
8785
client = Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"]
8886
gist = client.gist gist_id
8987

90-
file = if filename && gist.files[filename]
91-
gist.files.filename
88+
file = if filename.to_s.empty?
89+
# No file specified, return the value of the first key/value pair
90+
gist.files.first[1]
9291
else
93-
files.first
92+
# .files is a hash of :"filename.extension" => data pairs
93+
# Rather than using to_sym on arbitrary user input,
94+
# Find our file by calling to_s on the keys
95+
match = gist.files.find { |name, data| name.to_s == filename}
96+
match[1] if match
9497
end
9598

96-
file.content unless file.truncated
97-
rescue Octokit => e
98-
nil
99+
file[:content] if file && !file[:truncated]
99100
end
100101
end
101102
end

spec/fixtures/multiple-files.json

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"url": "https://api.github.com/gists/aa5a315d61ae9438b18d",
3+
"forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks",
4+
"commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits",
5+
"id": "aa5a315d61ae9438b18d",
6+
"description": "description of gist",
7+
"public": true,
8+
"owner": {
9+
"login": "octocat",
10+
"id": 1,
11+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/octocat",
14+
"html_url": "https://github.com/octocat",
15+
"followers_url": "https://api.github.com/users/octocat/followers",
16+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
20+
"organizations_url": "https://api.github.com/users/octocat/orgs",
21+
"repos_url": "https://api.github.com/users/octocat/repos",
22+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/octocat/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"user": null,
28+
"files": {
29+
"ring.erl": {
30+
"size": 932,
31+
"raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
32+
"type": "text/plain",
33+
"language": "Erlang",
34+
"truncated": false,
35+
"content": "contents of gist"
36+
},
37+
"hello-world.rb": {
38+
"size": 932,
39+
"raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
40+
"type": "text/plain",
41+
"language": "Ruby",
42+
"truncated": false,
43+
"content": "puts 'hello world'"
44+
}
45+
},
46+
"comments": 0,
47+
"comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/",
48+
"html_url": "https://gist.github.com/aa5a315d61ae9438b18d",
49+
"git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
50+
"git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
51+
"created_at": "2010-04-14T02:15:15Z",
52+
"updated_at": "2011-06-20T11:34:15Z",
53+
"forks": [
54+
{
55+
"user": {
56+
"login": "octocat",
57+
"id": 1,
58+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
59+
"gravatar_id": "",
60+
"url": "https://api.github.com/users/octocat",
61+
"html_url": "https://github.com/octocat",
62+
"followers_url": "https://api.github.com/users/octocat/followers",
63+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
64+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
65+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
66+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
67+
"organizations_url": "https://api.github.com/users/octocat/orgs",
68+
"repos_url": "https://api.github.com/users/octocat/repos",
69+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
70+
"received_events_url": "https://api.github.com/users/octocat/received_events",
71+
"type": "User",
72+
"site_admin": false
73+
},
74+
"url": "https://api.github.com/gists/dee9c42e4998ce2ea439",
75+
"id": "dee9c42e4998ce2ea439",
76+
"created_at": "2011-04-14T16:00:49Z",
77+
"updated_at": "2011-04-14T16:00:49Z"
78+
}
79+
],
80+
"history": [
81+
{
82+
"url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f",
83+
"version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
84+
"user": {
85+
"login": "octocat",
86+
"id": 1,
87+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
88+
"gravatar_id": "",
89+
"url": "https://api.github.com/users/octocat",
90+
"html_url": "https://github.com/octocat",
91+
"followers_url": "https://api.github.com/users/octocat/followers",
92+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
93+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
94+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
95+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
96+
"organizations_url": "https://api.github.com/users/octocat/orgs",
97+
"repos_url": "https://api.github.com/users/octocat/repos",
98+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
99+
"received_events_url": "https://api.github.com/users/octocat/received_events",
100+
"type": "User",
101+
"site_admin": false
102+
},
103+
"change_status": {
104+
"deletions": 0,
105+
"additions": 180,
106+
"total": 180
107+
},
108+
"committed_at": "2010-04-14T02:15:15Z"
109+
}
110+
]
111+
}

spec/fixtures/single-file.json

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"url": "https://api.github.com/gists/aa5a315d61ae9438b18d",
3+
"forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks",
4+
"commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits",
5+
"id": "aa5a315d61ae9438b18d",
6+
"description": "description of gist",
7+
"public": true,
8+
"owner": {
9+
"login": "octocat",
10+
"id": 1,
11+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/octocat",
14+
"html_url": "https://github.com/octocat",
15+
"followers_url": "https://api.github.com/users/octocat/followers",
16+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
20+
"organizations_url": "https://api.github.com/users/octocat/orgs",
21+
"repos_url": "https://api.github.com/users/octocat/repos",
22+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/octocat/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"user": null,
28+
"files": {
29+
"ring.erl": {
30+
"size": 932,
31+
"raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
32+
"type": "text/plain",
33+
"language": "Erlang",
34+
"truncated": false,
35+
"content": "contents of gist"
36+
}
37+
},
38+
"comments": 0,
39+
"comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/",
40+
"html_url": "https://gist.github.com/aa5a315d61ae9438b18d",
41+
"git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
42+
"git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
43+
"created_at": "2010-04-14T02:15:15Z",
44+
"updated_at": "2011-06-20T11:34:15Z",
45+
"forks": [
46+
{
47+
"user": {
48+
"login": "octocat",
49+
"id": 1,
50+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
51+
"gravatar_id": "",
52+
"url": "https://api.github.com/users/octocat",
53+
"html_url": "https://github.com/octocat",
54+
"followers_url": "https://api.github.com/users/octocat/followers",
55+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
56+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
57+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
58+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
59+
"organizations_url": "https://api.github.com/users/octocat/orgs",
60+
"repos_url": "https://api.github.com/users/octocat/repos",
61+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
62+
"received_events_url": "https://api.github.com/users/octocat/received_events",
63+
"type": "User",
64+
"site_admin": false
65+
},
66+
"url": "https://api.github.com/gists/dee9c42e4998ce2ea439",
67+
"id": "dee9c42e4998ce2ea439",
68+
"created_at": "2011-04-14T16:00:49Z",
69+
"updated_at": "2011-04-14T16:00:49Z"
70+
}
71+
],
72+
"history": [
73+
{
74+
"url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f",
75+
"version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
76+
"user": {
77+
"login": "octocat",
78+
"id": 1,
79+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
80+
"gravatar_id": "",
81+
"url": "https://api.github.com/users/octocat",
82+
"html_url": "https://github.com/octocat",
83+
"followers_url": "https://api.github.com/users/octocat/followers",
84+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
85+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
86+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
87+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
88+
"organizations_url": "https://api.github.com/users/octocat/orgs",
89+
"repos_url": "https://api.github.com/users/octocat/repos",
90+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
91+
"received_events_url": "https://api.github.com/users/octocat/received_events",
92+
"type": "User",
93+
"site_admin": false
94+
},
95+
"change_status": {
96+
"deletions": 0,
97+
"additions": 180,
98+
"total": 180
99+
},
100+
"committed_at": "2010-04-14T02:15:15Z"
101+
}
102+
]
103+
}

spec/gist_tag_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@
121121

122122
context "with token" do
123123
before { ENV["JEKYLL_GITHUB_TOKEN"] = "1234" }
124+
before {
125+
stub_request(:get, "https://api.github.com/gists/1342013").
126+
to_return(:status => 200, :body => fixture("single-file"), :headers => {"Content-Type" => "application/json"})
127+
}
124128
let(:gist_id) { "1342013" }
125129
let(:gist) { "page.gist_id" }
126130
let(:output) do
@@ -130,9 +134,20 @@
130134
end
131135

132136
it "produces the noscript tag" do
133-
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
137+
expect(output).to match(/<noscript><pre>contents of gist<\/pre><\/noscript>/)
134138
end
135139

140+
context "with a filename" do
141+
before {
142+
stub_request(:get, "https://api.github.com/gists/1342013").
143+
to_return(:status => 200, :body => fixture("multiple-files"), :headers => {"Content-Type" => "application/json"})
144+
}
145+
let(:content) { "{% gist 1342013 hello-world.rb %}" }
146+
147+
it "produces the noscript tag" do
148+
expect(output).to match(/<noscript><pre>puts &#39;hello world&#39;<\/pre><\/noscript>/)
149+
end
150+
end
136151
end
137152

138153
end

spec/spec_helper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
require File.expand_path("../lib/jekyll-gist.rb", TEST_DIR)
88

99
Jekyll.logger.log_level = :error
10-
STDERR.reopen(test(?e, '/dev/null') ? '/dev/null' : 'NUL:')
1110

1211
RSpec.configure do |config|
1312
config.run_all_when_everything_filtered = true
@@ -42,4 +41,9 @@ def site(opts = {})
4241
}))
4342
Jekyll::Site.new(conf)
4443
end
44+
45+
def fixture(name)
46+
path = File.expand_path "./fixtures/#{name}.json", File.dirname(__FILE__)
47+
File.open(path).read
48+
end
4549
end

0 commit comments

Comments
 (0)