Skip to content

Commit

Permalink
added stitch app export
Browse files Browse the repository at this point in the history
  • Loading branch information
aydrian committed Jun 18, 2018
1 parent edc004f commit fe281a5
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
9 changes: 9 additions & 0 deletions deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
source .env
echo Logging into Stitch
stitch-cli login --username=$STITCH_USERNAME --api-key=$STITCH_API_KEY
echo Importing from project directory
stitch-cli import --app-id=giphyresponder-mspiu --path=./stitch-app --strategy=merge
echo Logging out
stitch-cli logout
echo Deploy complete
13 changes: 13 additions & 0 deletions export
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
source .env
echo Logging into Stitch
stitch-cli login --username=$STITCH_USERNAME --api-key=$STITCH_API_KEY
echo Exporting Project to temp directory
rm -rf tmp
stitch-cli export --app-id=giphyresponder-mspiu --output=./tmp
echo Copying to project directory
cp -r ./tmp/. ./stitch-app
rm -rf tmp
echo Logging out
stitch-cli logout
echo Export complete
6 changes: 6 additions & 0 deletions stitch-app/auth_providers/anon-user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "5aa022b5b8b998813e540599",
"name": "anon-user",
"type": "anon-user",
"disabled": false
}
6 changes: 6 additions & 0 deletions stitch-app/auth_providers/api-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "5aa017b446224c87296031b9",
"name": "api-key",
"type": "api-key",
"disabled": false
}
5 changes: 5 additions & 0 deletions stitch-app/functions/GiphySearch/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "5aa02c65b8b998813e6e8200",
"name": "GiphySearch",
"private": false
}
31 changes: 31 additions & 0 deletions stitch-app/functions/GiphySearch/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
exports = function(search){
const giphyService = context.services.get("GiphyAPI");
return giphyService.get({
scheme: "http",
host: "api.giphy.com",
path: "/v1/gifs/search",
query: {
"api_key": ["dc6zaTOxFJmzC"],
"q": [search],
"rating": ["pg"],
"limit": ["5"]
},
headers: {
"Content-Type": [
"application/json"
]
}
}).then(payload => {
const body = EJSON.parse(payload.body.text());
const data = body.data;
let gifs = data.map(gif => {
return {
src: gif.images.fixed_height_small.url,
url: gif.url,
id: gif.id,
title: gif.title
};
});
return gifs;
});
};
5 changes: 5 additions & 0 deletions stitch-app/functions/SendEmail/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "5aa035b28f25b971a62d59c4",
"name": "SendEmail",
"private": false
}
29 changes: 29 additions & 0 deletions stitch-app/functions/SendEmail/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
exports = function(recipient, subData){
const sparkpostService = context.services.get("SparkPost");
return sparkpostService.post({
url: "https://api.sparkpost.com/api/v1/transmissions",
body: JSON.stringify({
"campaign_id": "giphy-responder",
"recipients": [
{
"address": recipient
}
],
"content": {
"template_id": "giphy-responder"
},
"substitution_data": subData
}),
headers: {
"Authorization": [
context.values.get("sp-api-key")
],
"Content-Type": [
"application/json"
]
}
}).then(payload => {
const body = EJSON.parse(payload.body.text());
return body.results;
});
};
7 changes: 7 additions & 0 deletions stitch-app/services/GiphyAPI/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "5aa02b5acfb737497eb9bf42",
"name": "GiphyAPI",
"type": "http",
"config": {},
"version": 1
}
10 changes: 10 additions & 0 deletions stitch-app/services/GiphyAPI/rules/GET.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "5aa02bc7b8b998813e6d2983",
"name": "GET",
"actions": [
"get"
],
"when": {
"%%args.url.host": "api.giphy.com"
}
}
7 changes: 7 additions & 0 deletions stitch-app/services/SparkPost/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "5aa01881b8b998813e42cfb6",
"name": "SparkPost",
"type": "http",
"config": {},
"version": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "5aa018d98f25b971a6f5ee77",
"name": "incoming",
"run_as_user_id": "",
"run_as_user_id_script_source": "",
"options": {
"httpMethod": "POST",
"secret": "purplemonkeydishwasher",
"secretAsQueryParam": true
},
"respond_result": false
}
35 changes: 35 additions & 0 deletions stitch-app/services/SparkPost/incoming_webhooks/incoming/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
exports = function(payload) {
const body = EJSON.parse(payload.body.text());

const relayMsg = body[0].msys.relay_message;
const localpart = relayMsg.rcpt_to.split('@')[0];
const from = relayMsg.friendly_from || relayMsg.msg_from;
const subject = relayMsg.content.subject;
console.log('Email received from: ', from);
console.log('Searching for: ', subject);
context.functions.execute("GiphySearch", subject).then(gifs => {
const mongodb = context.services.get("mongodb-atlas");
return mongodb.db('porg').collection('responses').insertOne({
"localpart": localpart,
"from": from,
"search": subject,
"gifs": gifs,
"relayMsg": relayMsg,
"ts": new Date()
}).then(result => {
let subData = {
search: subject,
gifs: gifs
};
return subData;
});
}).then(subData => {
console.log('Substitution Data: ', JSON.stringify(subData));
return context.functions.execute("SendEmail", from, subData);
}).then(result => {
console.log('Send Result: ', JSON.stringify(result));
return;
}).catch(err => {
console.log('Error: ', JSON.stringify(err));
});
};
10 changes: 10 additions & 0 deletions stitch-app/services/SparkPost/rules/POST.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "5aa037d2cfb737497ec8ff20",
"name": "POST",
"actions": [
"post"
],
"when": {
"%%args.url.host": "api.sparkpost.com"
}
}
9 changes: 9 additions & 0 deletions stitch-app/services/mongodb-atlas/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "5ad23cb246224c054025b23e",
"name": "mongodb-atlas",
"type": "mongodb-atlas",
"config": {
"clusterName": "Cluster0"
},
"version": 1
}
7 changes: 7 additions & 0 deletions stitch-app/services/mongodb-atlas/rules/porg.responses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "5ad23da5cfb737ad8f32be4b",
"namespace": "porg.responses",
"other_fields": {},
"write": {},
"read": {}
}
6 changes: 6 additions & 0 deletions stitch-app/stitch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app_id": "giphyresponder-mspiu",
"config_version": 20180301,
"name": "GiphyResponder",
"security": {}
}
6 changes: 6 additions & 0 deletions stitch-app/values/sp-api-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "5aa03563b8b998813e7aa3a2",
"name": "sp-api-key",
"value": "<SPARKPOST_API_KEY>",
"private": false
}

0 comments on commit fe281a5

Please sign in to comment.