Skip to content

Commit 66ebb96

Browse files
committed
Add notifications api thread creation and deletion.
1 parent 0ba2a1d commit 66ebb96

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

lib/github_api/activity/notifications.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,42 @@ def subscribed?(thread_id, params={})
7676
get_request("/notifications/threads/#{thread_id}/subscription", params)
7777
end
7878

79+
# Create a thread subscription
80+
#
81+
# This lets you subscribe to a thread, or ignore it. Subscribing to a thread
82+
# is unnecessary if the user is already subscribed to the repository. Ignoring
83+
# a thread will mute all future notifications (until you comment or get
84+
# @mentioned).
85+
#
86+
# = Parameters
87+
# * <tt>:subscribed</tt> - boolean - determines if notifications should be
88+
# received from this thread.
89+
# * <tt>:ignored</tt> - boolean - deterimines if all notifications should be
90+
# blocked from this thread.
91+
# = Examples
92+
# github = Github.new oauth_token: 'token'
93+
# github.activity.notifications.create 'thread-id',
94+
# 'subscribed': true
95+
# 'ignored': false
96+
#
97+
def create(thread_id, params={})
98+
assert_presence_of thread_id
99+
normalize! params
100+
put_request("/notifications/threads/#{thread_id}/subscription", params)
101+
end
102+
103+
# Delete a thread subscription
104+
#
105+
# = Examples
106+
# github = Github.new oauth_token: 'token'
107+
# github.activity.notifications.delete 'thread_id'
108+
#
109+
def delete(thread_id, params={})
110+
assert_presence_of thread_id
111+
normalize! params
112+
delete_request("/notifications/threads/#{thread_id}/subscription", params)
113+
end
114+
alias :remove :delete
115+
79116
end # Activity::Notifications
80117
end # Github
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Notifications, '#create' do
6+
let(:thread_id) { 1 }
7+
let(:request_path) { "/notifications/threads/#{thread_id}/subscription" }
8+
9+
let(:inputs) {{
10+
:subscribed => true,
11+
:ignored => false
12+
}}
13+
14+
before {
15+
subject.oauth_token = OAUTH_TOKEN
16+
stub_put(request_path).
17+
with(:body => inputs, :query => {:access_token => OAUTH_TOKEN}).
18+
to_return(:body => body, :status => status,
19+
:headers => {:content_type => "application/json; charset=utf-8"} )
20+
}
21+
22+
after { reset_authentication_for subject }
23+
24+
context "resource created successfully" do
25+
let(:body) { fixture('activity/subscribed.json') }
26+
let(:status) { 200 }
27+
28+
it 'asserts thread id presence' do
29+
expect { subject.create nil }.to raise_error(ArgumentError)
30+
end
31+
32+
it 'should create resource' do
33+
subject.create thread_id, inputs
34+
a_put(request_path).
35+
with(:body => inputs, :query => {:access_token => OAUTH_TOKEN}).
36+
should have_been_made
37+
end
38+
39+
it 'should return the resource' do
40+
thread = subject.create thread_id, inputs
41+
thread.subscribed.should be_true
42+
end
43+
end
44+
end # create
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Activity::Notifications, '#delete' do
6+
let(:thread_id) { 1 }
7+
let(:request_path) { "/notifications/threads/#{thread_id}/subscription" }
8+
let(:body) { '' }
9+
let(:status) { 204 }
10+
11+
before {
12+
subject.oauth_token = OAUTH_TOKEN
13+
stub_delete(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
14+
to_return(:body => body, :status => status,
15+
:headers => { :content_type => "application/json; charset=utf-8"})
16+
}
17+
18+
after { reset_authentication_for subject }
19+
20+
it { should respond_to :remove }
21+
22+
it "should delete the resource successfully" do
23+
subject.delete thread_id
24+
a_delete(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
25+
should have_been_made
26+
end
27+
28+
it "should fail to delete resource without 'user' parameter" do
29+
expect { subject.delete nil }.to raise_error(ArgumentError)
30+
end
31+
32+
context 'failed to delete' do
33+
let(:status) { 404 }
34+
35+
it "should fail to delete resource that is not found" do
36+
expect {
37+
subject.delete thread_id
38+
}.to raise_error(Github::Error::NotFound)
39+
end
40+
end
41+
end # delete

0 commit comments

Comments
 (0)