Skip to content

Commit 4b47408

Browse files
committed
Change gists to remove params requirement, add gist specific api call and change forks listing api endpoint.
1 parent 3ca60ca commit 4b47408

11 files changed

Lines changed: 192 additions & 127 deletions

File tree

lib/github_api/client/gists.rb

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ class Client::Gists < API
55

66
require_all 'github_api/client/gists', 'comments'
77

8-
REQUIRED_GIST_INPUTS = %w[
9-
description
10-
public
11-
files
12-
content
13-
].freeze
14-
158
# Access to Gists::Comments API
169
namespace :comments
1710

1811
# List a user's gists
1912
#
13+
# @see https://developer.github.com/v3/gists/#list-a-users-gists
14+
#
2015
# @example
2116
# github = Github.new
2217
# github.gists.list user: 'user-name'
@@ -30,6 +25,8 @@ class Client::Gists < API
3025
#
3126
# List all public gists
3227
#
28+
# @see https://developer.github.com/v3/gists/#list-all-public-gists
29+
#
3330
# github = Github.new
3431
# github.gists.list :public
3532
#
@@ -49,13 +46,15 @@ def list(*args)
4946
return response unless block_given?
5047
response.each { |el| yield el }
5148
end
52-
alias :all :list
49+
alias_method :all, :list
5350

5451
# List the authenticated user's starred gists
5552
#
53+
# @see https://developer.github.com/v3/gists/#list-starred-gists
54+
#
5655
# @example
57-
# github = Github.new oauth_token: '...'
58-
# github.gists.starred
56+
# github = Github.new oauth_token: '...'
57+
# github.gists.starred
5958
#
6059
# @return [Hash]
6160
#
@@ -69,22 +68,38 @@ def starred(*args)
6968

7069
# Get a single gist
7170
#
71+
# @see https://developer.github.com/v3/gists/#get-a-single-gist
72+
#
7273
# @example
73-
# github = Github.new
74-
# github.gists.get 'gist-id'
74+
# github = Github.new
75+
# github.gists.get 'gist-id'
76+
#
77+
# Get a specific revision of gist
78+
#
79+
# @see https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist
80+
#
81+
# @example
82+
# github = Github.new
83+
# github.gists.get 'gist-id', sha: '
7584
#
7685
# @return [Hash]
7786
#
7887
# @api public
7988
def get(*args)
8089
arguments(args, required: [:id])
8190

82-
get_request("/gists/#{arguments.id}", arguments.params)
91+
if (sha = arguments.params.delete('sha'))
92+
get_request("/gists/#{arguments.id}/#{sha}")
93+
else
94+
get_request("/gists/#{arguments.id}", arguments.params)
95+
end
8396
end
84-
alias :find :get
97+
alias_method :find, :get
8598

8699
# Create a gist
87100
#
101+
# @see https://developer.github.com/v3/gists/#create-a-gist
102+
#
88103
# @param [Hash] params
89104
# @option params [String] :description
90105
# Optional string
@@ -112,15 +127,15 @@ def get(*args)
112127
#
113128
# @api public
114129
def create(*args)
115-
arguments(args) do
116-
assert_required REQUIRED_GIST_INPUTS
117-
end
130+
arguments(args)
118131

119132
post_request("/gists", arguments.params)
120133
end
121134

122135
# Edit a gist
123136
#
137+
# @see https://developer.github.com/v3/gists/#edit-a-gist
138+
#
124139
# @param [Hash] params
125140
# @option [String] :description
126141
# Optional string
@@ -162,6 +177,8 @@ def edit(*args)
162177

163178
# List gist commits
164179
#
180+
# @see https://developer.github.com/v3/gists/#list-gist-commits
181+
#
165182
# @example
166183
# github = Github.new
167184
# github.gists.commits 'gist-id'
@@ -177,6 +194,8 @@ def commits(*args)
177194

178195
# Star a gist
179196
#
197+
# @see https://developer.github.com/v3/gists/#star-a-gist
198+
#
180199
# @example
181200
# github = Github.new
182201
# github.gists.star 'gist-id'
@@ -190,6 +209,8 @@ def star(*args)
190209

191210
# Unstar a gist
192211
#
212+
# @see https://developer.github.com/v3/gists/#unstar-a-gist
213+
#
193214
# @xample
194215
# github = Github.new
195216
# github.gists.unstar 'gist-id'
@@ -203,10 +224,13 @@ def unstar(*args)
203224

