Skip to content
Browse files

Merge pull request #171 from joeyw/implement-notifications-apis

Implement Notifications apis.
  • Loading branch information...
2 parents 8373dcb + 95d9c25 commit 44a8c8b277ab9413db5f514c60f50634a83dc10a @pengwynn pengwynn committed Oct 26, 2012
View
2 lib/octokit/client.rb
@@ -18,6 +18,7 @@
require 'octokit/client/repositories'
require 'octokit/client/users'
require 'octokit/client/events'
+require 'octokit/client/notifications'
require 'octokit/client/authorizations'
require 'octokit/client/refs'
require 'octokit/client/contents'
@@ -54,6 +55,7 @@ def initialize(options={})
include Octokit::Client::Repositories
include Octokit::Client::Users
include Octokit::Client::Events
+ include Octokit::Client::Notifications
include Octokit::Client::Authorizations
include Octokit::Client::Refs
include Octokit::Client::Contents
View
222 lib/octokit/client/notifications.rb
@@ -0,0 +1,222 @@
+module Octokit
+ class Client
+ module Notifications
+
+ # List your notifications
+ #
+ # @param options [Hash] Optional parameters
+ #
+ # @option options [Boolean] :all 'true' to show notifications marked as
+ # read.
+ #
+ # @option options [Boolean] :participating 'true' to show only
+ # notifications in which the user is directly participating or
+ # mentioned.
+ #
+ # @option options [String] :since Time filters out any notifications
+ # updated before the given time. The time should be passed in as UTC in
+ # the ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Ex. '2012-10-09T23:39:01Z'
+ #
+ # @return [Array<Hashie::Mash>] Array of notifications.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#list-your-notifications
+ #
+ # @example Get users notifications
+ # @client.notifications
+ #
+ # @example Get all notifications since a certain time.
+ # @client.notifications({all: true, since: '2012-10-09T23:39:01Z'})
+ def notifications(options={})
+ get("notifications", options)
+ end
+
+ # List your notifications in a repository
+ #
+ # @param repo [String, Hash, Repository] A GitHub repository
+ # @param options [Hash] Optional parameters
+ #
+ # @option options [Boolean] :all 'true' to show notifications marked as
+ # read.
+ #
+ # @option options [Boolean] :participating 'true' to show only
+ # notifications in which the user is directly participating or
+ # mentioned.
+ #
+ # @option options [String] :since Time filters out any notifications
+ # updated before the given time. The time should be passed in as UTC in
+ # the ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Ex. '2012-10-09T23:39:01Z'
+ #
+ # @return [Array<Hashie::Mash>] Array of notifications.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
+ #
+ # @example Get your notifications for pengwynn/octokit
+ # @client.repository_notifications('pengwynn/octokit')
+ #
+ # @example Get your notifications for pengwynn/octokit since a time.
+ # @client.repository_notifications({since: '2012-10-09T23:39:01Z'})
+ def repository_notifications(repo, options={})
+ get("repos/#{Repository.new repo}/notifications", options)
+ end
+ alias :repo_notifications :repository_notifications
+
+ # Mark notifications as read
+ #
+ # @param options [Hash] Optional parameters
+ #
+ # @option options [Boolean] :unread Changes the unread status of the
+ # threads.
+ #
+ # @option options [Boolean] :read Inverse of 'unread'.
+ #
+ # @option options [String] :last_read_at ('Now') Describes the last point
+ # that notifications were checked. Anything updated since this time
+ # will not be updated. The time should be passed in as UTC in the
+ # ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Ex. '2012-10-09T23:39:01Z'
+ #
+ # @return [Boolean] True if marked as read, false otherwise
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#mark-as-read
+ #
+ # @example
+ # @client.mark_notifications_as_read
+ def mark_notifications_as_read(options={})
+ begin
+ put("notifications", options, 3, true, true).status == 205
+ rescue
+ false
+ end
+ end
+
+ # Mark notifications from a specific repository as read
+ #
+ # @param repo [String, Hash, Repository] A GitHub repository
+ # @param options [Hash] Optional parameters
+ #
+ # @option options [Boolean] :unread Changes the unread status of the
+ # threads.
+ #
+ # @option options [Boolean] :read Inverse of 'unread'.
+ #
+ # @option options [String] :last_read_at ('Now') Describes the last point
+ # that notifications were checked. Anything updated since this time
+ # will not be updated. The time should be passed in as UTC in the
+ # ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Ex. '2012-10-09T23:39:01Z'
+ #
+ # @return [Boolean] True if marked as read, false otherwise
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
+ #
+ # @example
+ # @client.mark_notifications_as_read("pengwynn/octokit")
+ def mark_repository_notifications_as_read(repo, options={})
+ begin
+ put("repos/#{Repository.new repo}/notifications", options, 3, true, true).status == 205
+ rescue
+ false
+ end
+ end
+ alias :mark_repo_notifications_as_read :mark_repository_notifications_as_read
+
+ # List notifications for a specific thread
+ #
+ # @param thread_id [Integer] Id of the thread.
+ #
+ # @return [Array<Hashie::Mashie>] Array of notifications.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#view-a-single-thread
+ #
+ # @example
+ # @client.notification_thread(1000)
+ def thread_notifications(thread_id, options={})
+ get("notifications/threads/#{thread_id}", options)
+ end
+
+ # Mark thread as read
+ #
+ # @param thread_id [Integer] Id of the thread to update.
+ # @param options [Hash] Optional parameters.
+ #
+ # @option options [Boolean] :unread Changes the unread status of the
+ # threads.
+ #
+ # @option options [Boolean] :read Inverse of 'unread'.
+ #
+ # @return [Boolean] True if updated, false otherwise.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
+ #
+ # @example
+ # @client.mark_thread_as_ready(1, :read => false)
+ def mark_thread_as_read(thread_id, options={})
+ begin
+ patch("notifications/threads/#{thread_id}", options, 3, true, true).status == 205
+ rescue
+ false
+ end
+ end
+
+ # Get thread subscription
+ #
+ # @param thread_id [Integer] Id of the thread.
+ #
+ # @return [Hashie::Mash] Subscription.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
+ #
+ # @example
+ # @client.thread_subscription(1)
+ def thread_subscription(thread_id, options={})
+ get("notifications/threads/#{thread_id}/subscription", options)
+ end
+
+ # Update thread subscription
+ #
+ # This lets you subscribe to a thread, or ignore it. Subscribing to a
+ # thread is unnecessary if the user is already subscribed to the
+ # repository. Ignoring a thread will mute all future notifications (until
+ # you comment or get @mentioned).
+ #
+ # @param thread_id [Integer] Id of the thread.
+ # @param options
+ #
+ # @option options [Boolean] :subscribed Determines if notifications
+ # should be received from this repository.
+ #
+ # @option options [Boolean] :ignored Deterimines if all notifications
+ # should be blocked from this repository.
+ #
+ # @return [Hashie::Mash] Updated subscription.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
+ #
+ # @example Subscribe to notifications
+ # @client.update_thread_subscription(1, :subscribed => true)
+ #
+ # @example Ignore notifications from a repo
+ # @client.update_thread_subscription(1, :ignored => true)
+ def update_thread_subscription(thread_id, options={})
+ put("notifications/threads/#{thread_id}/subscription", options)
+ end
+
+ # Delete a thread subscription
+ #
+ # @param thread_id [Integer] Id of the thread.
+ #
+ # @return [Boolean] True if delete successful, false otherwise.
+ #
+ # @see http://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
+ #
+ # @example
+ # @client.delete_thread_subscription(1)
+ def delete_thread_subscription(thread_id, options={})
+ begin
+ delete("notifications/threads/#{thread_id}", options, 3, true, true).status == 204
+ rescue
+ false
+ end
+ end
+
+ end
+ end
+end
View
32 spec/fixtures/v3/notification_thread.json
@@ -0,0 +1,32 @@
+[
+ {
+ "id": 1,
+ "repository": {
+ "id": 1296269,
+ "owner": {
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "gravatar_id": "somehexcode",
+ "url": "https://api.github.com/users/octocat"
+ },
+ "name": "Hello-World",
+ "full_name": "octocat/Hello-World",
+ "description": "This your first repo!",
+ "private": false,
+ "fork": false,
+ "url": "https://api.github.com/repos/octocat/Hello-World",
+ "html_url": "https://github.com/octocat/Hello-World"
+ },
+ "subject": {
+ "title": "Greetings",
+ "url": "https://api.github.com/repos/pengwynn/octokit/issues/123",
+ "latest_comment_url": "https://api.github.com/repos/pengwynn/octokit/issues/comments/123"
+ },
+ "reason": "subscribed",
+ "unread": true,
+ "updated_at": "2012-09-25T07:54:41-07:00",
+ "last_read_at": "2012-09-25T07:54:41-07:00",
+ "url": "https://api.github.com/notifications/threads/1"
+ }
+]
View
32 spec/fixtures/v3/notifications.json
@@ -0,0 +1,32 @@
+[
+ {
+ "id": 1,
+ "repository": {
+ "id": 1296269,
+ "owner": {
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "gravatar_id": "somehexcode",
+ "url": "https://api.github.com/users/octocat"
+ },
+ "name": "Hello-World",
+ "full_name": "octocat/Hello-World",
+ "description": "This your first repo!",
+ "private": false,
+ "fork": false,
+ "url": "https://api.github.com/repos/octocat/Hello-World",
+ "html_url": "https://github.com/octocat/Hello-World"
+ },
+ "subject": {
+ "title": "Greetings",
+ "url": "https://api.github.com/repos/pengwynn/octokit/issues/123",
+ "latest_comment_url": "https://api.github.com/repos/pengwynn/octokit/issues/comments/123"
+ },
+ "reason": "subscribed",
+ "unread": true,
+ "updated_at": "2012-09-25T07:54:41-07:00",
+ "last_read_at": "2012-09-25T07:54:41-07:00",
+ "url": "https://api.github.com/notifications/threads/1"
+ }
+]
View
32 spec/fixtures/v3/repository_notifications.json
@@ -0,0 +1,32 @@
+[
+ {
+ "id": 1,
+ "repository": {
+ "id": 1296269,
+ "owner": {
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "gravatar_id": "somehexcode",
+ "url": "https://api.github.com/users/octocat"
+ },
+ "name": "Hello-World",
+ "full_name": "octocat/Hello-World",
+ "description": "This your first repo!",
+ "private": false,
+ "fork": false,
+ "url": "https://api.github.com/repos/octocat/Hello-World",
+ "html_url": "https://github.com/octocat/Hello-World"
+ },
+ "subject": {
+ "title": "Greetings",
+ "url": "https://api.github.com/repos/pengwynn/octokit/issues/123",
+ "latest_comment_url": "https://api.github.com/repos/pengwynn/octokit/issues/comments/123"
+ },
+ "reason": "subscribed",
+ "unread": true,
+ "updated_at": "2012-09-25T07:54:41-07:00",
+ "last_read_at": "2012-09-25T07:54:41-07:00",
+ "url": "https://api.github.com/notifications/threads/1"
+ }
+]
View
8 spec/fixtures/v3/thread_subscription.json
@@ -0,0 +1,8 @@
+{
+ "subscribed": true,
+ "ignored": false,
+ "reason": null,
+ "created_at": "2012-10-06T21:34:12Z",
+ "url": "https://api.github.com/notifications/threads/1/subscription",
+ "thread_url": "https://api.github.com/notifications/threads/1"
+}
View
8 spec/fixtures/v3/thread_subscription_update.json
@@ -0,0 +1,8 @@
+{
+ "subscribed": true,
+ "ignored": false,
+ "reason": null,
+ "created_at": "2012-10-06T21:34:12Z",
+ "url": "https://api.github.com/notifications/threads/1/subscription",
+ "thread_url": "https://api.github.com/notifications/threads/1"
+}
View
141 spec/octokit/client/notifications_spec.rb
@@ -0,0 +1,141 @@
+# -*- encoding: utf-8 -*-
+require 'helper'
+
+describe Octokit::Client::Notifications do
+
+ before do
+ @client = Octokit::Client.new(:login => "joeyw")
+ end
+
+ describe ".notifications" do
+
+ it "lists the notifications for the current user" do
+ stub_get("https://api.github.com/notifications").
+ to_return(:body => fixture("v3/notifications.json"))
+ notifications = @client.notifications
+ expect(notifications.first.id).to eq(1)
+ expect(notifications.first.unread).to be_true
+ end
+
+ end
+
+ describe ".repository_notifications" do
+
+ it "lists all notifications for a repository" do
+ stub_get("https://api.github.com/repos/pengwynn/octokit/notifications").
+ to_return(:body => fixture("v3/repository_notifications.json"))
+ notifications = @client.repository_notifications("pengwynn/octokit")
+ expect(notifications.first.id).to eq(1)
+ expect(notifications.first.unread).to be_true
+ end
+
+ end
+
+ describe ".mark_notifications_as_read" do
+
+ it "returns true when notifications are marked as read" do
+ stub_put("https://api.github.com/notifications").
+ to_return(:status => 205)
+ result = @client.mark_notifications_as_read
+ expect(result).to be_true
+ end
+
+ it "returns false when unsuccessful" do
+ stub_put("https://api.github.com/notifications").
+ to_return(:status => 500)
+ result = @client.mark_notifications_as_read
+ expect(result).to be_false
+ end
+
+ end
+
+ describe ".mark_repository_notifications_as_read" do
+
+ it "returns true when notifications for a repo are marked as read" do
+ stub_put("https://api.github.com/repos/pengwynn/octokit/notifications").
+ to_return(:status => 205)
+ result = @client.mark_repository_notifications_as_read("pengwynn/octokit")
+ expect(result).to be_true
+ end
+
+ it "returns false when unsuccessful" do
+ stub_put("https://api.github.com/repos/pengwynn/octokit/notifications").
+ to_return(:status => 500)
+ result = @client.mark_repository_notifications_as_read("pengwynn/octokit")
+ expect(result).to be_false
+ end
+
+ end
+
+ describe ".thread_notifications" do
+
+ it "returns notifications for a specific thread" do
+ stub_get("https://api.github.com/notifications/threads/1").
+ to_return(:body => fixture('v3/notification_thread.json'))
+ notifications = @client.thread_notifications(1)
+ expect(notifications.first.id).to eq(1)
+ expect(notifications.first.unread).to be_true
+ end
+
+ end
+
+ describe ".mark_thread_as_read" do
+
+ it "marks a thread as read" do
+ stub_patch("https://api.github.com/notifications/threads/1").
+ to_return(:status => 205)
+ result = @client.mark_thread_as_read(1)
+ expect(result).to be_true
+ end
+
+ it "returns false when unsuccessful" do
+ stub_patch("https://api.github.com/notifications/threads/1").
+ to_return(:status => 500)
+ result = @client.mark_thread_as_read(1)
+ expect(result).to be_false
+ end
+
+ end
+
+ describe ".thread_subscription" do
+
+ it "returns a thread subscription" do
+ stub_get("https://api.github.com/notifications/threads/1/subscription").
+ to_return(:body => fixture("v3/thread_subscription.json"))
+ subscription = @client.thread_subscription(1)
+ expect(subscription.subscribed).to be_true
+ end
+
+ end
+
+ describe ".update_thread_subscription" do
+
+ it "updates a thread subscription" do
+ stub_put("https://api.github.com/notifications/threads/1/subscription").
+ to_return(:body => fixture("v3/thread_subscription_update.json"))
+ subscription = @client.update_thread_subscription(1, :subscribed => true)
+ expect(subscription.subscribed).to be_true
+ end
+
+ end
+
+ describe ".delete_thread_subscription" do
+
+ it "returns true with successful thread deletion" do
+ stub_delete("https://api.github.com/notifications/threads/1").
+ to_return(:status => 204)
+ result = @client.delete_thread_subscription(1)
+ expect(result).to be_true
+ end
+
+ it "returns false when subscription deletion fails" do
+ stub_delete("https://api.github.com/notifications/threads/1").
+ to_return(:status => 500)
+ result = @client.delete_thread_subscription(1)
+ expect(result).to be_false
+ end
+
+
+ end
+
+end

0 comments on commit 44a8c8b

Please sign in to comment.
Something went wrong with that request. Please try again.