Skip to content

Commit

Permalink
Merge 901755c into 0f575d0
Browse files Browse the repository at this point in the history
  • Loading branch information
randytarampi committed Aug 9, 2018
2 parents 0f575d0 + 901755c commit b155be7
Show file tree
Hide file tree
Showing 74 changed files with 3,909 additions and 540 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ before_install:
- lscpu
- npm install -g npm
before_script:
- npx lerna run pretest
- npx lerna exec -- npm run pretest
script:
- npm run travis
after_script:
after_failure:
- npm run kill --prefix packages/posts
after_success:
- npm run coveralls
stages:
- name: Test
if: tag IS blank AND NOT commit_message ~= /(?:^release\(travis\):)/
- name: Deploy (dev)
if: tag IS blank AND type = push AND branch = master AND NOT commit_message ~= /(?:^release\(travis\):)/
if: tag IS blank AND NOT commit_message ~= /(?:^release\(travis\):)/
- name: Release
if: tag IS blank AND type = push AND branch = master AND NOT commit_message ~= /(?:^release\(travis\):)/
- name: Deploy (prd)
Expand Down Expand Up @@ -190,3 +192,6 @@ jobs:
notifications:
slack:
secure: MTSym0Ymg+oYMx+Fmf3a2g2LyVtPJ8hg4sjtbfdyPBZAhO3rU9RxRFrBimNp9knZppwFpofosW4C6bKEBAN6p2AfeZOqQmYuRetZdS27pO6mdBpH56EJ7o2VJS/5ec9Lqw77802FO8XiRtWwySvxyE9nEHmm5dxOYf2JYj9OTiyNqmyRJFTRQiURZLOxn1CJLUI3OB8A0dtRU1hI3xx0kP+q7fHXp8xdNssB8ETBXwzG3HFE12VQRseCe01U0PdmGiRwQRQGXcGg00KTPFU1Gin7LY9mhSHRV9TsmvTtx6gXMYHo2UiLts0Omeb80k1nDZwBcAYsY4bIpgI1e01jJpDIHbg4lWskbnnSZWHG9QPIsOcG236Oqz/5bj+J70gRMw1zu+UYnH8XcfgcV+tBOu86oj8rtWeFGr58mh1HOiNedvqef2xNi+Yc3Ykj3ZfjVITlI/Lfpi6ZPNhjpL4Bbanmyd3KbbaYpdWkY5+SYJOhidq6VpHpBIqsACUwsLl6X8Yajteh5TLMK9ixkaTL97BT8niHTtEwkraMs5a0yfi9EDQNOjFERJq48Jrhpuq+MosgmyMmgX+31khzIgMr9+AUy3wL3dmIO198+Wm/k2rElYJmYuORI+txzIwYDGsaUbusyhy578Q5ilsiLbF1jZnPrvlQKVLCTIAMlXdmTpw=
addons:
hosts:
- dynamodb-local
8 changes: 3 additions & 5 deletions bin/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ ln -snf $REPO_ROOT/node_modules/lwip/ $REPO_ROOT/packages/pseudoimage/node_modul
# NOTE-RT: Need to symlink `jsonresume-theme-randytarampi` at the top level `node_modules` directory so `resume-cli` can find it
ln -snf $REPO_ROOT/packages/resume/ $REPO_ROOT/node_modules/jsonresume-theme-randytarampi

# FIXME-RT: `serverless` expects us to have a full `node_modules` tree living below us here. Can't tell if I want to hack around things by symlinking to the top level modules folder, so I'll just play it safe and install again.
cd $REPO_ROOT/packages/posts;
npm install;
rm package-lock.json;

# FIXME-RT: Consumers of `css` needs it have local references to style assets so we can pull them in during the build
ln -snf $REPO_ROOT/node_modules/materialize-css/ $REPO_ROOT/packages/css/node_modules/materialize-css
ln -snf $REPO_ROOT/node_modules/@fortawesome/ $REPO_ROOT/packages/css/node_modules/@fortawesome

# NOTE-RT: Need to symlink `dynamodb-localhost` into `posts`
ln -snf $REPO_ROOT/node_modules/dynamodb-localhost/ $REPO_ROOT/packages/posts/node_modules/dynamodb-localhost;

