<commit_before>// Instagram doesn’t allow CORS, so we need to use JSONP.
import fetchJSONP from 'fetch-jsonp';

// Use Bemmit for fast BEM class names.
import bemmit from 'bemmit';

// Only import the functions we need from Ramda.
import compose from 'ramda/src/compose';
import curry from 'ramda/src/curry';
import join from 'ramda/src/join';
import mapObjIndexed from 'ramda/src/mapObjIndexed';
import test from 'ramda/src/test';
import values from 'ramda/src/values';

// Create a debugger.
import debug from 'debug';
const log = debug('instagram-feed-reader');

log('Debugging is enabled!');

// Alias this long-ass function name for brevity.
const esc = encodeURIComponent;

const formatArgs = mapObjIndexed((arg, key) => `${esc(key)}=${esc(arg)}`);
const getQueryString = compose(join('&'), values, formatArgs);

const buildLoginLink = args => {
  const queryString = getQueryString(args);
  const loginLink = `${IG_API_OAUTH}?${queryString}`;

  log(`loginLink: ${loginLink}`);

  const getClass = bemmit('instagram-feed');
  const loginClass = getClass('auth');

  return `
    <a href="${loginLink}" class="${loginClass}">Authorize Instagram</a>
  `;
};

const unsafe = {
  renderStringToDOM: curry((selector, htmlStr) => {

    // This is a side-effect.
    document.querySelector(selector).innerHTML = htmlStr;
  }),
};

// Create a shortcut for rendering into our app’s wrapper element.
const render = unsafe.renderStringToDOM('#app');

const showLogin = () => {
  const args = {
    client_id: IG_CLIENT_ID,
    redirect_uri: IG_REDIRECT_URI,
    response_type: 'token',
  };

  render(buildLoginLink(args));
};

const isLoggedIn = test(/^#access_token=/);
const getToken = replace('#access_token=', '');
const addTokenToArgs = assoc('access_token');
const getQueryStringWithToken = compose(getQueryString, addTokenToArgs);

export default function initialize() {
  if (isLoggedIn(document.location.hash)) {

    // TODO load user media from Instagram

  } else {
    showLogin();
  }
}<commit_msg>feat(pt3): code as described in pt3<commit_after>// Instagram doesn’t allow CORS, so we need to use JSONP.
import fetchJSONP from 'fetch-jsonp';

// Use Bemmit for fast BEM class names.
import bemmit from 'bemmit';

// Only import the functions we need from Ramda.
import assoc from 'ramda/src/assoc';
import compose from 'ramda/src/compose';
import concat from 'ramda/src/concat';
import curry from 'ramda/src/curry';
import join from 'ramda/src/join';
import mapObjIndexed from 'ramda/src/mapObjIndexed';
import replace from 'ramda/src/replace';
import tap from 'ramda/src/tap';
import test from 'ramda/src/test';
import values from 'ramda/src/values';

// Create a debugger.
import debug from 'debug';
const log = debug('instagram-feed-reader');

// Logs whatever is passed, then returns it unchanged.
const logAndReturn = tap(log);

log('Debugging is enabled!');

// Alias this long-ass function name for brevity.
const esc = encodeURIComponent;

const formatArgs = mapObjIndexed((arg, key) => `${esc(key)}=${esc(arg)}`);
const getQueryString = compose(join('&'), values, formatArgs);

const buildLoginLink = args => {
  const queryString = getQueryString(args);
  const loginLink = `${IG_API_OAUTH}?${queryString}`;

  log(`loginLink: ${loginLink}`);

  const getClass = bemmit('instagram-feed');
  const loginClass = getClass('auth');

  return `
    <a href="${loginLink}" class="${loginClass}">Authorize Instagram</a>
  `;
};

// Anything with side-effects goes in here to make it really obvious.
const unsafe = {
  renderStringToDOM: curry((selector, htmlStr) => {

    // This is a side-effect.
    document.querySelector(selector).innerHTML = htmlStr;
  }),

  fetchMediaAsJSON: endpoint => {
    return fetchJSONP(endpoint)
      .then(data => data.json())
      .then(logAndReturn);
  },
};

// Create a shortcut for rendering into our app’s wrapper element.
const render = unsafe.renderStringToDOM('#app');

const showLogin = () => {
  const args = {
    client_id: IG_CLIENT_ID,
    redirect_uri: IG_REDIRECT_URI,
    response_type: 'token',
  };

  render(buildLoginLink(args));
};

const isLoggedIn = test(/^#access_token=/);
const getToken = replace('#access_token=', '');
const addTokenToArgs = assoc('access_token');
const getQueryStringWithToken = compose(getQueryString, addTokenToArgs);
const getRecentMediaEndpoint = concat(`${IG_API_SELF_MEDIA_RECENT}?`);
const buildRequestURI = compose(getRecentMediaEndpoint, getQueryStringWithToken);

const showPhotos = (args = { count: 16 }) => {
  const token = getToken(document.location.hash);
  const endpoint = buildRequestURI(token, args);

  unsafe.fetchMediaAsJSON(endpoint);
};

export default function initialize(args) {
  if (isLoggedIn(document.location.hash)) {
    showPhotos(args);
  } else {
    showLogin();
  }
}
{'fraction_non_alphanumeric': 0.13902185223725286, 'fraction_numerical': 0.0008324661810613944, 'mean_word_length': 4.748803827751196, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1440587', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1513, 'n_words': 415}