Skip to content
Browse files

Lots of changes.

  • Loading branch information...
1 parent 3e5843d commit 27f8593072910c1fbcd926ffe689e497337a8834 @tmarshall committed Jan 4, 2012
Showing with 272 additions and 66 deletions.
  1. +1 −1 LICENSE
  2. +59 −6 README.md
  3. +211 −58 google-plus-api.js
  4. +1 −1 package.json
View
2 LICENSE
@@ -1,4 +1,4 @@
-Copyright 2011 Timothy J Marshall.
+Copyright 2011,2012 Timothy J Marshall.
All rights reserved.
Permission is hereby granted, free of charge, to any person
View
65 README.md
@@ -1,4 +1,4 @@
-Google+ API 0.0.1
+Google+ API 0.1.1
=================
## What's this?
@@ -44,24 +44,77 @@ Getting a user's activities looks something like:
```javascript
// id, callback
-plus.getActivities('106180961098165812195', function(err, res) {});
+plus.getPublicActivities('106180961098165812195', function(err, res) {});
// id, options, callback
-plus.getActivities('106180961098165812195', { maxResults: 10 }, function(err, res) {});
+plus.getPublicActivities('106180961098165812195', { maxResults: 10 }, function(err, res) {});
```
+Another way of doing it is:
+
+```javascript
+// id, collection, callback
+plus.getActivities('106180961098165812195', 'public', function(err, res) {});
+```
+
+Right now the only available collection is 'public,' but this allows some flexibility if and when that changes.
+
## Getting a specific activity
Once you have a list of activities from a user, you can then get more information for a specific one:
```javascript
// activity id, callback
plus.getActivity('z134sdlycvycc5ufd22pedkz0rbijjnzm04', function(err, res) {});
+```
-// id, options, callback
-plus.getActivity('z134sdlycvycc5ufd22pedkz0rbijjnzm04', { alt: 'json' }, function(err, res) {});
+## Getting more about a specific activity
+
+One of the core features of Google+ is the 'Plus One,' similar to Facebook's 'Like.' You can find a list of 'Plusoners' like this:
+
+```javascript
+// activity id, collection, callback
+plus.getActivityPeople('z134sdlycvycc5ufd22pedkz0rbijjnzm04', 'plusoners', function(err, res) {});
+```
+
+The collection types are currently 'plusoners' and 'resharers.' You can also access this via a shortcut:
+
+```javascript
+// activity id, callback
+plus.getActivityPlusoners('z134sdlycvycc5ufd22pedkz0rbijjnzm04', function(err, res) {});
+
+// activity id, callback
+plus.getActivityResharers('z134sdlycvycc5ufd22pedkz0rbijjnzm04', function(err, res) {});
+```
+
+You may also want to dig deeper and look at the comments for the activity.
+
+```javascript
+// activity id, callback
+plus.getActivityComments('z134sdlycvycc5ufd22pedkz0rbijjnzm04', function(err, res) {});
+```
+
+## Getting a specific comment
+
+As seen above you can retrieve the list of comments for a specific activity. But maybe you'd like to simply retrieve a single comment.
+
+```javascript
+// comment id, callback
+plus.getComment('i_gHb6AxAiPxyLxjJaW0gMItm0Eark-KwrOudJQ8F8Unt0muh-y4stTqGy_Tl24DKXYJWezdgDzlLveXdYO-Fg', function(err, res) {});
+```
+
+## Searching
+
+There are currently two types of searches available; Public activities and profiles.
+
+```javascript
+// query, callback
+plus.searchActivities('2012 election', function(err, res) {});
+
+// query, callback
+plus.searchProfiles('Marshall', function(err, res) {});
```
## Future updates
-The Google+ API just came out. So this will likely be updated in the near future.
+The Google+ API is still pretty young. There have already been a number of changes, and there are bound to be more. This module is subject to change.
View
269 google-plus-api.js
@@ -1,74 +1,227 @@
var https = require('https');
+/**
+ Base function
+
+ Takes your API key
+*/
function GooglePlus(apiKey) {
if(apiKey === undefined) {
throw 'No API key defined';
return;
}
- this.getProfile = function(id, b, c /* id, callback || id, opts, callback */) {
- return makeRequest('/plus/v1/people/' + id, c === undefined ? {} : b, c === undefined ? b : c);
- };
-
- this.getActivites = function(id, b, c /* id, callback || id, opts, callback */) {
- return makeRequest('/plus/v1/people/' + id + '/activities/public', c === undefined ? {} : b, c === undefined ? b : c);
- };
-
- this.getActivity = function(id, b, c /* id, callback || id, opts, callback */) {
- return makeRequest('/plus/v1/activities/' + id, c === undefined ? {} : b, c === undefined ? b : c);
- };
-
- function makeRequest(path, opts, callback) {
- var
- key,
- req,
- dataStr = '';
-
- if(callback === undefined) {
- throw 'No callback defined';
- return;
- }
-
- path += '?key=' + apiKey;
- for(key in opts) {
- path += '&' + key + '=' + opts[key];
- }
-
- req = https.request({
- host: 'www.googleapis.com',
- port: 443,
- path: path,
- method: 'GET'
- }, function(res) {
- res.on('data', function(data) {
- dataStr += data;
- });
-
- res.on('end', function() {
- if(opts.alt === undefined || opts.alt.toLowerCase() == 'json') {
- try {
- callback(null, JSON.parse(dataStr));
- } catch(err) {
- callback(null, dataStr);
- }
- }
- else {
+ this.apiKey = apiKey;
+
+ return this;
+}
+
+/**
+ Lists a user's activities in a given collection type
+ https://developers.google.com/+/api/latest/activities/list
+
+ First argument must be the user's Id
+ Second argument must be the collection type. Can be "public"
+ Arguments may be (id, collection, callback) or (id, collection, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getActivities = function(id, collection, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'people/' + id + '/activities/' + collection, b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Retrieves a single activity
+ https://developers.google.com/+/api/latest/activities/get
+
+ First argument must be the activity Id
+ Arguments may be (id, callback) or (id, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getActivity = function(id, a, b /* id, callback || id, opts, callback */) {
+ return makeRequest(this.apiKey, 'activities/' + id, b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Lists the comments for a given activity
+ https://developers.google.com/+/api/latest/comments/list
+
+ First argument must be the activity Id
+ Arguments may be (id, callback) or (id, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getActivityComments = function(id, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'activities/' + id + '/comments', b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Lists people who are in a given collection, for a given activity
+ https://developers.google.com/+/api/latest/people/listByActivity
+
+ First argument must be the activity Id
+ Second argument must be the collection type. Can be "plusoners" or "resharers"
+ Arguments may be (id, collection, callback) or (id, collection, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getActivityPeople = function(id, collection, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'activities/' + id + '/people/' + collection, b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Lists people who gave the given activity a plus one
+ https://developers.google.com/+/api/latest/people/listByActivity
+
+ First argument must be the activity Id
+ Arguments may be (id, callback) or (id, options, callback)
+*/
+GooglePlus.prototype.getActivityPlusoners = function(id, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'activities/' + id + '/people/plusoners', b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Lists people who reshared the given activity
+ https://developers.google.com/+/api/latest/people/listByActivity
+
+ First argument must be the activity Id
+ Arguments may be (id, callback) or (id, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getActivityResharers = function(id, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'activities/' + id + '/people/resharers', b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Gets a specific comment
+ https://developers.google.com/+/api/latest/comments/get
+
+ First argument must be the comment Id
+ Arguments may be (id, callback) or (id, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getComment = function(id, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'comments/' + id, b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Gets a user's profile
+ https://developers.google.com/+/api/latest/people/get
+
+ First argument must be the user's Id
+ Arguments may be (id, callback) or (id, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getProfile = function(id, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'people/' + id, b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Lists a user's public activity
+ https://developers.google.com/+/api/latest/activities/list
+
+ First argument must be the user's Id
+ Arguments may be (id, callback) or (id, options, callback)
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.getPublicActivities = function(id, a, b /* id, callback || id, options, callback */) {
+ return makeRequest(this.apiKey, 'people/' + id + '/activities/public', b === undefined ? {} : a, b === undefined ? a : b);
+};
+
+/**
+ Searches public activities
+ https://developers.google.com/+/api/latest/activities/search
+
+ First argument must be the search string
+ Arguments may be (id, callback) or (id, options, callback)
+ The search string will be placed into the options (wheter options is given or not), and will overwrite any existing key of 'query'
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.searchActivities = function(query, a, b /* id, callback || id, opts, callback */) {
+ var options = b === undefined ? {} : a;
+ options.query = query;
+ return makeRequest(this.apiKey, 'activities', options, b === undefined ? a : b);
+};
+
+/**
+ Searches public profiles
+ https://developers.google.com/+/api/latest/people/search
+
+ First argument must be the search string
+ Arguments may be (id, callback) or (id, options, callback)
+ The search string will be placed into the options (wheter options is given or not), and will overwrite any existing key of 'query'
+
+ Returns the HTTP request instance
+*/
+GooglePlus.prototype.searchProfiles = function(query, a, b /* id, callback || id, opts, callback */) {
+ var options = b === undefined ? {} : a;
+ options.query = query;
+ return makeRequest(this.apiKey, 'people', options, b === undefined ? a : b);
+};
+
+/**
+ Private function, used to make requests to G+
+
+ Takes the path, any options (becomes query params) & a callback
+
+ Returns the HTTP request instance
+*/
+function makeRequest(apiKey, path, opts, callback) {
+ var
+ key,
+ req,
+ dataStr = '';
+
+ if(callback === undefined) {
+ throw 'No callback defined';
+ return;
+ }
+
+ path = '/plus/v1/' + path + '?key=' + apiKey;
+ for(key in opts) {
+ path += '&' + key + '=' + opts[key];
+ }
+
+ req = https.request({
+ host: 'www.googleapis.com',
+ port: 443,
+ path: path,
+ method: 'GET'
+ }, function(res) {
+ res.on('data', function(data) {
+ dataStr += data;
+ });
+
+ res.on('end', function() {
+ if(opts.alt === undefined || opts.alt.toLowerCase() == 'json') {
+ try {
+ callback(null, JSON.parse(dataStr));
+ } catch(err) {
callback(null, dataStr);
}
- });
-
- res.on('close', function () {
- res.emit('end');
- });
+ }
+ else {
+ callback(null, dataStr);
+ }
});
- req.end();
- req.on('error', function(err) {
- callback(err);
+ res.on('close', function () {
+ res.emit('end');
});
+ });
+ req.end();
+
+ req.on('error', function(err) {
+ callback(err);
+ });
- return req;
- }
+ return req;
}
-module.exports = GooglePlus;
+module.exports = GooglePlus;
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Timothy J. Marshall <timothyjmarshall@gmail.com> (http://timothyjmarshall.com)",
"name": "Google_Plus_API",
"description": "Employs the official Google+ API",
- "version": "0.0.1",
+ "version": "0.1.1",
"main": "google-plus-api",
"engines": {
"node": ">=0.4.8"

0 comments on commit 27f8593

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