Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
feat: add feed.browse
Browse files Browse the repository at this point in the history
  • Loading branch information
oureta committed Jan 27, 2016
1 parent 7c23989 commit 55d1818
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 36 deletions.
19 changes: 2 additions & 17 deletions dist/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ var _error = require('./lib/error');

var _error2 = _interopRequireDefault(_error);

var _cookie = require('cookie');

var _cookie2 = _interopRequireDefault(_cookie);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
Expand Down Expand Up @@ -90,24 +86,13 @@ function check() {
}

return (0, _fetch.post)('auth/login', { login: login, password: password }, config).then(function (response) {
var cookies = _cookie2.default.parse(response.headers.get('set-cookie'));

// get subdomain / environemnt
var matched = /^https?:\/\/([^\.]+)\./.exec(response.url);
var sessionName = 'playerme_session';

if (matched) {
var subdomain = matched[1];
sessionName = subdomain + '_' + sessionName;
}

var playermeSession = cookies[sessionName];
var cookie = response.headers.get('set-cookie');

return response.json().then(function (responseJSON) {
// inject session key into response result
var resultWithSessionKey = _extends({}, responseJSON, {
results: _extends({}, responseJSON.results, {
playerme_session: playermeSession
cookie: cookie
})
});

Expand Down
15 changes: 15 additions & 0 deletions dist/feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.browse = browse;

var _fetch = require('./lib/fetch');

function browse() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var config = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

return (0, _fetch.get)('feed', args, config).then(_fetch.postProcess);
}
36 changes: 35 additions & 1 deletion dist/lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _fetch;
exports.get = get;
exports.post = post;
exports.postProcess = postProcess;
var stockFetch = undefined;
Expand All @@ -28,12 +29,45 @@ function _fetch(endpoint) {

var baseURL = config.baseURL || BASE_URL;

var additionalHeaders = {};
var cookie = config.cookie;

if (cookie) {
additionalHeaders.Cookie = cookie;
}

var headers = _extends({}, JSON_HEADERS, additionalHeaders);

return stockFetch(baseURL + '/' + endpoint, _extends({
headers: JSON_HEADERS,
headers: headers,
method: 'GET'
}, config));
}

function _serialize() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

var query = [];

for (var key in args) {
if (args.hasOwnProperty(key)) {
var name = encodeURIComponent(key);
var value = encodeURIComponent(args[key]);
query.push(name + '=' + value);
}
}

return query.join('&');
}

function get(endpoint, args) {
var config = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

var queryString = _serialize(args);

return _fetch(endpoint + (queryString ? '?' + queryString : ''), _extends({ method: 'GET' }, config));
}

function post(endpoint, args) {
var config = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

Expand Down
17 changes: 2 additions & 15 deletions src/auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { post, postProcess } from './lib/fetch';
import error from './lib/error';

import Cookie from 'cookie';

/**
* @module auth
*
Expand Down Expand Up @@ -66,26 +64,15 @@ export function check(args = {}, config = {}) {

return post('auth/login', { login, password }, config)
.then(response => {
const cookies = Cookie.parse(response.headers.get('set-cookie'));

// get subdomain / environemnt
const matched = /^https?:\/\/([^\.]+)\./.exec(response.url);
let sessionName = 'playerme_session';

if (matched) {
const subdomain = matched[1];
sessionName = `${subdomain}_${sessionName}`;
}

const playermeSession = cookies[sessionName];
const cookie = response.headers.get('set-cookie');

return response.json().then(responseJSON => {
// inject session key into response result
const resultWithSessionKey = {
...responseJSON,
results: {
...responseJSON.results,
playerme_session: playermeSession
cookie
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { get, postProcess } from './lib/fetch';

export function browse(args = {}, config = {}) {
return get('feed', args, config)
.then(postProcess);
}
37 changes: 36 additions & 1 deletion src/lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,50 @@ const BASE_URL = process.env.BASE_URL || 'https://player.me/api/v1';
export default function _fetch(endpoint, config = {}) {
const baseURL = config.baseURL || BASE_URL;

const additionalHeaders = {};
const { cookie } = config;
if (cookie) {
additionalHeaders.Cookie = cookie;
}

const headers = {
...JSON_HEADERS,
...additionalHeaders
};

return stockFetch(
`${baseURL}/${endpoint}`, {
headers: JSON_HEADERS,
headers,
method: 'GET',
...config
}
);
}

function _serialize(args = {}) {
const query = [];

for (const key in args) {
if (args.hasOwnProperty(key)) {
const name = encodeURIComponent(key);
const value = encodeURIComponent(args[key]);
query.push(`${name}=${value}`);
}
}

return query.join('&');
}

export function get(endpoint, args, config = {}) {
const queryString = _serialize(args);

return _fetch(
endpoint + (queryString ? '?' + queryString : ''),
{ method: 'GET', ...config }
);
}


export function post(endpoint, args, config = {}) {
return _fetch(endpoint, {
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion test-dist/auth/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _auth = require('../../dist/auth');
_chai.assert.typeOf(user, 'object');
_chai.assert.ok(user.id);
_chai.assert.equal(user.username, login);
_chai.assert.ok(user.playerme_session);
_chai.assert.ok(user.cookie);
}, done);
});
});
25 changes: 25 additions & 0 deletions test-dist/feed/browse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var _mocha = require('mocha');

var _chai = require('chai');

var _utils = require('../lib/utils');

var _feed = require('../../dist/feed');

(0, _mocha.describe)('feed.browse', function () {
var cookie = process.env.COOKIE;

(0, _mocha.it)('should be defined,\n return a Promise,\n and fail if no authenticated', function (done) {
_chai.assert.ok(_feed.browse);
_chai.assert.typeOf((0, _feed.browse)(), 'Promise');
(0, _utils.shouldFail)((0, _feed.browse)(), done);
});

(0, _mocha.it)('should succeed if authenticated', function (done) {
(0, _utils.shouldSucceed)((0, _feed.browse)({}, { cookie: cookie }), function (feeds) {
_chai.assert.isArray(feeds);
}, done);
});
});
2 changes: 1 addition & 1 deletion test/auth/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('auth.check', () => {
assert.typeOf(user, 'object');
assert.ok(user.id);
assert.equal(user.username, login);
assert.ok(user.playerme_session);
assert.ok(user.cookie);
},
done
)
Expand Down
33 changes: 33 additions & 0 deletions test/feed/browse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, it } from 'mocha';
import { assert } from 'chai';
import { shouldFail, shouldSucceed } from '../lib/utils';

import { browse } from '../../dist/feed';

describe('feed.browse', () => {
const cookie = process.env.COOKIE;

it(
`should be defined,
return a Promise,
and fail if no authenticated`,
done => {
assert.ok(browse);
assert.typeOf(browse(), 'Promise');
shouldFail(browse(), done);
}
);

it(
`should succeed if authenticated`,
done => {
shouldSucceed(
browse({}, { cookie }),
feeds => {
assert.isArray(feeds);
},
done
);
}
);
});

0 comments on commit 55d1818

Please sign in to comment.