Skip to content

Commit ad34791

Browse files
committed
Move starring from repos to activity namespace. Refactor tests.
1 parent ceeb9bd commit ad34791

11 files changed

Lines changed: 243 additions & 215 deletions

File tree

lib/github_api/activity.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Activity < API
55
extend AutoloadHelper
66

77
autoload_all 'github_api/activity',
8-
:Notifications => 'notifications'
8+
:Notifications => 'notifications',
9+
:Starring => 'starring'
910

1011
# Create new Activity API
1112
def initialize(options = {})
@@ -17,5 +18,10 @@ def notifications
1718
@notifications ||= ApiFactory.new 'Activity::Notifications'
1819
end
1920

21+
# Access to Activity::Starring API
22+
def starring
23+
@starring ||= ApiFactory.new 'Activity::Starring'
24+
end
25+
2026
end # Activity
2127
end # Github
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
module Github
44
# Repository Starring is a feature that lets users bookmark repositories.
55
# Stars are shown next to repositories to show an approximate level of interest. # Stars have no effect on notifications or the activity feed.
6-
class Repos::Starring < API
6+
class Activity::Starring < API
77

88
# List stargazers
99
#
1010
# = Examples
1111
# github = Github.new :user => 'user-name', :repo => 'repo-name'
12-
# github.repos.starring.list
13-
# github.repos.starring.list { |star| ... }
12+
# github.activity.starring.list
13+
# github.activity.starring.list { |star| ... }
1414
#
1515
def list(user_name, repo_name, params={})
1616
set :user => user_name, :repo => repo_name
@@ -27,13 +27,13 @@ def list(user_name, repo_name, params={})
2727
#
2828
# = Examples
2929
# github = Github.new
30-
# github.repos.starring.starred :user => 'user-name'
30+
# github.activity.starring.starred :user => 'user-name'
3131
#
3232
# List repos being starred by the authenticated user
3333
#
3434
# = Examples
3535
# github = Github.new :oauth_token => '...'
36-
# github.repos.starring.starred
36+
# github.activity.starring.starred
3737
#
3838
def starred(*args)
3939
params = args.extract_options!
@@ -54,7 +54,7 @@ def starred(*args)
5454
#
5555
# = Examples
5656
# github = Github.new
57-
# github.repos.starring.starring? 'user-name', 'repo-name'
57+
# github.activity.starring.starring? 'user-name', 'repo-name'
5858
#
5959
def starring?(user_name, repo_name, params={})
6060
assert_presence_of user_name, repo_name
@@ -71,7 +71,7 @@ def starring?(user_name, repo_name, params={})
7171
#
7272
# = Examples
7373
# github = Github.new
74-
# github.repos.starring.star 'user-name', 'repo-name'
74+
# github.activity.starring.star 'user-name', 'repo-name'
7575
#
7676
def star(user_name, repo_name, params={})
7777
set :user => user_name, :repo => repo_name

lib/github_api/repos.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Repos < API
1616
:Keys => 'keys',
1717
:Merging => 'merging',
1818
:PubSubHubbub => 'pub_sub_hubbub',
19-
:Starring => 'starring',
2019
:Statuses => 'statuses',
2120
:Watching => 'watching'
2221

@@ -96,11 +95,6 @@ def pubsubhubbub
9695
@pubsubhubbub ||= ApiFactory.new 'Repos::PubSubHubbub'
9796
end
9897

99-
# Access to Repos::Starring API
100-
def starring
101-
@starring ||= ApiFactory.new 'Repos::Starring'
102-
end
103-
10498
# Access to Repos::Statuses API
10599
def statuses
106100
@statuses ||= ApiFactory.new 'Repos::Statuses'

spec/github/activity/activity_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
its(:notifications) { should be_a Github::Activity::Notifications }
1010

