Skip to content

Instantly share code, notes, and snippets.

@zxqx
Created December 8, 2014 22:16
Show Gist options
  • Save zxqx/94add4ea13ab43960fc5 to your computer and use it in GitHub Desktop.
Save zxqx/94add4ea13ab43960fc5 to your computer and use it in GitHub Desktop.
import Promise from 'bluebird';
import extend from 'extend';
import urlUtil from 'url';
import _ from 'underscore';
import debug from 'debug';
// Models
import PostList from './models/PostList.js';
import InfluencerList from './models/InfluencerList.js';
import ContentList from './models/ContentList.js';
debug = debug('ContentStore');
// Content types
var ORIGINAL_CONTENT = 'original';
var SHARED_CONTENT = 'shared';
/**
* Fetch application content via transport layer
* Set up content as domain model objects
* @param {HttpTransport} transport
* @param {ConfigStore} config
* @constructor
*/
export default class ContentStore
{
constructor(transport, config)
{
this.transport = transport;
this.config = config;
}
/**
* Get top post data and instantiate PostList model
* @param {object} params
* @return {Promise}
*/
async getTopPost(params)
{
var config = this.config('content.topPost');
// Merge runtime params with static defaults
params = extend({}, config.params, params);
debug('getting top post');
var res = await this.transport.get(config.url, params);
debug('got top post');
return new PostList(res.body.data);
}
/**
* Get top influencers data and instantiate InfluencerList model
* @return {Promise}
*/
getTopInfluencers(params)
{
var config = this.config('content.topInfluencers');
// Merge runtime params with static defaults
params = extend({}, config.params, params);
debug('getting top influencers');
return this.transport.get(config.url, params)
.then(res => {
debug('got top influencers');
var uniqueInfluencers = _.uniq(res.body.data, x => x.actor.id);
return new InfluencerList(uniqueInfluencers);
});
}
/**
* Aggregate top streaming original and shared content into a single collection
* @param {object} params
* @return {Promise}
*/
getTopStreamingContent(params)
{
debug('getting top streaming content');
return Promise.all([
this.getTopStreamingOriginalContent(params),
this.getTopStreamingSharedContent(params)
])
.then(res => {
debug('got top streaming content');
var originalContent = res[0];
//var sharedContent = res[1];
//originalContent.add(sharedContent.models);
return originalContent;
});
}
/**
* Get top original content data and instantiate ContentList collection
* @param {object} params
* @return {Promise}
*/
getTopStreamingOriginalContent(params)
{
var config = this.config('content.topStreamingOriginalContent');
// Merge runtime params with static defaults
params = extend({}, config.params, params);
return this.transport.get(config.url, params)
.then(res => new ContentList(res.body.data, ORIGINAL_CONTENT));
}
/**
* Get top shared content data and instantiate ContentList collection
* @param {object} params
* @return {Promise}
*/
getTopStreamingSharedContent(params)
{
var config = this.config('content.topStreamingSharedContent');
// Merge runtime params with static defaults
params = extend({}, config.params, params);
return this.transport.get(config.url, params)
.then(res => new ContentList(res.body.data, SHARED_CONTENT));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment