const request = require('request');
const keyval = require('../keyval');

const username = 'arnelle-balane';
const endpoint = `https://medium.com/${username}/latest/`;

function fetchLatestMediumPost() {
    return keyval.get('medium').catch(() => {
        const options = {
            url: endpoint,
            headers: {
                'Accept': 'application/json'
            }
        };
        return new Promise((resolve, reject) => {
            request(options, (err, response, body) => {
                const index = body.indexOf('{');
                const data = JSON.parse(body.substring(index));
                return keyval.set('medium', data).then(resolve);
            });
        });
    });
}

exports.fetchLatestMediumPost = fetchLatestMediumPost;

const request = require('request');
const keyval = require('../keyval');

const username = 'arnelle-balane';
const endpoint = `https://medium.com/${username}/latest/`;

function fetchLatestMediumPosts() {
    return keyval.get('medium').catch(() => {
        const options = {
            url: endpoint,
            headers: {
                'Accept': 'application/json'
            }
        };
        return new Promise((resolve, reject) => {
            request(options, (err, response, body) => {
                const index = body.indexOf('{');
                const data = JSON.parse(body.substring(index));
                return keyval.set('medium', data.payload.posts).then(resolve);
            });
        });
    });
}

exports.fetchLatestMediumPosts = fetchLatestMediumPosts;

store medium posts data only to keyval store