204225
# Check if a gist is starred
205226
#
206-
# = Examples
207-
# github = Github.new
208-
# github.gists.starred? 'gist-id'
227+
# @see https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
209228
#
229+
# @example
230+
# github = Github.new
231+
# github.gists.starred? 'gist-id'
232+
#
233+
# @api public
210234
def starred?(*args)
211235
arguments(args, required: [:id])
212236
get_request("/gists/#{arguments.id}/star", arguments.params)
@@ -225,14 +249,16 @@ def starred?(*args)
225249
def fork(*args)
226250
arguments(args, required: [:id])
227251

228-
post_request("/gists/#{arguments.id}/fork", arguments.params)
252+
post_request("/gists/#{arguments.id}/forks", arguments.params)
229253
end
230254

231255
# List gist forks
232256
#
257+
# @see https://developer.github.com/v3/gists/#list-gist-forks
258+
#
233259
# @example
234-
# github = Github.new
235-
# github.gists.forks 'gist-id'
260+
# github = Github.new
261+
# github.gists.forks 'gist-id'
236262
#
237263
# @api public
238264
def forks(*args)
@@ -245,6 +271,8 @@ def forks(*args)
245271

246272
# Delete a gist
247273
#
274+
# @see https://developer.github.com/v3/gists/#delete-a-gist
275+
#
248276
# @example
249277
# github = Github.new
250278
# github.gists.delete 'gist-id'

spec/github/client/gists/create_spec.rb

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Gists, '#create' do
5+
RSpec.describe Github::Client::Gists, '#create' do
66
let(:request_path) { "/gists" }
77