11+
its(:starring) { should be_a Github::Activity::Starring }
1112
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Starring, '#list' do
6+
let(:user) { 'peter-murach' }
7+
let(:repo) { 'github' }
8+
let(:request_path) { "/repos/#{user}/#{repo}/stargazers" }
9+
10+
after { reset_authentication_for subject }
11+
12+
before {
13+
stub_get(request_path).
14+
to_return(:body => body, :status => status, :headers => {})
15+
}
16+
17+
context 'resource found' do
18+
let(:body) { fixture("repos/stargazers.json") }
19+
let(:status) { 200 }
20+
21+
it { should respond_to :all }
22+
23+
it "should fail to get resource without username" do
24+
expect { subject.list }.to raise_error(ArgumentError)
25+
end
26+
27+
it "should yield iterator if block given" do
28+
subject.should_receive(:list).with(user, repo).and_yield('github')
29+
subject.list(user, repo) { |param| 'github' }
30+
end
31+
32+
it "should get the resources" do
33+
subject.list user, repo
34+
a_get(request_path).should have_been_made
35+
end
36+
37+
it "should return array of resources" do
38+
stargazers = subject.list user, repo
39+
stargazers.should be_an Array
40+
stargazers.should have(1).items
41+
end
42+
43+
it "should return result of mash type" do
44+
stargazers = subject.list user, repo
45+
stargazers.first.should be_a Hashie::Mash
46+
end
47+
48+
it "should get watcher information" do
49+
stargazers = subject.list user, repo
50+
stargazers.first.login.should == 'octocat'
51+
end
52+
53+
context "fail to find resource" do
54+
let(:body) { '' }
55+
let(:status) { 404 }
56+
57+
it "should return 404 not found message" do
58+
expect {
59+
subject.list user, repo
60+
}.to raise_error(Github::Error::NotFound)
61+
end
62+
end
63+
end
64+
end # list
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Starring, '#star' do
6+
let(:user) { 'peter-murach' }
7+
let(:repo) { 'github' }
8+
let(:request_path) { "/user/starred/#{user}/#{repo}" }
9+
10+
after { reset_authentication_for subject }
11+
12+
context "user authenticated" do
13+
context "with correct information" do
14+
before do
15+
subject.oauth_token = OAUTH_TOKEN
16+
stub_put(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
17+
to_return(:body => "", :status => 204, :headers => {})
18+
end
19+
20+
it "should successfully star a repo" do
21+
subject.star user, repo
22+
a_put(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
23+
should have_been_made
24+
end
25+
end
26+
end
27+
28+
context "user unauthenticated" do
29+
it "should fail" do
30+
stub_put(request_path).to_return(:body => "", :status => 401, :headers => {})
31+
expect {
32+
subject.star user, repo
33+
}.to raise_error(Github::Error::Unauthorized)
34+
end
35+
end
36+
end # star
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Starring, '#starred' do
6+
let(:user) { 'peter-murach' }
7+
let(:repo) { 'github' }
8+
9+
after { reset_authentication_for subject }
10+
11+
context "if user unauthenticated" do
12+
it "should fail to get resource without username " do
13+
stub_get("/user/starred").
14+
to_return(:body => '', :status => 401, :headers => {})
15+
expect { subject.starred }.to raise_error(Github::Error::Unauthorized)
16+
end
17+
18+
it "should get the resource with username" do
19+
stub_get("/users/#{user}/starred").
20+
to_return(:body => fixture("repos/starred.json"), :status => 200, :headers => {})
21+
subject.starred :user => user
22+
a_get("/users/#{user}/starred").should have_been_made
23+
end
24+
end
25+
26+
context "if user authenticated" do
27+
before do
28+
subject.oauth_token = OAUTH_TOKEN
29+
stub_get("/user/starred").
30+
with(:query => {:access_token => OAUTH_TOKEN}).
31+
to_return(:body => fixture("repos/starred.json"),
32+
:status => 200, :headers => {})
33+
end
34+
35+
it "should get the resources" do
36+
subject.starred
37+
a_get("/user/starred").with(:query => {:access_token => OAUTH_TOKEN}).
38+
should have_been_made
39+
end
40+
41+
it "should return array of resources" do
42+
starred = subject.starred
43+
starred.should be_an Array
44+
starred.should have(1).items
45+
end
46+
47+
it "should get starred information" do
48+
starred = subject.starred
49+
starred.first.name.should == 'Hello-World'
50+
starred.first.owner.login.should == 'octocat'
51+
end
52+
end
53+
end # starred
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Starring, '#list' do
6+
let(:user) { 'peter-murach' }
7+
let(:repo) { 'github' }
8+
let(:request_path) { "/user/starred/#{user}/#{repo}" }
9+
10+
after { reset_authentication_for subject }
11+
12+
context "with username ane reponame passed" do
13+
context "this repo is being watched by the user"
14+
before do
15+
stub_get(request_path).
16+
to_return(:body => "", :status => 404,
17+
:headers => {:user_agent => subject.user_agent})
18+
end
19+
20+
it "should return false if resource not found" do
21+
starring = subject.starring? user, repo
22+
starring.should be_false
23+
end
24+
25+
it "should return true if resoure found" do
26+
stub_get(request_path).to_return(:body => "", :status => 200,
27+
:headers => {:user_agent => subject.user_agent})
28+
starring = subject.starring? user, repo
29+
starring.should be_true
30+
end
31+
end
32+
33+
context "without username and reponame passed" do
34+
it "should fail validation " do
35+
expect { subject.starring? nil, nil }.to raise_error(ArgumentError)
36+
end
37+
end
38+
end # starring?
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Starring, '#unstar' do
6+
let(:user) { 'peter-murach' }
7+
let(:repo) { 'github' }
8+
let(:request_path) { "/user/starred/#{user}/#{repo}" }
9+
10+
after { reset_authentication_for subject }
11+
12+
context "user authenticated" do
13+
context "with correct information" do
14+
before do
15+
subject.oauth_token = OAUTH_TOKEN
16+
stub_delete("/user/starred/#{user}/#{repo}").
17+
with(:query => {:access_token => OAUTH_TOKEN}).
18+
to_return(:body => "", :status => 204, :headers => {})
19+
end
20+
21+
it "should successfully unstar a repo" do
22+
subject.unstar user, repo
23+
a_delete(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
24+
should have_been_made
25+
end
26+
end
27+
end
28+
29+
context "user unauthenticated" do
30+
it "should fail" do
31+
stub_delete(request_path).to_return(:body => "", :status => 401, :headers => {})
32+
expect {
33+
subject.unstar user, repo
34+
}.to raise_error(Github::Error::Unauthorized)
35+
end
36+
end
37+
end # unstar

0 commit comments

Comments
 (0)