Skip to content

Commit

Permalink
crack two
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed May 21, 2015
1 parent 1d0c11c commit 82d316b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
93 changes: 53 additions & 40 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,74 +169,76 @@ 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)
* @param {function} callback - The callback function.
*
* @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) {
callback = options;
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);
});
};

Expand Down Expand Up @@ -276,7 +278,6 @@ PubSub.prototype.subscription = function(name, options) {

options = options || {};
options.name = name;

return new Subscription(this, options);
};

Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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
});
});

Expand Down
1 change: 1 addition & 0 deletions lib/pubsub/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 4 additions & 16 deletions lib/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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);
};

Expand Down

0 comments on commit 82d316b

Please sign in to comment.