88
let(:inputs) {
@@ -19,8 +19,8 @@
1919

2020
before {
2121
stub_post(request_path).with(inputs).
22-
to_return(:body => body, :status => status,
23-
:headers => {:content_type => "application/json; charset=utf-8"})
22+
to_return(body: body, status: status,
23+
headers: {content_type: "application/json; charset=utf-8"})
2424
}
2525

2626
after { reset_authentication_for(subject) }
@@ -29,36 +29,23 @@
2929
let(:body) { fixture('gists/gist.json') }
3030
let(:status) { 201 }
3131

32-
it "should fail to create resource if 'files' input is missing" do
33-
expect {
34-
subject.create inputs.except('files')
35-
}.to raise_error(Github::Error::RequiredParams)
36-
end
37-
38-
it "should fail to create resource if 'public' input is missing" do
39-
expect {
40-
subject.create inputs.except('public')
41-
}.to raise_error(Github::Error::RequiredParams)
42-
end
43-
44-
it "should create resource successfully" do
32+
it "creates resource successfully" do
4533
subject.create inputs
46-
a_post(request_path).with(inputs).should have_been_made
34+
expect(a_post(request_path).with(inputs)).to have_been_made
4735
end
4836

49-
it "should return the resource" do
37+
it "returns the resource" do
5038
gist = subject.create inputs
51-
gist.should be_a Github::ResponseWrapper
39+
expect(gist).to be_a Github::ResponseWrapper
5240
end
5341

54-
it "should get the gist information" do
42+
it "gets the gist information" do
5543
gist = subject.create inputs
56-
gist.user.login.should == 'octocat'
44+
expect(gist.user.login).to eq('octocat')
5745
end
5846
end
5947

6048
it_should_behave_like 'request failure' do
6149
let(:requestable) { subject.create inputs }
6250
end
63-
6451
end # create

spec/github/client/gists/delete_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Gists, '#delete' do
5+
RSpec.describe Github::Client::Gists, '#delete' do
66
let(:gist_id) { 1 }
77
let(:request_path) { "/gists/#{gist_id}" }
88
let(:body) { fixture('gists/gist.json') }
99
let(:status) { 204 }
1010

1111
before {
12-
stub_delete(request_path).to_return(:body => body, :status => status,
13-
:headers => {:content_type => "application/json; charset=utf-8"})
12+
stub_delete(request_path).to_return(body: body, status: status,
13+
headers: {content_type: "application/json; charset=utf-8"})
1414
}
1515

1616
after { reset_authentication_for(subject) }
1717

18-
it 'should raise error if gist id not present' do
18+
it 'raises error if gist id not present' do
1919
expect { subject.delete }.to raise_error(ArgumentError)
2020
end
2121

22-
it "should remove resource successfully" do
23-
subject.delete gist_id
24-
a_delete(request_path).should have_been_made
22+
it "removes resource successfully" do
23+
subject.delete(gist_id)
24+
expect(a_delete(request_path)).to have_been_made
2525
end
2626

2727
it_should_behave_like 'request failure' do
2828
let(:requestable) { subject.delete gist_id }
2929
end
30-
3130
end # delete

spec/github/client/gists/edit_spec.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Gists, '#edit' do
5+
RSpec.describe Github::Client::Gists, '#edit' do
66
let(:gist_id) { 1 }
77
let(:request_path) { "/gists/#{gist_id}" }
88

@@ -27,8 +27,8 @@
2727

2828
before {
2929
stub_patch(request_path).with(inputs).
30-
to_return(:body => body, :status => status,
31-
:headers => {:content_type => "application/json; charset=utf-8"})
30+
to_return(body: body, status: status,
31+
headers: {content_type: "application/json; charset=utf-8"})
3232
}
3333

3434
after { reset_authentication_for(subject) }
@@ -37,24 +37,23 @@
3737
let(:body) { fixture('gists/gist.json') }
3838
let(:status) { 200 }
3939

40-
it "should edit resource successfully" do
41-
subject.edit gist_id, inputs
42-
a_patch(request_path).with(inputs).should have_been_made
40+
it "edits resource successfully" do
41+
subject.edit(gist_id, inputs)
42+
expect(a_patch(request_path).with(inputs)).to have_been_made
4343
end
4444

45-
it "should return the resource" do
46-
gist = subject.edit gist_id, inputs
47-
gist.should be_a Github::ResponseWrapper
45+
it "returns the resource" do
46+
gist = subject.edit(gist_id, inputs)
47+
expect(gist).to be_a(Github::ResponseWrapper)
4848
end
4949

50-
it "should get the gist information" do
50+
it "gets the gist information" do
5151
gist = subject.edit gist_id, inputs
52-
gist.user.login.should == 'octocat'
52+
expect(gist.user.login).to eq('octocat')
5353
end
5454
end
5555

5656
it_should_behave_like 'request failure' do
5757
let(:requestable) { subject.edit gist_id, inputs }
5858
end
59-
6059
end # edit

spec/github/client/gists/fork_spec.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Gists, '#star' do
5+
RSpec.describe Github::Client::Gists, '#fork' do
66
let(:gist_id) { 1 }
7-
let(:request_path) { "/gists/#{gist_id}/fork" }
7+
let(:request_path) { "/gists/#{gist_id}/forks" }
88
let(:body) { fixture('gists/gist.json') }
99
let(:status) { 201 }
1010

1111
before {
12-
stub_post(request_path).to_return(:body => body, :status => status,
13-
:headers => {:content_type => "application/json; charset=utf-8"})
12+
stub_post(request_path).to_return(body: body, status: status,
13+
headers: {content_type: "application/json; charset=utf-8"})
1414
}
1515

1616
after { reset_authentication_for(subject) }
1717

18-
it "should fail to fork gist without gist id" do
18+
it "fails to fork gist without gist id" do
1919
expect { subject.fork }.to raise_error(ArgumentError)
2020
end
2121

2222
it "should fork resource successfully" do
23-
subject.fork gist_id
24-
a_post(request_path).should have_been_made
23+
subject.fork(gist_id)
24+
expect(a_post(request_path)).to have_been_made
2525
end
2626

27-
it "should return the resource" do
28-
gist = subject.fork gist_id
29-
gist.should be_a Github::ResponseWrapper
27+
it "returns the resource" do
28+
gist = subject.fork(gist_id)
29+
expect(gist).to be_a(Github::ResponseWrapper)
3030
end
3131

32-
it "should get the gist information" do
32+
it "gets the gist information" do
3333
gist = subject.fork gist_id
34-
gist.user.login.should == 'octocat'
34+
expect(gist.user.login).to eq('octocat')
3535
end
3636

3737
it_should_behave_like 'request failure' do
3838
let(:requestable) { subject.fork gist_id }
3939
end
40-
4140
end # fork

0 commit comments

Comments
 (0)