Skip to content

Commit

Permalink
Merge pull request #13 from alonbrody/skip-unsupported-api
Browse files Browse the repository at this point in the history
Skip unsupported api
  • Loading branch information
alonbrody authored Mar 7, 2017
2 parents fb60847 + 97e54fb commit d157d87
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var BASEURL = "https://graph.facebook.com/v2.5";
// cannot be loaded due to missing permissions,
// or does not support this operation
var MISSING_ERROR_CODE = 100;
var NOT_SUPPORTED_CODE = 3001;

//edge url for each node type
var EDGEMAP = {
Expand Down Expand Up @@ -71,7 +72,7 @@ FacebookInsightStream.prototype._handleData = function ( data ) {
}

FacebookInsightStream.prototype._init = function ( callback ) {
var options = this.options;
var options = this.options;

// building url pattern for all the request
var until = Date.now();
Expand All @@ -82,7 +83,7 @@ FacebookInsightStream.prototype._init = function ( callback ) {
until = Math.round( until / 1000 );
since = Math.round( since / 1000 );

var path = [
var path = [
BASEURL,
"{id}",
options.edge,
Expand Down Expand Up @@ -117,8 +118,8 @@ FacebookInsightStream.prototype._init = function ( callback ) {
}

// this url is urlPattern shared by all the requests
// each request using thie pattern should replace the
// {id} and {metric} place holders with real values
// each request using thie pattern should replace the
// {id} and {metric} place holders with real values
this.url = [ path, query ].join( "?" )

// options.itemlist is a function that can return either array of items or
Expand Down Expand Up @@ -150,10 +151,10 @@ FacebookInsightStream.prototype._initItem = function ( item ) {
};

var url = strReplace( "{base}/{id}?access_token={token}", model )

var title = "FACEBOOK " + options.node.toUpperCase();
console.log( new Date().toISOString(), title, url )

return request.getAsync( url )
.bind( this )
.get( 1 )
Expand Down Expand Up @@ -181,7 +182,7 @@ FacebookInsightStream.prototype._initItem = function ( item ) {
// _collect will be called once for each metric, the insight api request
// single api call for each metric, wich result in a list of values ( value per day)
// so in attempt to create one table with all the metrics,
// we are buffering each result in a key value map, with key for
// we are buffering each result in a key value map, with key for
// each day in the collected time range, and appending each value
// of the current metric to the appropriate key in the buffer.
// finally we generating single row for each day.
Expand Down Expand Up @@ -209,7 +210,7 @@ FacebookInsightStream.prototype._collect = function ( metrics, item, buffer, eve
this.emit( "progress", {
total: this.total,
loaded: ++this.loaded,
message: "{{remaining}} " + options.node + "s remaining"
message: "{{remaining}} " + options.node + "s remaining"
})
return data;
}
Expand Down Expand Up @@ -256,7 +257,7 @@ FacebookInsightStream.prototype._collect = function ( metrics, item, buffer, eve
.each( function ( val ) {
var key = val.end_time || val.time || 'lifetime';
// when using breakdowns we get numerous results for
// the same date therefore we need to identify unique
// the same date therefore we need to identify unique
// keys for the buffer by the date and different breakdowns
// we're using the '__' to later seperate the date
Object.keys( val.breakdowns || {} ).forEach( function ( b ){
Expand Down Expand Up @@ -317,8 +318,8 @@ FacebookInsightStream.prototype._collect = function ( metrics, item, buffer, eve
* that generated the error by calling to retry, otherwise it emmits error.
*
* Overide this method to create your own error handling and retrying mechanism
*
* @param {Error} error
*
* @param {Error} error
* @param {Function} retry the function that should be invoke to retry the process
*/
FacebookInsightStream.prototype.handleError = function ( error, retry ) {
Expand All @@ -329,14 +330,16 @@ FacebookInsightStream.prototype.handleError = function ( error, retry ) {
}
}

// predicate-based error filter
// predicate-based error filter
function SkippedError ( error ) {
return error.skip === true;
}

function errorHandler ( options, body ) {
if ( body.error ) {
body.error.skip = options.ignoreMissing && body.error.code === MISSING_ERROR_CODE
var missingItem = body.error.code === MISSING_ERROR_CODE
body.error.skip = (options.ignoreMissing && missingItem)
|| body.error.code === NOT_SUPPORTED_CODE

throw body.error
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "facebook-insight-stream",
"version": "1.0.5",
"version": "1.0.6",
"description": "Readable stream for reading facebook insights",
"main": "index.js",
"scripts": {
Expand Down
39 changes: 29 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,34 @@ describe( "Skip missing data", function () {
})
})

describe( "Skip missing item", function () {
var result = {};
var source = {
apps: [ 'myApp' ]
}
var _err = { message: 'skip error', code: 3001 };
var response = { "myApp": { error: _err, name: "myApp", data: dataGenerator( 1, null ) } }

before( initialize( result, response, source ) )
after( reset )

it( 'Skip missing item', function () {
var dataSize = Object.keys( result.data[ 0 ] ).length;
// the data size should be as the size of
// metrics + 2 columns ( date, name, id excluding the first metric )
assert.equal( dataSize, METRICS.length + 2 );
assert.equal( result.data.length, 1 )
})
})

describe( "error", function () {
var result = {};
var source = {
apps: [ "myApp" ],
}
var _err = { message: "test error" };
var response = { "myApp": { error: _err, name: "myApp" } }

before( initialize( result, response, source ) )
after( reset )

Expand Down Expand Up @@ -66,7 +86,7 @@ describe( "retry", function () {

describe( "progress", function () {
var result = {};
var source = {
var source = {
apps: [ "myApp" ],
};
var response = { "myApp": { data: dataGenerator( 1, null ), name: "myApp" } }
Expand Down Expand Up @@ -127,7 +147,7 @@ describe( "collect", function () {
apps: [ "myApp1", "myApp2" ],
}

var response = {
var response = {
"myApp1": { data: dataGenerator( 100, null ), name: "myApp1" },
"myApp2": { data: dataGenerator( 100, null ), name: "myApp2" }
}
Expand All @@ -147,9 +167,9 @@ function initialize( result, response, source ) {

result.batchCount = 0;

return function ( done ) {
return function ( done ) {

request.get = function ( url, callback ) {
request.get = function ( url, callback ) {
var metric;
url = url.split( BASEURL )[ 1 ];
var params = url.split( "?" )[ 0 ].split( "/" );
Expand All @@ -169,15 +189,15 @@ function initialize( result, response, source ) {
res = { name: appName };
} else if ( appError ) {
res = { error: appError };
response[ app ].error = null;
response[ app ].error = null;
} else {
res = { data: appData[ metric ] }
}

res = JSON.stringify( res )

callback( null, { 1: res } )
}
callback( null, { 1: res } )
}

var options = {
pastdays: "30",
Expand Down Expand Up @@ -245,5 +265,4 @@ function dataGenerator ( size, emptyMetric, name ) {
// also saving the app name
data.name = name;
return data
}

}

0 comments on commit d157d87

Please sign in to comment.