cd $REPO_ROOT;
npx lerna link;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"scripts": {
"clean": "npx lerna clean --yes; rm -rf node_modules package-lock.json packages/*/package-lock.json packages/*/dist packages/*/coverage packages/*/.nyc_output packages/*/.serverless packages/*/.webpack",
"postinstall": "./bin/bootstrap.sh",
"test": "npx lerna run test",
"cover": "rm -rf .nyc_output/ coverage/ && npx lerna run cover && mkdir .nyc_output/ coverage/ && npx lerna exec -- \"if [ -d \"./.nyc_output\" ]; then cp -rv .nyc_output ../..; fi;\" && npx nyc merge ./.nyc_output coverage/coverage.json && npx nyc report --reporter=lcov --temp-directory coverage && ls -al .nyc_output && ls -al coverage",
"test": "npx lerna exec -- npm test",
"cover": "rm -rf .nyc_output/ coverage/ && npx lerna exec -- npm run cover && mkdir .nyc_output/ coverage/ && npx lerna exec -- \"if [ -d \"./.nyc_output\" ]; then cp -rv .nyc_output ../..; fi;\" && npx nyc merge ./.nyc_output coverage/coverage.json && npx nyc report --reporter=lcov --temp-directory coverage && ls -al .nyc_output && ls -al coverage",
"coveralls": "cat coverage/lcov.info | npx coveralls",
"travis": "npm run cover && npm run coveralls"
"travis": "npm run cover && npm run coveralls",
"kill": "npx lerna run kill"
}
}
3 changes: 2 additions & 1 deletion packages/css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"pretest": "npx gulp -LLLL --color lint",
"postinstall": "npm run build",
"build": "npx gulp -LLLL --color build",
"test": "npx gulp -LLLL --color test"
"test": "echo \"What tests?\"",
"cover": "echo \"What tests?\""
},
"description": "Some common styles",
"bugs": {
Expand Down
4 changes: 3 additions & 1 deletion packages/js/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Creator from "./lib/creator";
import Photo from "./lib/photo";
import Post from "./lib/post";
import SizedPhoto from "./lib/sizedPhoto";
import * as util from "./lib/util";

export {
Creator,
Photo,
Post,
SizedPhoto
SizedPhoto,
util
};
3 changes: 2 additions & 1 deletion packages/js/lib/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Moment from "moment";
import Creator from "./creator";
import {compositeKeySeparator} from "./util";

class Post {
constructor(id, type, source, dateCreated, datePublished, title, body, sourceUrl, creator) {
Expand All @@ -15,7 +16,7 @@ class Post {
}

get uid() {
return `${this.source}-${this.id}`;
return `${this.source}${compositeKeySeparator}${this.id}`;
}

get datePublished() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Photo, Post} from "@randy.tarampi/js";
import Photo from "../photo";
import Post from "../post";

export default type => {
switch (type) {
Expand Down
2 changes: 2 additions & 0 deletions packages/js/lib/util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const compositeKeySeparator = "--@me/sep!-";
export {default as getEntityForType} from "./getEntityForType";
3 changes: 2 additions & 1 deletion packages/js/test/unit/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {expect} from "chai";
import Moment from "moment";
import Post from "../../lib/post";
import {compositeKeySeparator} from "../../lib/util";

describe("Post", () => {
describe("constructor", () => {
Expand Down Expand Up @@ -113,7 +114,7 @@ describe("Post", () => {
};
const post = Post.fromJSON(postJSON);

expect(post.uid).to.eql(`${postJSON.source}-${postJSON.id}`);
expect(post.uid).to.eql(`${postJSON.source}${compositeKeySeparator}${postJSON.id}`);
});
});
});
4 changes: 2 additions & 2 deletions packages/jsx/lib/api/fetchPosts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fetch from "isomorphic-fetch";
import getEntityForType from "../util/getEntityForType";
import {util} from "@randy.tarampi/js";

export default (fetchUrl, page) => {
return fetch(`${fetchUrl}?page=${page}`)
.then(body => body.json())
.then(posts => {
return posts.map((postJson) => {
const Constructor = getEntityForType(postJson.type);
const Constructor = util.getEntityForType(postJson.type);
return Constructor.fromJSON(postJson);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/posts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jspm_packages
dist
data/flickr
.webpack
.dynamoDb

# Environment variables

Expand Down
4 changes: 0 additions & 4 deletions packages/posts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ A post aggregator/blog thingy. For some usernames at some popular services, pull

Currently supports:
- [Unsplash](https://unsplash.com/documentation)
- [500px](https://github.com/500px/api-documentation)
- [Flickr](https://www.flickr.com/services/api/)
- [Instagram](https://www.instagram.com/developer/)
- [S3](https://github.com/Automattic/knox)
Expand Down Expand Up @@ -43,16 +42,13 @@ dev: &devConfig
acmArn: <an ACM ARN so serverless can setup for HTTPS>
kmsKeyArn: <a KMS ARN so serverless-secrets can pull API keys and other `environmentSecrets` out of the SSM store>
environment: &environment
F00PX_USER_NAME:
FLICKR_USER_NAME:
UNSPLASH_USER_NAME:
INSTAGRAM_USER_NAME:
INSTAGRAM_AUTH_REDIRECT_URI:
INSTAGRAM_USER_ID:
TUMBLR_USER_NAME:
environmentSecrets: &environmentSecrets
F00PX_API_KEY: f00px-api-key
F00PX_API_SECRET: f00px-api-secret
FLICKR_API_KEY: flickr-api-key
FLICKR_API_SECRET: flickr-api-secret
UNSPLASH_API_KEY: unsplash-api-key
Expand Down
2 changes: 1 addition & 1 deletion packages/posts/blog/me.service.client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "isomorphic-fetch";
import logger from "../logger";
import logger from "../lib/logger";

export default (fetchUrl, page) => {
return fetch(`${fetchUrl}?page=${page}`)
Expand Down
13 changes: 2 additions & 11 deletions packages/posts/blog/postSource.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
class PostSource {
constructor(type, client, initalizerPromise) {
constructor(type, client) {
this.type = type;
this.client = client;

if (initalizerPromise) {
this.initializing = initalizerPromise
.then((initializedClient) => {
this.client = initializedClient;
return this;
});
} else {
this.initializing = Promise.resolve(this);
}
this.initializing = Promise.resolve(this);
}

get isEnabled() {
Expand Down
2 changes: 1 addition & 1 deletion packages/posts/blog/searchPosts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash";
import logger from "../logger";
import logger from "../lib/logger";
import {initializePostSources} from "./postSources";

const searchPosts = postSearchParams => {
Expand Down
70 changes: 70 additions & 0 deletions packages/posts/db/models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import dynamoose from "dynamoose";
import logger from "../../lib/logger";
import PostSchema from "../schema/post";

const Post = dynamoose.model(process.env.POSTS_DYNAMODB_TABLE, PostSchema);

const postModelInstanceToEntity = postModelInstance => postModelInstance && postModelInstance.toEntity();

/**
* Persist a [Post]{@link Post}
* @param post {Post}
* @returns {Promise<Post>}
*/
export const createPost = async post => {
logger.debug(`[Post.createPost] persisting post (${post.uid})`);
const postModelInstance = await Post.create(post, {overwrite: true});
logger.debug(`[Post.createPost] persisted post (${JSON.stringify(postModelInstance && postModelInstance.uid)})`);
return postModelInstanceToEntity(postModelInstance);
};

/**
* Retrieve a single [Post]{@link Post} matching a [Post.uid]{@link Post.uid} or some combination of other attributes
* @param postUidOrParams {String|Object[]}
* @returns {Promise<Post>}
*/
export const getPost = async postUidOrParams => {
logger.debug(`[Post.getPost] retrieving post (${JSON.stringify(postUidOrParams)})`);
const queryParams = typeof postUidOrParams === "string"
? {uid: {eq: postUidOrParams}}
: postUidOrParams;
const postModelInstance = await Post.queryOne(queryParams).exec();
logger.debug(`[Post.getPost] retrieved post (${postModelInstance && postModelInstance.uid})`);
return postModelInstanceToEntity(postModelInstance);
};

/**
* Persist an array of [Posts]{@link Post}
* @param posts {Post[]}
* @returns {Promise<Post[]>}
*/
export const createPosts = async posts => {
logger.debug(`[Post.createPosts] persisting posts (${JSON.stringify(posts.map(post => post.uid))})`);
await Post.batchPut(posts, {overwrite: true});
const postModelInstances = await Post.batchGet(posts.map(post => post.uid)); // NOTE-RT: Ugh. This is gross af. According to the docs, `batchPut`should return some Dynamoose model instances, but if you look at their tests and source they just return a statement of success and what wasn't processed
logger.debug(`[Post.createPosts] persisted posts (${JSON.stringify(postModelInstances.map(postModelInstance => postModelInstance.uid))})`);
return postModelInstances.map(postModelInstanceToEntity);
};

/**
* Retrieve an array of [Posts]{@link Post} matching a [Post.uid]{@link Post.uid} or some combination of other attributes
* @param postUidsOrParams {Object}
* @returns {Promise<Post[]>}
*/
export const getPosts = async postUidsOrParams => {
logger.debug(`[Post.getPosts] retrieving posts (${JSON.stringify(postUidsOrParams)})`);
let postModelInstances;

if (postUidsOrParams instanceof Array) {
postModelInstances = await Post.batchGet(postUidsOrParams.map(postUid => {
return {uid: postUid};
}));
} else {
postModelInstances = await Post.query(postUidsOrParams).all().exec();
}

logger.debug(`[Post.getPosts] retrieved posts (${JSON.stringify(postModelInstances.map(postModelInstance => postModelInstance.uid))})`);
return postModelInstances.map(postModelInstanceToEntity);
};

export default Post;
75 changes: 75 additions & 0 deletions packages/posts/db/schema/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {Photo, Post, util} from "@randy.tarampi/js";
import {Schema} from "dynamoose";

const post = new Schema({
uid: {
type: String,
hashKey: true,
default: model => `${model.source}${util.compositeKeySeparator}${model.id}`
},
id: {
type: String,
required: true,
index: {
global: true
}
},
type: {
type: String,
required: true,
enum: [
Photo.name,
Post.name
],
index: {
global: true
}
},
source: {
type: String,
required: true,
index: {
global: true
}
},
datePublished: {
type: Date,
index: {
global: true
}
},
dateCreated: {
type: Date,
index: {
global: true
}
},
title: {
type: String
},
body: {
type: String
},
sourceUrl: {
type: String
},
creator: {
type: Object
},
sizedPhotos: {
type: [Object]
}
}, {
throughput: 1,
useNativeBooleans: false,
useDocumentTypes: false,
timestamps: true,
expires: 7 * 24 * 60 * 60
});

post.methods.toEntity = function () {
const Constructor = util.getEntityForType(this.type);
return Constructor.fromJSON(this);
};

export default post;
Loading

0 comments on commit b155be7

Please sign in to comment.