From 5eba8236e2fcce297e9eed56e89481e158061656 Mon Sep 17 00:00:00 2001 From: Kareem Amin Date: Fri, 26 Jun 2020 17:39:54 -0400 Subject: [PATCH 1/3] refactor: made functions annonymous, removed all the notes, linked to readme for more information, simplfiied the sample code --- actions/.DS_Store | Bin 0 -> 6148 bytes actions/get-reddit-posts/.DS_Store | Bin 0 -> 6148 bytes actions/get-reddit-posts/action-definition.js | 27 +++++++ .../get-reddit-posts/get-top-reddit-posts.js | 46 ++++++++++++ .../get-reddit-posts/output-sample.js | 0 .../get-reddit-posts/output-schema.js | 0 .../get-reddit-posts/test-inputs.js | 0 index.js | 12 +++ src/get_top_reddit_posts/definition.js | 64 ---------------- .../get_top_reddit_posts.js | 71 ------------------ src/index.js | 22 ------ 11 files changed, 85 insertions(+), 157 deletions(-) create mode 100644 actions/.DS_Store create mode 100644 actions/get-reddit-posts/.DS_Store create mode 100644 actions/get-reddit-posts/action-definition.js create mode 100644 actions/get-reddit-posts/get-top-reddit-posts.js rename src/get_top_reddit_posts/output_sample.js => actions/get-reddit-posts/output-sample.js (100%) rename src/get_top_reddit_posts/output_parameter_schema.js => actions/get-reddit-posts/output-schema.js (100%) rename src/get_top_reddit_posts/test_get_top_reddit_posts.js => actions/get-reddit-posts/test-inputs.js (100%) create mode 100644 index.js delete mode 100644 src/get_top_reddit_posts/definition.js delete mode 100644 src/get_top_reddit_posts/get_top_reddit_posts.js delete mode 100644 src/index.js diff --git a/actions/.DS_Store b/actions/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fda1b1bca640ae6d8ffbc0ab458446926dc9a474 GIT binary patch literal 6148 zcmeHKQA-;^5T1?EZX)766cqZhFG3#@$SEiy!jaks--Hu=s73F*pa}CK!bccx#-~oU{HCSt4^Msgwqcc)-o;gH8 zV+^1N3I0+jXQJ8h9vLuu=fMT;TM2D=xPNWvgN6eA#eSyG<4ngC3$TOu`5y@_>Banb zRPJuJOKq;NPbLkyvgYsZpI!a98ULL8x~0{Dz;Bym+2Is^qw(U}ufsu}>HGp?r{hfX z8JPiQfEjoz2Hbfh7vJgycwT0J8F;r0nDarQ8Vnp(7U}4~hDrd$I=V^FHnoJwQ4Rx# zl|}495jGXkrV3YL2%C<6<>CT|l|`El!WAFF%`9AnB4~CTUr{)Sz#_NI05kBI0aN>- zc;^29`h5LQLEJC{%)tN2fT-_o?_EPPfr3ykK=VE61Z*IU+|Nm>c@#XEgSJ9-g@JYd!&MR$3BYIWG9q zIvFD+R_v|NRPBAQT9?@yW-nu#ku}7eqZw;9@a64S{us~X&RM64-`nG#@!pd--;DEL z*gP8NE+2bb4&7V04PB6?3tAxBllSCyZs@M@j5}om2dAVVgFwr4)_z=6Z*a=1A-B~}xbV#1iTUWpp z*j3;t4ZFJk-~N35-zE9T6>tUql>*Wp+z2+cnPq72@+0)MK&7e%UJHUIzs literal 0 HcmV?d00001 diff --git a/actions/get-reddit-posts/action-definition.js b/actions/get-reddit-posts/action-definition.js new file mode 100644 index 0000000..e7b6835 --- /dev/null +++ b/actions/get-reddit-posts/action-definition.js @@ -0,0 +1,27 @@ +// See README.MD for an in-depth explanation of each parameter. +module.exports = { + name: "get-top-reddit-posts", + function: require('./get-top-reddit-posts.js'), + documentationUri: "https://github.com/clay-run/action-template-nodejs/blob/master/README.md", + iconUri: "https://logo.clearbit.com/reddit.com?size=80", + displayName: "Get Top Reddit Posts", + description: "This action allows to get the top posts of a sub-reddit", + inputParameterSchema: [ + { + name: "subredditName", + type: "text", + optional: false, + displayName: "SubReddit Name", + description: "The sub-reddit name to get posts from" + }, + { + name: "numberOfPosts", + type: "number", + displayName: "# of posts", + description: "The number of posts to fetch" + } + ], + outputParameterSchema: require('./output_parameter_schema.js'), + inputSample: require('./test-inputs').goodSample, + outputSample: require('./output_sample.js'), +} diff --git a/actions/get-reddit-posts/get-top-reddit-posts.js b/actions/get-reddit-posts/get-top-reddit-posts.js new file mode 100644 index 0000000..eabacef --- /dev/null +++ b/actions/get-reddit-posts/get-top-reddit-posts.js @@ -0,0 +1,46 @@ +const superagent = require('superagent') + +/* + * This code is run when your action is called and is passed all parameters and authentication keys that were defined + * in the action-definition. + + * @param {object} actionInputs - This will be passed the inputs from the action-definition. + * @param {object} context - Clay specific helpers + * @param {object} context.auth - Authentication information based on the authentcation method defined in the action-definition + * @param {object} context.status = Clay specific statuses that can be passed on either Success or Failure. They impact how the result is shown in the UI. + * @param {function} context.log - Clay specific logs + * @param {function} context.success|fail - return this function to end exection of the action + * + * You can find more instructions in the README: https://github.com/clay-run/action-template-nodejs/blob/master/README.md. + */ + +module.exoprts = (actionInputs, context) => { + context.log('debug: inside getLastRedditPosts with inputs: ', actionInputs) + + let numberOfPosts = 10 + + if(!actionInputs.subredditName){ + return context.fail({ + message: `invalid subredditName parameter:${oneSubredditName}`, + errorType: context.status.INVALID_INPUT_PAREMETER + }) + } + + try { + const redditResponse = await superagent.get(`https://www.reddit.com/r/${oneSubredditName}/top.json?limit=${numberOfPosts}`) + // get top 10 + return context.success({ + data: redditResponse.body.data.children.splice(0,9), + textPreview: "# of posts found", + imagePreview: "https://logo.clearbit.com/reddit.com?size=80" + }) + } + catch(e) { + return context.fail({ + message: `Call to reddit failed with error: ${err.message}` + }) + } +} + + + \ No newline at end of file diff --git a/src/get_top_reddit_posts/output_sample.js b/actions/get-reddit-posts/output-sample.js similarity index 100% rename from src/get_top_reddit_posts/output_sample.js rename to actions/get-reddit-posts/output-sample.js diff --git a/src/get_top_reddit_posts/output_parameter_schema.js b/actions/get-reddit-posts/output-schema.js similarity index 100% rename from src/get_top_reddit_posts/output_parameter_schema.js rename to actions/get-reddit-posts/output-schema.js diff --git a/src/get_top_reddit_posts/test_get_top_reddit_posts.js b/actions/get-reddit-posts/test-inputs.js similarity index 100% rename from src/get_top_reddit_posts/test_get_top_reddit_posts.js rename to actions/get-reddit-posts/test-inputs.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..2070d9a --- /dev/null +++ b/index.js @@ -0,0 +1,12 @@ +/* + This is an Action Package. It consists of one more Actions. + Add an Action by importing it's action-definition.js and adding it to the actionDefinitions array +*/ + +const getTopRedditPosts = require("./actions/get-reddit-posts/action-definition.js") + +module.exports = { + name: "Reddit-Action-Package", + description: "Actions to search and get posts from Reddit", + actionDefinitions: [getTopRedditPosts], +} diff --git a/src/get_top_reddit_posts/definition.js b/src/get_top_reddit_posts/definition.js deleted file mode 100644 index 85b7656..0000000 --- a/src/get_top_reddit_posts/definition.js +++ /dev/null @@ -1,64 +0,0 @@ -const getTopRedditPostsActionFunction = require('./get_top_reddit_posts.js') - -/* - * This is where you define your Action. - */ -const getTopRedditPostsActionDefinition = { - name: "gettopredditposts", // unique action name across the entire action package - function: getTopRedditPostsActionFunction, // the action function to execute - documentationUri: "https://github.com/clay-run/action-template-nodejs/blob/master/README.md", // optional - link to documentation - iconUri: "https://logo.clearbit.com/reddit.com?size=80", // optional - icon to be displayed on Clay - displayName: "Get top reddit posts", // the action name to be displayed on Clay - description: "This action allows to get the top posts of a specific subreddit", // the action description to be displayed on Clay - //actionGroups: ["Social Media", "Reddit"], // INTERNAL ONLY - folders that will include the public action on Clay - //authentications: [ // optional - authentication methods that can be used by the action function - // { - // provider: "api_key", - // } - //], - inputParameterSchema: [ // the action function expects an object with the following parameters - { - name: "subredditName", // the parameter key in the object - type: "text", // the parameter type - boolean, number, text, array, object - optional: false, // optional - determines if the parameter is optional, defaults to false - displayName: "SubReddit Name", // optional field - the parameter name displayed on Clay - description: "The SubReddit name to get posts from" // optional field - the parameter description displayed on Clay - }, - { - name: "numberOfPosts", - type: "number", - optional: true, - displayName: "# of posts", - description: "The number of posts to fetch" - } - ], - //supportsBatching: true, // optional - if allowed, the action function will receive a flag to indicate that it is running in batch mode - //maximumBatchSize: 100, // optional - the maximum number of input objects to be received if batch mode is enabled - outputParameterSchema: require('./output_parameter_schema.js'), - inputSample: { // optional - a sample input object for the action function - subredditName: "cats", - numberOfPosts: 10 - }, - outputSample: require('./output_sample.js'), - //pricing: { // INTERNAL ONLY - pricing rules for the action function - // requiresPayment: true, - // chargedById: "a1b2c3d4", - // paymentPlan: "subscription", - // trialRuns: 1000, - // pricePerRun: 5 // integer in one hundredth of a cent - //}, - //restrictionFlags: { // INTERNAL ONLY - restriction rules related to the action function - // isExportable: true //optional - prevents the action function output to be exported from Clay - //}, - //isPublic: false, // INTERNAL ONLY - allows Clay admins to make the action function public - //rateLimitRules: { // optional - allows rate limiting rules to be defined in order to throttle the execution of the action function - // concurrency: [ - // { - // bucket: ['USER'], - // limit: 4 - // } - // ] - //} -} - -module.exports = getTopRedditPostsActionDefinition diff --git a/src/get_top_reddit_posts/get_top_reddit_posts.js b/src/get_top_reddit_posts/get_top_reddit_posts.js deleted file mode 100644 index 3131556..0000000 --- a/src/get_top_reddit_posts/get_top_reddit_posts.js +++ /dev/null @@ -1,71 +0,0 @@ -const superagent = require('superagent') - - -/* - * This is where you write the behavior of your Action. - * - * `actionInputs` must match the inputs you defined in the Action definition. - * - * `context` provides `.log` `.fail` `.success` and `.status` to help you interact with the Clay platform. - */ -async function getTopRedditPostsActionFunction(actionInputs, context) { // <--- All Actions take two parameters: `actionInputs` and `context` - context.log('debug: inside getLastRedditPosts with inputs: ', actionInputs) - // ^^^ the `context` object exposes a .log method to help you see logs inside Clay - - let numberOfPosts = 10 - if(actionInputs.numberOfPosts){ - numberOfPosts = actionInputs.numberOfPosts - } - - const oneSubredditName = actionInputs.subredditName - if(typeof oneSubredditName != 'string' || oneSubredditName.length === 0){ - return context.fail({ - message: `invalid subredditName parameter:${oneSubredditName}` - }) - } - - const redditResponse = await superagent.get(`https://www.reddit.com/r/${oneSubredditName}/top.json?limit=${numberOfPosts}`) - - try { - const topPosts = [] - for(const oneRedditPostData of redditResponse.body.data.children){ - const oneRedditPost = { - title: oneRedditPostData.data.title, - id: oneRedditPostData.data.id, - author: oneRedditPostData.data.author, - numberOfUpvotes: oneRedditPostData.data.ups, - numberOfComments: oneRedditPostData.data.num_comments, - url: `https://www.reddit.com${oneRedditPostData.data.permalink}` - } - topPosts.push(oneRedditPost) - } - - let textPreview = "No posts found" - - if(topPosts.length > 0){ - textPreview = `${topPosts.length} posts found, e.g. ${topPosts[0].title}` - } - - const imagePreview = "https://logo.clearbit.com/reddit.com?size=80" - - return context.success({ - // ^^^ `context` provides a `.success` handler structuring the response to your user - data: {arrayOfPosts: topPosts}, - textPreview, - imagePreview - }) - } catch (err){ - /** - * Errors that reach this block are not logged by default because context.fail will treat them as properly handled errors. - * - * If you want visibility on Clay to errors that occur here, you should use context.log below. - */ - // context.log(err.message) - return context.fail({ - // ^^^ `context` provides a `.fail` handler for error handling - message: `Call to reddit failed with error: ${err.message}` - }) - } -} - -module.exports = getTopRedditPostsActionFunction diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 2a883c2..0000000 --- a/src/index.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * - * This is where you define your Action Package. - * - * An Action Package contains one or more Action Definitions. - * - * Be sure to add any actions you want included in your Action Package to the `actionDefinitions` array. - * - */ - -const getTopRedditPostsActionDefinition = require("./get_top_reddit_posts/definition.js") - -/* - * The Action Package definition - */ -const RedditActionPackage = { - name: "redditactionpackage", // a globally unique name to identify the package - description: "Actions to help search and read posts on Reddit", // a description of the package's purpose or content - actionDefinitions: [getTopRedditPostsActionDefinition], // the actions included in the action package -} - -module.exports = RedditActionPackage From 8159aa03581e884885d140c1c77023be565d990c Mon Sep 17 00:00:00 2001 From: Kareem Amin Date: Fri, 26 Jun 2020 18:32:08 -0400 Subject: [PATCH 2/3] refactor: change the main action file name to index.js --- .../get-reddit-posts/get-top-reddit-posts.js | 46 ------------------- actions/get-reddit-posts/output-schema.js | 4 +- 2 files changed, 3 insertions(+), 47 deletions(-) delete mode 100644 actions/get-reddit-posts/get-top-reddit-posts.js diff --git a/actions/get-reddit-posts/get-top-reddit-posts.js b/actions/get-reddit-posts/get-top-reddit-posts.js deleted file mode 100644 index eabacef..0000000 --- a/actions/get-reddit-posts/get-top-reddit-posts.js +++ /dev/null @@ -1,46 +0,0 @@ -const superagent = require('superagent') - -/* - * This code is run when your action is called and is passed all parameters and authentication keys that were defined - * in the action-definition. - - * @param {object} actionInputs - This will be passed the inputs from the action-definition. - * @param {object} context - Clay specific helpers - * @param {object} context.auth - Authentication information based on the authentcation method defined in the action-definition - * @param {object} context.status = Clay specific statuses that can be passed on either Success or Failure. They impact how the result is shown in the UI. - * @param {function} context.log - Clay specific logs - * @param {function} context.success|fail - return this function to end exection of the action - * - * You can find more instructions in the README: https://github.com/clay-run/action-template-nodejs/blob/master/README.md. - */ - -module.exoprts = (actionInputs, context) => { - context.log('debug: inside getLastRedditPosts with inputs: ', actionInputs) - - let numberOfPosts = 10 - - if(!actionInputs.subredditName){ - return context.fail({ - message: `invalid subredditName parameter:${oneSubredditName}`, - errorType: context.status.INVALID_INPUT_PAREMETER - }) - } - - try { - const redditResponse = await superagent.get(`https://www.reddit.com/r/${oneSubredditName}/top.json?limit=${numberOfPosts}`) - // get top 10 - return context.success({ - data: redditResponse.body.data.children.splice(0,9), - textPreview: "# of posts found", - imagePreview: "https://logo.clearbit.com/reddit.com?size=80" - }) - } - catch(e) { - return context.fail({ - message: `Call to reddit failed with error: ${err.message}` - }) - } -} - - - \ No newline at end of file diff --git a/actions/get-reddit-posts/output-schema.js b/actions/get-reddit-posts/output-schema.js index edf025b..5bdc828 100644 --- a/actions/get-reddit-posts/output-schema.js +++ b/actions/get-reddit-posts/output-schema.js @@ -1,4 +1,6 @@ -module.exports = [ // the action function must return an object with the following parameters +// These are used to help Clay's interface provide ways to extract data from the output of this action into the table + +module.exports = [ { name: "arrayOfPosts", type: "array", From e46419be5018868605076f9b8c5475528cb315f2 Mon Sep 17 00:00:00 2001 From: Niki Carroll Date: Mon, 10 Aug 2020 22:35:47 -0400 Subject: [PATCH 3/3] Directory structure fixes to comply with expectations from clay-action-client. --- actions/.DS_Store | Bin 6148 -> 0 bytes actions/get-reddit-posts/.DS_Store | Bin 6148 -> 0 bytes .../get-reddit-posts/action-definition.js | 4 +- src/get-reddit-posts/get-top-reddit-posts.js | 71 ++++++++++++++++++ .../get-reddit-posts/output-sample.js | 0 .../get-reddit-posts/output-schema.js | 0 .../get-reddit-posts/test-inputs.js | 2 +- index.js => src/index.js | 2 +- 8 files changed, 75 insertions(+), 4 deletions(-) delete mode 100644 actions/.DS_Store delete mode 100644 actions/get-reddit-posts/.DS_Store rename {actions => src}/get-reddit-posts/action-definition.js (88%) create mode 100644 src/get-reddit-posts/get-top-reddit-posts.js rename {actions => src}/get-reddit-posts/output-sample.js (100%) rename {actions => src}/get-reddit-posts/output-schema.js (100%) rename {actions => src}/get-reddit-posts/test-inputs.js (99%) rename index.js => src/index.js (79%) diff --git a/actions/.DS_Store b/actions/.DS_Store deleted file mode 100644 index fda1b1bca640ae6d8ffbc0ab458446926dc9a474..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKQA-;^5T1?EZX)766cqZhFG3#@$SEiy!jaks--Hu=s73F*pa}CK!bccx#-~oU{HCSt4^Msgwqcc)-o;gH8 zV+^1N3I0+jXQJ8h9vLuu=fMT;TM2D=xPNWvgN6eA#eSyG<4ngC3$TOu`5y@_>Banb zRPJuJOKq;NPbLkyvgYsZpI!a98ULL8x~0{Dz;Bym+2Is^qw(U}ufsu}>HGp?r{hfX z8JPiQfEjoz2Hbfh7vJgycwT0J8F;r0nDarQ8Vnp(7U}4~hDrd$I=V^FHnoJwQ4Rx# zl|}495jGXkrV3YL2%C<6<>CT|l|`El!WAFF%`9AnB4~CTUr{)Sz#_NI05kBI0aN>- zc;^29`h5LQLEJC{%)tN2fT-_o?_EPPfr3ykK=VE61Z*IU+|Nm>c@#XEgSJ9-g@JYd!&MR$3BYIWG9q zIvFD+R_v|NRPBAQT9?@yW-nu#ku}7eqZw;9@a64S{us~X&RM64-`nG#@!pd--;DEL z*gP8NE+2bb4&7V04PB6?3tAxBllSCyZs@M@j5}om2dAVVgFwr4)_z=6Z*a=1A-B~}xbV#1iTUWpp z*j3;t4ZFJk-~N35-zE9T6>tUql>*Wp+z2+cnPq72@+0)MK&7e%UJHUIzs diff --git a/actions/get-reddit-posts/action-definition.js b/src/get-reddit-posts/action-definition.js similarity index 88% rename from actions/get-reddit-posts/action-definition.js rename to src/get-reddit-posts/action-definition.js index e7b6835..4e4a600 100644 --- a/actions/get-reddit-posts/action-definition.js +++ b/src/get-reddit-posts/action-definition.js @@ -21,7 +21,7 @@ module.exports = { description: "The number of posts to fetch" } ], - outputParameterSchema: require('./output_parameter_schema.js'), + outputParameterSchema: require('./output-schema.js'), inputSample: require('./test-inputs').goodSample, - outputSample: require('./output_sample.js'), + outputSample: require('./output-sample.js'), } diff --git a/src/get-reddit-posts/get-top-reddit-posts.js b/src/get-reddit-posts/get-top-reddit-posts.js new file mode 100644 index 0000000..3131556 --- /dev/null +++ b/src/get-reddit-posts/get-top-reddit-posts.js @@ -0,0 +1,71 @@ +const superagent = require('superagent') + + +/* + * This is where you write the behavior of your Action. + * + * `actionInputs` must match the inputs you defined in the Action definition. + * + * `context` provides `.log` `.fail` `.success` and `.status` to help you interact with the Clay platform. + */ +async function getTopRedditPostsActionFunction(actionInputs, context) { // <--- All Actions take two parameters: `actionInputs` and `context` + context.log('debug: inside getLastRedditPosts with inputs: ', actionInputs) + // ^^^ the `context` object exposes a .log method to help you see logs inside Clay + + let numberOfPosts = 10 + if(actionInputs.numberOfPosts){ + numberOfPosts = actionInputs.numberOfPosts + } + + const oneSubredditName = actionInputs.subredditName + if(typeof oneSubredditName != 'string' || oneSubredditName.length === 0){ + return context.fail({ + message: `invalid subredditName parameter:${oneSubredditName}` + }) + } + + const redditResponse = await superagent.get(`https://www.reddit.com/r/${oneSubredditName}/top.json?limit=${numberOfPosts}`) + + try { + const topPosts = [] + for(const oneRedditPostData of redditResponse.body.data.children){ + const oneRedditPost = { + title: oneRedditPostData.data.title, + id: oneRedditPostData.data.id, + author: oneRedditPostData.data.author, + numberOfUpvotes: oneRedditPostData.data.ups, + numberOfComments: oneRedditPostData.data.num_comments, + url: `https://www.reddit.com${oneRedditPostData.data.permalink}` + } + topPosts.push(oneRedditPost) + } + + let textPreview = "No posts found" + + if(topPosts.length > 0){ + textPreview = `${topPosts.length} posts found, e.g. ${topPosts[0].title}` + } + + const imagePreview = "https://logo.clearbit.com/reddit.com?size=80" + + return context.success({ + // ^^^ `context` provides a `.success` handler structuring the response to your user + data: {arrayOfPosts: topPosts}, + textPreview, + imagePreview + }) + } catch (err){ + /** + * Errors that reach this block are not logged by default because context.fail will treat them as properly handled errors. + * + * If you want visibility on Clay to errors that occur here, you should use context.log below. + */ + // context.log(err.message) + return context.fail({ + // ^^^ `context` provides a `.fail` handler for error handling + message: `Call to reddit failed with error: ${err.message}` + }) + } +} + +module.exports = getTopRedditPostsActionFunction diff --git a/actions/get-reddit-posts/output-sample.js b/src/get-reddit-posts/output-sample.js similarity index 100% rename from actions/get-reddit-posts/output-sample.js rename to src/get-reddit-posts/output-sample.js diff --git a/actions/get-reddit-posts/output-schema.js b/src/get-reddit-posts/output-schema.js similarity index 100% rename from actions/get-reddit-posts/output-schema.js rename to src/get-reddit-posts/output-schema.js diff --git a/actions/get-reddit-posts/test-inputs.js b/src/get-reddit-posts/test-inputs.js similarity index 99% rename from actions/get-reddit-posts/test-inputs.js rename to src/get-reddit-posts/test-inputs.js index 3326baa..657a2ac 100644 --- a/actions/get-reddit-posts/test-inputs.js +++ b/src/get-reddit-posts/test-inputs.js @@ -17,4 +17,4 @@ module.exports = { anotherSample: { subredditName: "dogs" } -} \ No newline at end of file +} diff --git a/index.js b/src/index.js similarity index 79% rename from index.js rename to src/index.js index 2070d9a..c1955f1 100644 --- a/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ Add an Action by importing it's action-definition.js and adding it to the actionDefinitions array */ -const getTopRedditPosts = require("./actions/get-reddit-posts/action-definition.js") +const getTopRedditPosts = require("./get-reddit-posts/action-definition.js") module.exports = { name: "Reddit-Action-Package",