From 82d316ba64b1ed04854907c0eac21b2657034a06 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Thu, 21 May 2015 12:47:12 -0400 Subject: [PATCH] crack two --- lib/pubsub/index.js | 93 ++++++++++++++++++++++---------------- lib/pubsub/subscription.js | 1 + lib/pubsub/topic.js | 20 ++------ 3 files changed, 58 insertions(+), 56 deletions(-) diff --git a/lib/pubsub/index.js b/lib/pubsub/index.js index 58faa8072c2..7186c8d56e0 100644 --- a/lib/pubsub/index.js +++ b/lib/pubsub/index.js @@ -169,19 +169,20 @@ PubSub.prototype.createTopic = function(name, callback) { * Your provided callback will either be invoked with an error object, if an API * error occurred, or a {@linkcode module:pubsub/subscription} object. * - * @throws {Error} If a name is not provided. + * @throws {Error} If a Topic instance or topic name is not provided. + * @throws {Error} If a subName is not provided. * - * @param {string} name - The name of the subscription. + * @param {module:pubsub/topic|string} - topic - The Topic to create a + * subscription to. + * @param {string} subName - The name of the subscription. * @param {object=} options - Configuration object. - * @param {number=} options.ackDeadlineSeconds - The maximum time after - * receiving a message that you must ack a message before it is redelivered. - * @param {boolean=} options.autoAck - Automatically acknowledge the message - * once it's pulled. (default: false) - * @param {number=} options.interval - Interval in milliseconds to check for new + * @param {number} options.ackDeadlineSeconds - The maximum time after receiving + * a message that you must ack a message before it is redelivered. + * @param {boolean} options.autoAck - Automatically acknowledge the message once + * it's pulled. (default: false) + * @param {number} options.interval - Interval in milliseconds to check for new * messages. (default: 10) - * @param {string=} options.projectId - The projectId where the topic resource - * exists. - * @param {boolean=} options.reuseExisting - If the subscription already exists, + * @param {boolean} options.reuseExisting - If the subscription already exists, * reuse it. The options of the existing subscription are not changed. If * false, attempting to create a subscription that already exists will fail. * (default: false) @@ -189,27 +190,25 @@ PubSub.prototype.createTopic = function(name, callback) { * * @example * // Without specifying any options. - * var topic = 'messageTopic'; - * var sub = 'messageSubscription'; + * var topic = pubsub.topic('messageCenter'); + * var name = 'newMessages'; * - * pubsub.subscribe(topic, sub, function(err, subscription, apiResponse) {}); + * pubsub.subscribe(topic, name, function(err, subscription, apiResponse) {}); * * // With options. - * pubsub.subscribe(topic, sub, { + * pubsub.subscribe(topic, name, { * ackDeadlineSeconds: 90, * autoAck: true, * interval: 30 * }, function(err, subscription, apiResponse) {}); */ -PubSub.prototype.subscribe = function(topicName, subName, options, callback) { - var self = this; - - if (!topicName) { - throw new Error('A Topic name is required for a new subscription.'); +PubSub.prototype.subscribe = function(topic, subName, options, callback) { + if (!util.is(topic, 'string') && !(topic instanceof Topic)) { + throw new Error('A Topic is required for a new subscription.'); } if (!subName) { - throw new Error('A Subscription name is required for a new subscription.'); + throw new Error('A subscription name is required for a new subscription.'); } if (!callback) { @@ -217,26 +216,29 @@ PubSub.prototype.subscribe = function(topicName, subName, options, callback) { options = {}; } + options = options || {}; + + if (util.is(topic, 'string')) { + topic = this.topic(topic); + } + var body = { - topic: topicName + topic: topic.name }; if (options.ackDeadlineSeconds) { body.ackDeadlineSeconds = options.ackDeadlineSeconds; } - var projectId = options.projectId || this.projectId; + var subscription = topic.subscription(subName, options); - var path = Subscription.formatName_(projectId, subName); - - this.makeReq_('PUT', path, null, body, function(err, result) { - if (options.reuseExisting && err && err.code === 409) { - callback(null, self.subscription(subName, options), result); - } else if (err) { + this.makeReq_('PUT', subscription.name, null, body, function(err, result) { + if (err && !(err.code === 409 && options.reuseExisting)) { callback(err, null, result); - } else { - callback(null, self.subscription(subName, options), result); + return; } + + callback(null, subscription, result); }); }; @@ -276,7 +278,6 @@ PubSub.prototype.subscription = function(name, options) { options = options || {}; options.name = name; - return new Subscription(this, options); }; @@ -346,8 +347,23 @@ PubSub.prototype.getSubscriptions = function(options, callback) { options = options || {}; + var projectId = this.projectId; + var topicName; var query = {}; + if (util.is(options.topic, 'string')) { + topicName = options.topic; + } else if (options.topic instanceof Topic) { + // Return Subscriptions to the user under the Topic's PubSub instance. + self = options.topic.pubsub; + projectId = self.projectId; + topicName = options.topic.unformattedName; + } + + if (options.projectId) { + projectId = options.projectId; + } + if (options.pageSize) { query.pageSize = options.pageSize; } @@ -356,11 +372,9 @@ PubSub.prototype.getSubscriptions = function(options, callback) { query.pageToken = options.pageToken; } - var projectId = options.projectId || this.projectId; - var apiPath = util.format('{projectPath}{topicPath}/subscriptions', { projectPath: 'projects/' + projectId, - topicPath: options.topic ? '/topics/' + options.topic : '' + topicPath: topicName ? '/topics/' + topicName : '' }); this.makeReq_('GET', apiPath, query, null, function(err, result) { @@ -370,14 +384,13 @@ PubSub.prototype.getSubscriptions = function(options, callback) { } var subscriptions = (result.subscriptions || []).map(function(sub) { - // Depending on if we're using a subscriptions.list or - // topics.subscriptions.list API endpoint, we will get back a Subscription - // resource or just the name of the subscription. - var subName = sub.name || sub; - return new Subscription(self, { projectId: projectId, - name: subName + + // Depending on if we're using a subscriptions.list or + // topics.subscriptions.list API endpoint, we will get back a + // Subscription resource or just the name of the subscription. + name: sub.name || sub }); }); diff --git a/lib/pubsub/subscription.js b/lib/pubsub/subscription.js index e87e3077edc..ac5e10b244d 100644 --- a/lib/pubsub/subscription.js +++ b/lib/pubsub/subscription.js @@ -117,6 +117,7 @@ function Subscription(pubsub, options) { var projectId = options.projectId || pubsub.projectId; this.name = Subscription.formatName_(projectId, options.name); + this.unformattedName = options.name; this.makeReq_ = pubsub.makeReq_.bind(pubsub); diff --git a/lib/pubsub/topic.js b/lib/pubsub/topic.js index a6289b23fb5..101b3425b26 100644 --- a/lib/pubsub/topic.js +++ b/lib/pubsub/topic.js @@ -237,8 +237,8 @@ Topic.prototype.getSubscriptions = function(options, callback) { options = {}; } - options.projectId = this.pubsub.projectId; - options.topic = this.unformattedName; + options = options || {}; + options.topic = this; this.pubsub.getSubscriptions(options, callback); }; @@ -277,18 +277,8 @@ Topic.prototype.getSubscriptions = function(options, callback) { * interval: 30 * }, function(err, subscription, apiResponse) {}); */ -Topic.prototype.subscribe = function(name, options, callback) { - if (!callback) { - callback = options; - options = {}; - } - - var topicName = this.name; - - options = options || {}; - options.projectId = this.pubsub.projectId; - - return this.pubsub.subscribe(topicName, name, options, callback); +Topic.prototype.subscribe = function(subName, options, callback) { + return this.pubsub.subscribe(this, subName, options, callback); }; /** @@ -319,8 +309,6 @@ Topic.prototype.subscribe = function(name, options, callback) { * }); */ Topic.prototype.subscription = function(name, options) { - options = options || {}; - options.projectId = this.pubsub.projectId; return this.pubsub.subscription(name, options); };