Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce a method for setting credentials (setConfig()) that d… #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ src
build/example.js
.vscode
.env.example
babel.config.js
jest.config.js
tsconfig.json
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ This library looks for the terms "lynx blue line" and "delay" to determine if a

1. You'll need to register your own [Twitter Application](https://developer.twitter.com)
2. Generate a Bearer token
3. Create a `.env` using the provided `.env.example` with your Twitter application credentials
4. Follow the example code in `src/example.ts` for your own implementation
3. Follow the example code in `src/example.ts` for your own implementation
- The first thing you'll need to call is `setConfig` and pass it an object with your `{BEARER_TOKEN: ''}`.
- `CONSUMER_KEY` and `CONSUMER_SECRET` _can_ be set if you want. Streamlined auto-auth may be added in the future.
- The example code uses `dotenv`; Create a `.env` using the provided `.env.example` with your Twitter application credentials. If you want to use this method in the future you'll need to install `dotenv` into your own project.

### Libraries

Expand Down
7 changes: 0 additions & 7 deletions babel.config.js

This file was deleted.

188 changes: 0 additions & 188 deletions jest.config.js

This file was deleted.

12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
{
"name": "@opencltbrigade/cats-tweet-analyzer",
"version": "0.0.2",
"version": "1.0.0",
"description": "",
"main": "./build/index.js",
"private": false,
"scripts": {
"test": "jest",
"build": "tsc",
"start": "tsc && node ./build/example.js"
},
"author": "Open Charlotte Brigade",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"@babel/preset-typescript": "^7.6.0",
"@types/node": "^12.7.5",
"babel-jest": "^24.9.0",
"jest": "^24.9.0",
"typescript": "^3.6.3"
"typescript": "^3.6.3",
"dotenv": "^8.1.0"
},
"dependencies": {
"dotenv": "^8.1.0",
"node-fetch": "^2.6.0",
"twitter": "^1.7.1"
}
Expand Down
7 changes: 6 additions & 1 deletion src/example.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import getTimelineAnalysis from './getTimelineAnalysis';
import { getTimeline } from './lib';
import { getTimeline, setConfig } from './lib';
import { AnalyzedTweet } from './types';

require('dotenv').config();

const displayAlertTweets = async () => {
try {
const { TWITTER_BEARER_TOKEN } = process.env;
setConfig({ BEARER_TOKEN: TWITTER_BEARER_TOKEN });

const tweets: Array<AnalyzedTweet> = await getTimelineAnalysis();
const filtered = tweets.filter(
(tweet) =>
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import getTimelineAnalysis from './getTimelineAnalysis';
import { analyzeTweets, getTimeline, getTwitterClient } from './lib';
import {
analyzeTweets,
getTimeline,
getTwitterClient,
setConfig
} from './lib';

export { getTimeline, analyzeTweets, getTwitterClient, getTimelineAnalysis };
export { getTimeline, analyzeTweets, getTwitterClient, getTimelineAnalysis, setConfig };
15 changes: 15 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Configuration } from '../types';

let config: Configuration = {
CONSUMER_KEY: '',
CONSUMER_SECRET: '',
BEARER_TOKEN: ''
};

export const setConfig = (newValues: Configuration): void => {
config = { ...config, ...newValues };
};

export const getConfig = (): Configuration => {
return { ...config };
};
11 changes: 6 additions & 5 deletions src/lib/getTwitterClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require('dotenv').config();
import { getConfig } from '.';

const TwitterClient = require('twitter');

export function getTwitterClient() {
const { TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_BEARER_TOKEN } = process.env;
const { CONSUMER_KEY: consumer_key, CONSUMER_SECRET: consumer_secret, BEARER_TOKEN: bearer_token } = getConfig();

const twitter = new TwitterClient({
consumer_key: TWITTER_CONSUMER_KEY,
consumer_secret: TWITTER_CONSUMER_SECRET,
bearer_token: TWITTER_BEARER_TOKEN
consumer_key,
consumer_secret,
bearer_token
});
return twitter;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import analyzeTweets from './analyzeTweets';
import { getConfig, setConfig } from './config';
import getTimeline from './getTimeline';
import getTwitterClient from './getTwitterClient';

export { getTwitterClient, getTimeline, analyzeTweets };
export { getTwitterClient, getTimeline, analyzeTweets, getConfig, setConfig };
7 changes: 7 additions & 0 deletions src/types/Configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Configuration {
CONSUMER_KEY?: string;
CONSUMER_SECRET?: string;
BEARER_TOKEN?: string;
}

export default Configuration;
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import AnalyzedTweet from './AnalyzedTweet';
import Configuration from './Configuration';
import NestedTweet from './NestedTweet';
import ServiceDelayData from './ServiceDelayData';
import ServiceRestorationData from './ServiceRestorationData';
import TimelineResponse from './TimelineResponse';
import Tweet from './Tweet';

export { Tweet, TimelineResponse, AnalyzedTweet, ServiceDelayData, ServiceRestorationData, NestedTweet };
export { Tweet, TimelineResponse, AnalyzedTweet, ServiceDelayData, ServiceRestorationData, NestedTweet, Configuration };
Loading