Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
joefraley committed Sep 13, 2017
1 parent 6074503 commit 9fda808
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 80 deletions.
7 changes: 7 additions & 0 deletions config.local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
// proxy: {
// host: "127.0.0.1",
// port: 8888
// }
proxy: false
};
9 changes: 5 additions & 4 deletions src/parse-html.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { complement, compose, isNil, pickBy, toLower } = require("ramda");
const { compose } = require("ramda");
const { get } = require("./network");
const constants = require("./constants");
const cheerio = require("cheerio");
const parseAnimalFrom = require("./utils");
const S = require("sanctuary");

const detailPage = async detailUrl => {
const { data } = await get(`${detailUrl}`);
Expand All @@ -12,7 +13,7 @@ const detailPage = async detailUrl => {

const table = stats.find(constants.selectors.TABLE).children();

const parseDetail = compose(toLower, key =>
const parseDetail = S.compose(S.of(S.Maybe), key =>
$$(table)
.find(`th:contains("${key}")`)
.siblings("td")
Expand All @@ -21,7 +22,7 @@ const detailPage = async detailUrl => {
.trim()
);

const raw = pickBy(complement(isNil), {
const raw = {
adoptFee: parseDetail("Adopt Fee"),
age: parseDetail("Age"),
animalType: parseDetail("Type"),
Expand All @@ -36,7 +37,7 @@ const detailPage = async detailUrl => {
name: stats.find("h2").text(),
sex: parseDetail("Sex"),
weight: parseDetail("Weight")
});
};

return parseAnimalFrom(Date.now())(raw);
};
Expand Down
24 changes: 0 additions & 24 deletions src/schema/index.js

This file was deleted.

56 changes: 25 additions & 31 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
const getMonth = require("date-fns/get_month");
const getTime = require("date-fns/get_time");
const getYear = require("date-fns/get_year");
const parseDate = require("date-fns/parse");
const R = require("ramda");
const S = require("sanctuary");
const setMonth = require("date-fns/set_month");
const setYear = require("date-fns/set_year");

const {
always,
complement,
compose,
dec,
equals,
evolve,
findIndex,
head,
identity,
isNil,
map,
nth,
pickBy,
replace,
split,
toLower,
toUpper,
trim,
tryCatch
} = R;
const S = require("sanctuary");
const { Nothing, Just, Left, Right, Maybe } = S;
const setYear = require("date-fns/set_year");
const setMonth = require("date-fns/set_month");
const getYear = require("date-fns/get_year");
const getMonth = require("date-fns/get_month");
const getTime = require("date-fns/get_time");

const dropNonNumbers = replace(/[^0-9\.]+/g, ""); // eslint-disable-line no-useless-escape
const dropNonNumbers = R.replace(/[^0-9\.]+/g, ""); // eslint-disable-line no-useless-escape
const parseAdoptFee = R.compose(S.parseFloat, dropNonNumbers);

const parseAgeFrom = today =>
compose(list => {
Expand All @@ -48,23 +39,26 @@ const parseAgeFrom = today =>
return result;
}, split(" "));

const parseColor = R.compose(S.map(S.trim), S.splitOn(","), S.toLower);

module.exports = (date = Date.now()) =>
compose(
pickBy(complement(isNil)),
R.compose(
R.map(S.join),
R.pickBy(x => S.isJust(x)),
evolve({
adoptFee: S.compose(S.parseFloat, dropNonNumbers),
age: parseAgeFrom(date),
animalType: S.toLower,
breed: S.toLower,
color: R.compose(S.map(S.trim), S.splitOn(","), S.toLower),
dateAvailable: S.compose(getTime, parseDate),
description: S.trim,
adoptFee: S.map(parseAdoptFee),
age: S.map(x => S.Nothing(x)),
animalType: S.map(S.toLower),
breed: S.map(S.toLower),
color: S.map(parseColor),
dateAvailable: S.map(R.compose(getTime, parseDate)),
description: S.map(S.trim),
id: R.identity,
imageUrl: R.identity,
kennel: S.toUpper,
location: S.toLower,
kennel: S.map(S.toUpper),
location: R.identity,
name: R.identity,
sex: R.compose(S.head, S.splitOn(""), S.toUpper),
weight: S.parseFloat
sex: S.map(R.compose(S.head, S.splitOn(""), S.toUpper)),
weight: S.map(S.parseFloat)
})
);
1 change: 0 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Animal } from "../src/schema";
import { get } from "axios";
import { list } from "tcomb";
import listen from "test-listen";
Expand Down
42 changes: 22 additions & 20 deletions test/utils/parseAnimalFrom.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import { Animal } from "../../src/schema";
import parseAnimalFrom from "../../src/utils";
import data from "../fixtures";
import test from "ava";
import { compose, omit, keys, __, has } from "ramda";
import R from "ramda";
import S from "sanctuary";

const testDate = new Date("01/01/2000");
const parseAnimal = parseAnimalFrom(testDate);

test("returns a vaild animal", assert => {
const testDate = new Date("01/01/2000");
const animal = data();
const testAnimal = R.map(S.toMaybe, data());
const results = parseAnimal(testAnimal);

const actual = parseAnimalFrom(testDate)(animal);
const expected = S.keys(data());
const actual = S.keys(results);

assert.notThrows(() => Animal(actual));
assert.deepEqual(actual, expected);
});

test("omits all nil fields", assert => {
const testDate = new Date("01/01/2000");
const _default = data();
const animal = compose(omit(__, _default), keys)(_default);
test("does not omit Just(value)'s", assert => {
const noAge = R.omit(["age"]);
const animal = noAge(data());
const actual = parseAnimal(animal);
});

const actual = parseAnimalFrom(testDate)(animal);
test("omits all Nothing's", assert => {
const animal = S.map(R.always(S.Nothing), data({}));
const actual = parseAnimal(animal);
const expected = {};

assert.deepEqual(actual, expected);
});

test("omits any single nil fields", assert => {
const testDate = new Date("01/01/2000");
const animal = omit(["age"], data());

const actual = parseAnimalFrom(testDate)(animal);

assert.false(has("age", actual));
test("omits any Nothing", assert => {
const noAge = R.omit(["age"]);
const animal = noAge(data());
const actual = parseAnimal(animal);
});

0 comments on commit 9fda808

Please sign in to comment.