UNPKG

instagram-node-lib

Version:

This package is a wrapper for the Instagram API.

128 lines (105 loc) 4.23 kB
// Generated by CoffeeScript 1.3.3 (function() { var InstagramSubscriptions, crypto, url; url = require('url'); crypto = require('crypto'); InstagramSubscriptions = (function() { function InstagramSubscriptions(parent) { this.parent = parent; } /* Verification Methods */ InstagramSubscriptions.prototype.handshake = function(request, response, complete) { var body, headers, parsedRequest; parsedRequest = url.parse(request.url, true); if (parsedRequest['query']['hub.mode'] === 'subscribe' && (parsedRequest['query']['hub.challenge'] != null) && parsedRequest['query']['hub.challenge'].length > 0) { body = parsedRequest['query']['hub.challenge']; headers = { 'Content-Length': body.length, 'Content-Type': 'text/plain' }; response.writeHead(200, headers); response.write(body); if ((parsedRequest['query']['hub.verify_token'] != null) && (complete != null)) { complete(parsedRequest['query']['hub.verify_token']); } } else { response.writeHead(400); } return response.end(); }; InstagramSubscriptions.prototype.verified = function(request) { var calculated_signature, encoding, hmac; if (request.rawBody === null || request.headers['x-hub-signature'] === void 0 || request.headers['x-hub-signature'] === null) { return false; } hmac = crypto.createHmac('sha1', this.parent._config.client_secret); hmac.update(request.rawBody); calculated_signature = hmac.digest(encoding = 'hex'); if (calculated_signature !== request.headers['x-hub-signature']) { return false; } return true; }; /* Shared Subscription Methods */ InstagramSubscriptions.prototype._subscribe = function(params) { var i, _i, _len, _ref; params['method'] = "POST"; params['path'] = "/" + this.parent._api_version + "/subscriptions/"; if ((typeof params['callback_url'] === 'undefined' || params['callback_url'] === null) && this.parent._config.callback_url !== null) { params['callback_url'] = this.parent._config.callback_url; } params['post_data'] = { object: params['object'], aspect: 'media', client_id: this.parent._config.client_id, client_secret: this.parent._config.client_secret, callback_url: params['callback_url'] }; _ref = ['object_id', 'verify_token', 'lat', 'lng', 'radius']; for (_i = 0, _len = _ref.length; _i < _len; _i++) { i = _ref[_i]; if (params[i] != null) { params['post_data'][i] = params[i]; } } return this.parent._request(params); }; InstagramSubscriptions.prototype._unsubscribe = function(params) { params['method'] = "DELETE"; params['path'] = "/" + this.parent._api_version + "/subscriptions/"; if (params['id'] != null) { params['path'] += "?id=" + params['id']; } else { params['path'] += "?object=" + params['object']; } params['path'] += "&client_secret=" + this.parent._config.client_secret + "&client_id=" + this.parent._config.client_id; return this.parent._request(params); }; /* Shared Public Methods */ InstagramSubscriptions.prototype.subscribe = function(params) { return this._subscribe(params); }; InstagramSubscriptions.prototype.list = function(params) { params = this.parent._clone(params); params['path'] = "/" + this.parent._api_version + "/subscriptions?client_secret=" + this.parent._config.client_secret + "&client_id=" + this.parent._config.client_id; return this.parent._request(params); }; InstagramSubscriptions.prototype.unsubscribe = function(params) { return this._unsubscribe(params); }; InstagramSubscriptions.prototype.unsubscribe_all = function(params) { if (params['object'] === void 0 || !(params['object'] != null)) { params['object'] = 'all'; } return this._unsubscribe(params); }; return InstagramSubscriptions; })(); module.exports = InstagramSubscriptions; }).call(this);