bas 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/._.rspec_status +0 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +14 -0
  5. data/CHANGELOG.md +4 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/CONTRIBUTING.md +66 -0
  8. data/Gemfile +24 -0
  9. data/LICENSE +21 -0
  10. data/README.md +362 -0
  11. data/Rakefile +12 -0
  12. data/SECURITY.md +13 -0
  13. data/lib/bas/dispatcher/base.rb +31 -0
  14. data/lib/bas/dispatcher/discord/exceptions/invalid_webhook_token.rb +16 -0
  15. data/lib/bas/dispatcher/discord/implementation.rb +51 -0
  16. data/lib/bas/dispatcher/discord/types/response.rb +22 -0
  17. data/lib/bas/dispatcher/slack/exceptions/invalid_webhook_token.rb +16 -0
  18. data/lib/bas/dispatcher/slack/implementation.rb +51 -0
  19. data/lib/bas/dispatcher/slack/types/response.rb +21 -0
  20. data/lib/bas/domain/birthday.rb +25 -0
  21. data/lib/bas/domain/email.rb +34 -0
  22. data/lib/bas/domain/exceptions/function_not_implemented.rb +18 -0
  23. data/lib/bas/domain/pto.rb +28 -0
  24. data/lib/bas/domain/work_items_limit.rb +25 -0
  25. data/lib/bas/fetcher/base.rb +43 -0
  26. data/lib/bas/fetcher/imap/base.rb +70 -0
  27. data/lib/bas/fetcher/imap/types/response.rb +27 -0
  28. data/lib/bas/fetcher/imap/use_case/support_emails.rb +26 -0
  29. data/lib/bas/fetcher/notion/base.rb +52 -0
  30. data/lib/bas/fetcher/notion/exceptions/invalid_api_key.rb +15 -0
  31. data/lib/bas/fetcher/notion/exceptions/invalid_database_id.rb +15 -0
  32. data/lib/bas/fetcher/notion/helper.rb +21 -0
  33. data/lib/bas/fetcher/notion/types/response.rb +26 -0
  34. data/lib/bas/fetcher/notion/use_case/birthday_next_week.rb +41 -0
  35. data/lib/bas/fetcher/notion/use_case/birthday_today.rb +29 -0
  36. data/lib/bas/fetcher/notion/use_case/pto_next_week.rb +71 -0
  37. data/lib/bas/fetcher/notion/use_case/pto_today.rb +30 -0
  38. data/lib/bas/fetcher/notion/use_case/work_items_limit.rb +37 -0
  39. data/lib/bas/fetcher/postgres/base.rb +46 -0
  40. data/lib/bas/fetcher/postgres/helper.rb +16 -0
  41. data/lib/bas/fetcher/postgres/types/response.rb +42 -0
  42. data/lib/bas/fetcher/postgres/use_case/pto_today.rb +32 -0
  43. data/lib/bas/formatter/base.rb +53 -0
  44. data/lib/bas/formatter/birthday.rb +34 -0
  45. data/lib/bas/formatter/exceptions/invalid_data.rb +15 -0
  46. data/lib/bas/formatter/pto.rb +88 -0
  47. data/lib/bas/formatter/support_emails.rb +69 -0
  48. data/lib/bas/formatter/work_items_limit.rb +64 -0
  49. data/lib/bas/mapper/base.rb +30 -0
  50. data/lib/bas/mapper/imap/support_emails.rb +56 -0
  51. data/lib/bas/mapper/notion/birthday_today.rb +68 -0
  52. data/lib/bas/mapper/notion/pto_today.rb +70 -0
  53. data/lib/bas/mapper/notion/work_items_limit.rb +65 -0
  54. data/lib/bas/mapper/postgres/pto_today.rb +47 -0
  55. data/lib/bas/use_cases/types/config.rb +19 -0
  56. data/lib/bas/use_cases/use_case.rb +39 -0
  57. data/lib/bas/use_cases/use_cases.rb +377 -0
  58. data/lib/bas/version.rb +6 -0
  59. data/lib/bas.rb +9 -0
  60. data/renovate.json +6 -0
  61. data/sig/business_automation_system.rbs +4 -0
  62. metadata +107 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d8ff973fc126ff9d0af5adf9200417f302a9a86cc50be3d3ef090a8979a22b1
