├── .gitignore ├── .npmignore ├── README.md ├── package.json ├── LICENSE └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Facebook-Blendata 2 | ========= 3 | 4 | A small api for making requests to Facebook's Graph API. In house package. 5 | 6 | ## Installation 7 | 8 | npm install facebook-blendata --save 9 | 10 | ## Usage 11 | 12 | ``` 13 | import { FacebookApi } from 'facebook-blendata'; 14 | 15 | const apiKey = '1234'; 16 | const domain = 'www.yourDomain.com'; 17 | const privateKey = 'abc'; 18 | 19 | // initialize gravityForms instance 20 | const gravityForms = new GravityFormsAPI(apiKey, domain, privateKey); 21 | 22 | const method = 'GET'; 23 | const route = 'forms/1/entries'; 24 | 25 | // Create a signature for the route you want to interact with 26 | const signature = gravityForms.createSignature(method, route); 27 | 28 | // Make the api request 29 | const results = gravityForms.request(route, signature); 30 | ``` 31 | 32 | ## Release History 33 | 34 | * 0.0.1 Initial release 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "facebook-blendata", 3 | "version": "0.0.5", 4 | "description": "An inhouse package to manage facebook API calls", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "babel src --presets babel-preset-es2015 --out-dir lib", 8 | "prepublish": "npm run build" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/poetic/blendata-facebook" 13 | }, 14 | "keywords": [ 15 | "facebook" 16 | ], 17 | "author": "poetic systems", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/poetic/blendata-facebook/issues" 21 | }, 22 | "homepage": "https://github.com/poetic/blendata-facebook#readme", 23 | "devDependencies": { 24 | "babel-cli": "^6.8.0", 25 | "babel-preset-es2015": "^6.6.0" 26 | }, 27 | "dependencies": { 28 | "lodash": "^4.12.0", 29 | "request-promise": "^3.0.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Poetic 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import _ from 'lodash'; 2 | import request from 'request-promise'; 3 | 4 | class FacebookApi { 5 | constructor() { 6 | this._setup(); 7 | } 8 | 9 | _setup() { 10 | this.baseEndPoint = 'https://graph.facebook.com'; 11 | } 12 | 13 | _throwError(desc) { 14 | throw new Error(desc); 15 | } 16 | 17 | _get(uri) { 18 | if (!uri) { 19 | this._throwError('FacebookApi is missing required arguments'); 20 | } 21 | 22 | const requestOptions = { uri, json: true }; 23 | 24 | return request(requestOptions); 25 | } 26 | 27 | formatPageData(pageData, periods, filterFields) { 28 | const formattedData = {}; 29 | 30 | periods.forEach((period) => { 31 | const filteredPageData = pageData.filter(metric => metric.period === period); 32 | 33 | formattedData[period] = filteredPageData.map((metric) => { 34 | const mostRecentMetricValueObject = metric.values[metric.values.length - 1]; 35 | const formattedMetric = { 36 | name: metric.name, 37 | title: metric.title, 38 | value: mostRecentMetricValueObject.value, 39 | }; 40 | 41 | return formattedMetric; 42 | }); 43 | 44 | if (filterFields) { 45 | formattedData[period] = formattedData[period].filter( 46 | metric => _.includes(filterFields, metric.name) 47 | ); 48 | } 49 | }); 50 | 51 | return formattedData; 52 | } 53 | 54 | getPageData(pageId, pageToken) { 55 | const uri = `${this.baseEndPoint}/${pageId}/insights/?access_token=${pageToken}`; 56 | 57 | return this._get(uri); 58 | } 59 | 60 | getUserPages(longTermToken) { 61 | const formattedToken = longTermToken.split('=').pop(); 62 | const uri = `${this.baseEndPoint}/me/accounts?access_token=${formattedToken}`; 63 | 64 | return this._get(uri); 65 | } 66 | 67 | get60DayUserToken(appId, appSecret, tempToken) { 68 | const uri = ( 69 | `${this.baseEndPoint}/oauth/access_token?grant_type=fb_exchange_token&` + 70 | `client_id=${appId}&client_secret=${appSecret}&fb_exchange_token=${tempToken}` 71 | ); 72 | 73 | return this._get(uri); 74 | } 75 | } 76 | 77 | exports.FacebookApi = FacebookApi; 78 | --------------------------------------------------------------------------------