-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (53 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Lob api demo
* Accepts user input and uses the Lob api to create a letter to a state representative
* Running on node.js lts (v8.9.1)
*
* @author Janicklas Ralph
*/
const chalk = require('chalk');
const getInput = require('./scripts/input');
const getRepresentativeInfo = require('./scripts/representative');
const createLetter = require('./scripts/lob');
const printError = (err) => console.error(chalk`\n{red.bold ${err}}`);
/**
* The main function, kickstarts the program
*
* @returns Prints the url of the created letter in console
*/
const main = async () => {
let userInput;
let representativeInfo;
let letterResponse;
// Prompt user input
try {
userInput = await getInput();
} catch(err) {
return printError(err);
}
// Format address for google civic api
const inputAddress = [
userInput.addressLine1,
userInput.addressLine2,
userInput.city,
userInput.state,
userInput.country,
userInput.zip,
].filter(v => v.trim()).join(' ');
// Get a state representative's details from the google civic api
try {
representativeInfo = await getRepresentativeInfo(inputAddress);
} catch(err) {
return printError(err);
}
// Create a letter using Lob
try {
letterResponse = await createLetter(userInput, representativeInfo);
} catch (err) {
return printError(`Failed to create a letter with the info provided.\n${err}`);
}
// Print the url of the letter created.
console.log(chalk`\n\n{green.bold Your letter has been successfully created!}`);
console.log(chalk`{blue ${letterResponse.url}}`);
};
main();