4
+ data.tar.gz: 846dfe3cd6d719aa0a7cc17ce2219a0dee54a5d89043a475ec93b4786fd688d2
5
+ SHA512:
6
+ metadata.gz: 14ea01f630b4a296fa94a352d14bc00111833d2f5b1867861280c5aa32abca445bdc7ec888c83a4a673109aba2c5e3239fdccc48cc7315477a3b579d200b649c
7
+ data.tar.gz: 3b73cf8b1dc7d931c3cd428141f2a3806c1b90c23bb1c0ac2dc755bf16278b679f42405f4d504b5cfbd6d1f09787d065f34fe3a06cee15ed9d684a93d2249971
data/._.rspec_status ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - 'spec/**/*_spec.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (26.03.2024)
4
+ - [Add the BNS gem code](https://github.com/kommitters/bas/issues/1)
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community includes:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the
26
+ overall community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing other's private information, such as a physical or email
35
+ address, without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at [oss@kommit.co](mailto:oss@kommit.co).
63
+
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available
126
+ at [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,66 @@
1
+ # Contributing 🎉
2
+
3
+ Hi there! We are thrilled that you'd like to contribute to this project. Your help is essential for keeping it great 🙌.
4
+
5
+ Contributions to this project are [released][contributions-under-repo] to the public under the project's open source license.
6
+
7
+ Here are a few things you can do that will increase the likelihood of your pull request being accepted:
8
+ - Follow standards for style and code quality.
9
+ - Write tests (if applicable).
10
+ - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
11
+ - Write a [good commit message][commit-msg].
12
+ - All content, comments, pull requests and other contributions must comply with the [code of conduct][coc].
13
+
14
+ ## Getting started
15
+
16
+ * Make sure you have a [GitHub account][github-signup].
17
+ * Create a GitHub issue for your contribution, assuming one does not already exist.
18
+ * Choose one of the templates and clearly describe the issue.
19
+ * Fork the repository on GitHub.
20
+ * Try to keep your local repository in a "rebased" state.
21
+
22
+ ## Finding things to work on
23
+
24
+ Issues marked with [good first issue][good-first-issue] or [help wanted][help-wanted] are usually pretty self-contained and a good place to get started.
25
+
26
+ If you see any issues that are assigned to a particular person or have the `📍 Work in progress` label, that means
27
+ someone is currently working on that issue this issue in the next week or two.
28
+
29
+ Of course, feel free to create a new issue if you think something needs to be added or fixed.
30
+
31
+ ## Making changes
32
+
33
+ * Create a topic branch from where you want to base your work `git checkout -b my-branch-name`.
34
+ * This is usually the `main` branch.
35
+ * Please avoid working directly on the `main` branch.
36
+
37
+ ## Submitting changes
38
+ * Push changes to your fork.
39
+ * Submit a pull request.
40
+
41
+ At this point, you're waiting on us. We like to at least comment on pull requests within three
42
+ business days (typically, one business day). We may suggest some changes, improvements or
43
+ alternatives.
44
+
45
+ ## Resources
46
+
47
+ - [How to Contribute to Open Source][oss-how-to]
48
+ - [Using Pull Requests][github-help-pr]
49
+ - [GitHub Help][github-help]
50
+
51
+ ## Contact
52
+
53
+ Get in touch: [oss@kommit.co][mail-to] | [@kommitters_oss][twitter] on Twitter.
54
+
55
+
56
+ [twitter]: https://twitter.com/kommitters_oss
57
+ [mail-to]: mailto:oss@kommit.co
58
+ [github-signup]: https://github.com/signup/free
59
+ [github-help]: https://help.github.com
60
+ [github-help-pr]: https://help.github.com/articles/about-pull-requests
61
+ [oss-how-to]: https://opensource.guide/how-to-contribute
62
+ [coc]: https://github.com/kommitters/soroban-did-contract/blob/main/CODE_OF_CONDUCT.md
63
+ [commit-msg]: https://github.com/erlang/otp/wiki/Writing-good-commit-messages
64
+ [good-first-issue]: https://github.com/kommitters/soroban-did-contract/issues?q=is%3Aopen+is%3Aissue+label%3A%22%F0%9F%91%8B+Good+first+issue%22
65
+ [help-wanted]: https://github.com/kommitters/soroban-did-contract/issues?q=is%3Aopen+is%3Aissue+label%3A%22%F0%9F%86%98+Help+wanted%22
66
+ [contributions-under-repo]: https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bas.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "net-imap", "~> 0.4.10"
11
+ gem "net-smtp", "~> 0.4.0.1"
12
+ gem "rspec", "~> 3.0"
13
+ gem "rubocop", "~> 1.21"
14
+ gem "simplecov", require: false, group: :test
15
+ gem "simplecov-lcov", "~> 0.8.0"
16
+
17
+ gem "vcr"
18
+ gem "webmock"
19
+
20
+ gem "httparty"
21
+
22
+ gem "pg", "~> 1.5", ">= 1.5.4"
23
+
24
+ gem "gmail_xoauth"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 kommitters Open Source
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,362 @@
1
+ # BAS - Business Automation System
2
+
3
+ Many organizations and individuals rely on automation across various contexts in their daily operations. With BAS, we aim to provide an open-source platform that empowers users to create customized automation systems tailored to their unique requirements. BAS consists of a series of abstract components designed to facilitate the creation of diverse use cases, regardless of context.
4
+
5
+ The underlying idea is to develop generic components that can serve a wide range of needs, this approach ensures that all members of the community can leverage the platform's evolving suite of components and use cases to their advantage.
6
+
7
+ ![Gem Version](https://img.shields.io/gem/v/bas?style=for-the-badge)
8
+ ![Gem Total Downloads](https://img.shields.io/gem/dt/bas?style=for-the-badge)
9
+ ![Build Badge](https://img.shields.io/github/actions/workflow/status/kommitters/bas/ci.yml?branch=project-opensource-config&style=for-the-badge)
10
+ [![Coverage Status](https://img.shields.io/coveralls/github/kommitters/bas?style=for-the-badge)](https://coveralls.io/github/kommitters/bas?branch=main)
11
+ ![GitHub License](https://img.shields.io/github/license/kommitters/bas?style=for-the-badge)
12
+ [![OpenSSF Scorecard](https://img.shields.io/ossf-scorecard/github.com/kommitters/bas?label=openssf%20scorecard&style=for-the-badge)](https://api.securityscorecards.dev/projects/github.com/kommitters/bas)
13
+ [![OpenSSF Best Practices](https://img.shields.io/cii/summary/8383?label=openssf%20best%20practices&style=for-the-badge)](https://bestpractices.coreinfrastructure.org/projects/8383)
14
+
15
+ ## Installation
16
+
17
+ Install the gem and add to the application's Gemfile by executing:
18
+
19
+ $ bundle add bas
20
+
21
+ If bundler is not being used to manage dependencies, install the gem by executing:
22
+
23
+ $ gem install bas
24
+
25
+ ## Requirements
26
+
27
+ * Ruby 2.6.0 or higher
28
+
29
+ ## Building my own use case
30
+
31
+ The gem provides with basic interfaces, types, and methods to shape your own use cases in an easy way.
32
+
33
+ There are 7 currently implemented use cases:
34
+ * Birthday notifications - from Notion to Discord
35
+ * Next Week Birthday notifications - from Notion to Discord
36
+ * PTO notifications - from Notion to Discord
37
+ * Next Week PTO notifications - from Notion to Discord
38
+ * PTO notifications - from Postgres to Slack
39
+ * WIP limit exceeded - from Notion to Discord
40
+ * Support email notification - from IMAP to Discord
41
+
42
+ For this example we'll analyze the birthday notification use case, bringing data from a notion database, and dispatching the
43
+ notifications to a Discord channel.
44
+
45
+ A *Use Case* object, consists on 4 main components, having it's own responsibility:
46
+
47
+ ### 1. Fetcher - Obtaining the data
48
+
49
+ Specifically, a fetcher is an object in charged of bringing data from a data source. The gem already provides the base interface
50
+ for building your own fetcher for your specific data source, or rely on already built classes if they match your purpose.
51
+
52
+ The base interface for a fetcher can be found under the `bas/fetcher/base.rb` class. Since this is a implementation of the `Fetcher::Base`
53
+ for bringing data from a Notion database, it was created on a new namespace for that data source, it can be found under
54
+ `/bas/fetcher/notion/use_case/birthday_today.rb`. It implements specific logic for fetching the data and validating the response.
55
+
56
+ ### 2. Mapper - Shaping it
57
+
58
+ The **Mapper** responsibility, is to shape the data using custom types from the app domain, bringing it into a
59
+ common structure understandable for other components, specifically the **Formatter**.
60
+
61
+ Because of the use case, the Mapper implementation for it, relies on specific types for representing a Birthday it self. It can be found
62
+ under `/bas/mapper/notion/birthday_today.rb`
63
+
64
+ ### 3. Formatter - Preparing the message
65
+
66
+ The **Formatter**, is in charge of preparing the message to be sent in our notification, and give it the right format with the right data.
67
+ The template or 'format' to be used should be included in the use case configurations, which we'll review in a further step. It's
68
+ implementation can be found under `/bas/formatter/birthday.rb`.
69
+
70
+ ### 4. Dispatcher - Sending your notification
71
+
72
+ Finally, the **Dispatcher** basically, sends or dispatches the formatted message into a destination, since the use case was implemented for
73
+ Discord, it implements specific logic to communicate with a Discord channel using a webhook. The webhook configuration and name for the 'Sender'
74
+ in the channel should be provided with the initial use case configurations. It can be found under `/bas/dispatcher/discord/implementation.rb`
75
+
76
+ ## Examples
77
+
78
+ In this example, we demonstrate how to instantiate a birthday notification use case and execute it in a basic Ruby project. We'll also cover its deployment in a serverless configuration, specifically using a simple Lambda deployment.
79
+
80
+ ### Preparing the configurations
81
+
82
+ We'll need some configurations for this specific use case:
83
+ * A **Notion database ID**, from a database with the following structure:
84
+
85
+ | Complete Name (text) | BD_this_year (formula) | BD (date) |
86
+ | -------------------- | --------------------------- | ------------------------ |
87
+ | John Doe | January 24, 2024 | January 24, 2000 |
88
+ | Jane Doe | June 20, 2024 | June 20, 2000 |
89
+
90
+ With the following formula for the **BD_this_year** column: `dateAdd(prop("BD"), year(now()) - year(prop("BD")), "years")`
91
+
92
+ * A Notion secret, which can be obtained, by creating an integration here: `https://developers.notion.com/`, browsing on the **View my integrations** option, and selecting the **New Integration** or **Create new integration** buttons.
93
+
94
+ * A webhook key, which can be generated directly on discord on the desired channel, following this instructions: `https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks`
95
+
96
+ * A filter, to determine which data to bring from the database, for this specific case, the filter we used is:
97
+
98
+ ```
99
+ #### file.rb ####
100
+ today = Date.now
101
+ {
102
+ "filter": {
103
+ "or": [
104
+ {
105
+ "property": "BD_this_year",
106
+ "date": {
107
+ "equals": today
108
+ }
109
+ }
110
+ ]
111
+ },
112
+ "sorts": []
113
+ }
114
+ ```
115
+
116
+ * A template for the formatter to be used for formatting the payload to dispatch to Discord. For this specific case, the format of the messages should be:
117
+
118
+ `"NAME, Wishing you a very happy birthday! Enjoy your special day! :birthday: :gift:"`
119
+
120
+ ### Instantiating the Use Case
121
+
122
+ To instantiate the use case, the `UseCases` module should provide a method for this purpose, for this specific case, the `notify_birthday_from_notion_to_discord`
123
+ is the desired one.
124
+
125
+ **Normal ruby code**
126
+ ```
127
+ filter = {
128
+ "filter": {
129
+ "or": [
130
+ {
131
+ "property": "BD_this_year",
132
+ "date": {
133
+ "equals": today
134
+ }
135
+ }
136
+ ]
137
+ },
138
+ "sorts": []
139
+ }
140
+
141
+ options = {
142
+ fetch_options: {
143
+ base_url: "https://api.notion.com",
144
+ database_id: NOTION_DATABASE_ID,
145
+ secret: NOTION_API_INTEGRATION_SECRET,
146
+ filter: filter
147
+ },
148
+ dispatch_options: {
149
+ webhook: "https://discord.com/api/webhooks/1199213527672565760/KmpoIzBet9xYG16oFh8W1RWHbpIqT7UtTBRrhfLcvWZdNiVZCTM-gpil2Qoy4eYEgpdf",
150
+ name: "Birthday Bot"
151
+ }
152
+ }
153
+
154
+ use_case = UseCases.notify_birthday_from_notion_to_discord(options)
155
+
156
+ use_case.perform
157
+
158
+ ```
159
+
160
+ ### Serverless
161
+ We'll explain how to configure and deploy a use case with serverless, this example will cover the PTO's notifications use case.
162
+
163
+ #### Configuring environment variables
164
+ Create the environment variables configuration file.
165
+
166
+ ```bash
167
+ cp env.yml.example env.yml
168
+ ```
169
+
170
+ And put the following env variables
171
+ ```
172
+ dev:
173
+ NOTION_DATABASE_ID: NOTION_DATABASE_ID
174
+ NOTION_SECRET: NOTION_SECRET
175
+ DISCORD_WEBHOOK: DISCORD_WEBHOOK
176
+ DISCORD_BOT_NAME: DISCORD_BOT_NAME
177
+ prod:
178
+ NOTION_DATABASE_ID: NOTION_DATABASE_ID
179
+ NOTION_SECRET: NOTION_SECRET
180
+ DISCORD_WEBHOOK: DISCORD_WEBHOOK
181
+ DISCORD_BOT_NAME: DISCORD_BOT_NAME
182
+
183
+ ```
184
+
185
+ The variables should be defined either in the custom settings section within the `serverless.yml` file to ensure accessibility by all lambdas, or in the environment configuration option for each lambda respectively. For example:
186
+
187
+ ```bash
188
+ # Accessible by all the lambdas
189
+ custom:
190
+ settings:
191
+ api:
192
+ NOTION_DATABASE_ID: ${file(./env.yml):${env:STAGE}.NOTION_DATABASE_ID}
193
+ NOTION_SECRET: ${file(./env.yml):${env:STAGE}.NOTION_SECRET}}
194
+
195
+ # Accessible by the lambda
196
+ functions:
197
+ lambdaName:
198
+ environment:
199
+ NOTION_DATABASE_ID: ${file(./env.yml):${env:STAGE}.NOTION_DATABASE_ID}
200
+ NOTION_SECRET: ${file(./env.yml):${env:STAGE}.NOTION_SECRET}}
201
+ ```
202
+
203
+ #### Schedule
204
+ the schedule is configured using an environment variable containing the cron configuration. For example:
205
+ ```bash
206
+ # env.yml file
207
+ SCHEDULER: cron(0 13 ? * MON-FRI *)
208
+
209
+ # serverless.yml
210
+ functions:
211
+ lambdaName:
212
+ events:
213
+ - schedule:
214
+ rate: ${file(./env.yml):${env:STAGE}.SCHEDULER}
215
+ ```
216
+
217
+ To learn how to modify the cron configuration follow this guide: [Schedule expressions using rate or cron](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html)
218
+
219
+ #### Building your lambda
220
+ On your serverless configuration, create your lambda function, on your serverless `/src` folder.
221
+
222
+ ```ruby
223
+ # frozen_string_literal: true
224
+
225
+ require 'bas'
226
+
227
+ # Initialize the environment variables
228
+ NOTION_BASE_URL = 'https://api.notion.com'
229
+ NOTION_DATABASE_ID = ENV.fetch('PTO_NOTION_DATABASE_ID')
230
+ NOTION_SECRET = ENV.fetch('PTO_NOTION_SECRET')
231
+ DISCORD_WEBHOOK = ENV.fetch('PTO_DISCORD_WEBHOOK')
232
+ DISCORD_BOT_NAME = ENV.fetch('PTO_DISCORD_BOT_NAME')
233
+
234
+ module Notifier
235
+ # Service description
236
+ class UseCaseName
237
+ def self.notify(*)
238
+ options = { fetch_options:, dispatch_options: }
239
+
240
+ begin
241
+ use_case = UseCases.use_case_build_function(options)
242
+
243
+ use_case.perform
244
+ rescue StandardError => e
245
+ { body: { message: e.message } }
246
+ end
247
+ end
248
+
249
+ def self.fetch_options
250
+ {
251
+ base_url: NOTION_BASE_URL,
252
+ database_id: NOTION_DATABASE_ID,
253
+ secret: NOTION_SECRET,
254
+ filter: {
255
+ filter: { "and": fetch_filter }
256
+ }
257
+ }
258
+ end
259
+
260
+ def self.fetch_filter
261
+ today = Common::TimeZone.set_colombia_time_zone.strftime('%F').to_s
262
+
263
+ [
264
+ { property: 'Desde?', date: { on_or_before: today } },
265
+ { property: 'Hasta?', date: { on_or_after: today } }
266
+ ]
267
+ end
268
+
269
+ def self.dispatch_options
270
+ {
271
+ webhook: DISCORD_WEBHOOK,
272
+ name: DISCORD_BOT_NAME
273
+ }
274
+ end
275
+
276
+ def self.format_options
277
+ {
278
+ template: ':beach: individual_name is on PTO'
279
+ }
280
+ end
281
+ end
282
+ end
283
+ ```
284
+
285
+ #### Configure the lambda
286
+ In the `serverless.yml` file, add a new instance in the `functions` block with this structure:
287
+
288
+ ```bash
289
+ functions:
290
+ ptoNotification:
291
+ handler: src/lambdas/pto_notification.Notifier::PTO.notify
292
+ environment:
293
+ PTO_NOTION_DATABASE_ID: ${file(./env.yml):${env:STAGE}.PTO_NOTION_DATABASE_ID}
294
+ PTO_NOTION_SECRET: ${file(./env.yml):${env:STAGE}.PTO_NOTION_SECRET}
295
+ PTO_DISCORD_WEBHOOK: ${file(./env.yml):${env:STAGE}.PTO_DISCORD_WEBHOOK}
296
+ PTO_DISCORD_BOT_NAME: ${file(./env.yml):${env:STAGE}.DISCORD_BOT_NAME}
297
+ events:
298
+ - schedule:
299
+ name: pto-daily-notification
300
+ description: "Notify every 24 hours at 8:30 a.m (UTC-5) from monday to friday"
301
+ rate: cron(${file(./env.yml):${env:STAGE}.PTO_SCHEDULER})
302
+ enabled: true
303
+ ```
304
+
305
+ #### Deploying
306
+
307
+ Configure the AWS keys:
308
+
309
+ ```bash
310
+ serverless config credentials --provider aws --key YOUR_KEY --secret YOUR_SECRET
311
+ ```
312
+
313
+ Deploy the project:
314
+ ```bash
315
+ STAGE=prod sls deploy --verbose
316
+ ```
317
+
318
+ ## Development
319
+
320
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
321
+
322
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
323
+
324
+ ## Changelog
325
+
326
+ Features and bug fixes are listed in the [CHANGELOG][changelog] file.
327
+
328
+ ## Code of conduct
329
+
330
+ We welcome everyone to contribute. Make sure you have read the [CODE_OF_CONDUCT][coc] before.
331
+
332
+ ## Contributing
333
+
334
+ For information on how to contribute, please refer to our [CONTRIBUTING][contributing] guide.
335
+
336
+ ## License
337
+
338
+ The gem is licensed under an MIT license. See [LICENSE][license] for details.
339
+
340
+ <br/>
341
+
342
+ <hr/>
343
+
344
+ [<img src="https://github.com/kommitters/chaincerts-smart-contracts/assets/1649973/d60d775f-166b-4968-89b6-8be847993f8c" width="80px" alt="kommit"/>](https://kommit.co)
345
+
346
+ <sub>
347
+
348
+ [Website][kommit-website] •
349
+ [Github][kommit-github] •
350
+ [X][kommit-x] •
351
+ [LinkedIn][kommit-linkedin]
352
+
353
+ </sub>
354
+
355
+ [license]: https://github.com/kommitters/bas/blob/main/LICENSE
356
+ [coc]: https://github.com/kommitters/bas/blob/main/CODE_OF_CONDUCT.md
357
+ [changelog]: https://github.com/kommitters/bas/blob/main/CHANGELOG.md
358
+ [contributing]: https://github.com/kommitters/bas/blob/main/CONTRIBUTING.md
359
+ [kommit-website]: https://kommit.co
360
+ [kommit-github]: https://github.com/kommitters
361
+ [kommit-x]: https://twitter.com/kommitco
362
+ [kommit-linkedin]: https://www.linkedin.com/company/kommit-co
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/SECURITY.md ADDED
@@ -0,0 +1,13 @@
1
+ # Security Policy
2
+
3
+ ## Reporting Security Issues
4
+
5
+ To report a security issue, you can either:
6
+ - Privately report a vulnerability through repository's Security tab by clicking "Report a vulnerability".
7
+ - Email us at [oss@kommit.co](mailto:oss@kommit.co).
8
+
9
+ Please, make sure to provide a description of the issue, the steps you took to create the issue, affected versions, and, if known, mitigations for the issue.
10
+
11
+ ## Responsible Disclosure
12
+
13
+ If the issue is confirmed as a vulnerability, we will open a Security Advisory and acknowledge your contributions as part of it.