blocks-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +197 -0
- data/Rakefile +4 -0
- data/docs/ARCHITECTURE.md +160 -0
- data/lib/blocks/sdk/base_client.rb +43 -0
- data/lib/blocks/sdk/cache/memory_cache.rb +92 -0
- data/lib/blocks/sdk/clients/translations_client.rb +136 -0
- data/lib/blocks/sdk/configuration.rb +38 -0
- data/lib/blocks/sdk/rails/controllers/api/translations_controller.rb +53 -0
- data/lib/blocks/sdk/rails/controllers/translations_webhooks_controller.rb +62 -0
- data/lib/blocks/sdk/rails/i18n_backend.rb +79 -0
- data/lib/blocks/sdk/rails/initializer.rb +25 -0
- data/lib/blocks/sdk/rails/railtie.rb +33 -0
- data/lib/blocks/sdk/rails/routes.rb +17 -0
- data/lib/blocks/sdk/rails/translations_route_mapper.rb +21 -0
- data/lib/blocks/sdk/rails.rb +12 -0
- data/lib/blocks/sdk/service_manager.rb +93 -0
- data/lib/blocks/sdk/version.rb +5 -0
- data/lib/blocks/sdk.rb +111 -0
- data/mise.toml +2 -0
- data/sig/blocks/sdk.rbs +6 -0
- metadata +107 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ac08c590b7f3183166bf999b2854b33ef8c30153dabfaa55890342613857e145
|
|
4
|
+
data.tar.gz: cbbb32696c580610edbe7da2bb3ba5a61e2bc414d48e17d6d53ecf280dc94dbc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 91460d37d72ac5463aee39b8dea358c767ec6e7e45b18840dd5ddc8633b3f3069d07a840d464920e3a01162df27ff1420c6795b548faeffdcb7485a4b3d3e912
|
|
7
|
+
data.tar.gz: 80984e3cbd6a233bf434a6778f21aa0d1de25090736e20da5e2538a01e171248789b441578a28340c4265ec1ab5b17f15bdfea48403627a82f6ff16c535bfad5
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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
|
|
10
|
+
identity 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 include:
|
|
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 overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
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 email 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
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
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 of
|
|
86
|
+
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 permanent
|
|
93
|
+
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 the
|
|
113
|
+
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 at
|
|
126
|
+
[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/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 bugloper
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# Blocks SDK
|
|
2
|
+
|
|
3
|
+
A Ruby gem that provides clients for Blocks Services, including translations, with Rails integration, caching, and webhook support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Service Clients**: Extensible client pattern for integrating with Blocks Services
|
|
8
|
+
- **Translation Service**: UILM translation client with caching
|
|
9
|
+
- **Rails Integration**: Automatic route setup and controller integration
|
|
10
|
+
- **In-Memory Caching**: Thread-safe cache for service responses
|
|
11
|
+
- **Webhook Support**: Cache invalidation via webhooks
|
|
12
|
+
- **Boot-time Loading**: Preload translations on Rails boot
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Add this line to your application's Gemfile:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
gem "blocks-sdk"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
And then execute:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bundle install
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Configuration
|
|
29
|
+
|
|
30
|
+
Create an initializer file `config/initializers/blocks_sdk.rb`:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
Blocks::Sdk.configure do |config|
|
|
34
|
+
config.uilm_base_url = ENV.fetch("UILM_BASE_URL")
|
|
35
|
+
config.uilm_project_key = ENV.fetch("UILM_PROJECT_KEY")
|
|
36
|
+
config.webhook_secret = ENV.fetch("BLOCKS_WEBHOOK_SECRET", nil) # Optional
|
|
37
|
+
config.cache_enabled = true
|
|
38
|
+
config.logger = Rails.logger
|
|
39
|
+
end
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Environment Variables
|
|
43
|
+
|
|
44
|
+
- `UILM_BASE_URL`: Base URL for UILM translation service
|
|
45
|
+
- `UILM_PROJECT_KEY`: Project key for authentication
|
|
46
|
+
- `BLOCKS_WEBHOOK_SECRET`: Secret for webhook authentication (optional)
|
|
47
|
+
- `BLOCKS_TRANSLATION_MODULES`: Comma-separated list of modules to preload (e.g., "module1,module2")
|
|
48
|
+
- `BLOCKS_TRANSLATION_LANGUAGES`: Comma-separated list of languages to preload (e.g., "en,de")
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### Frontend API Endpoint
|
|
53
|
+
|
|
54
|
+
The gem automatically creates a Rails API endpoint for fetching translations:
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
GET /blocks/api/translations?module_name=your_module&language=en
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**Query Parameters:**
|
|
61
|
+
- `module_name` (required): The module name to fetch translations for
|
|
62
|
+
- `language` (optional): Language code (defaults to "en")
|
|
63
|
+
|
|
64
|
+
**Response:**
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"key1": "translation1",
|
|
68
|
+
"key2": "translation2"
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Backend Usage
|
|
73
|
+
|
|
74
|
+
Fetch translations programmatically in your Rails application:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
# Fetch translations with caching
|
|
78
|
+
result = Blocks::Sdk.service_manager.fetch(
|
|
79
|
+
service_name: "translations",
|
|
80
|
+
params: { module_name: "your_module", language: "en" },
|
|
81
|
+
config: {
|
|
82
|
+
base_url: ENV["UILM_BASE_URL"],
|
|
83
|
+
project_key: ENV["UILM_PROJECT_KEY"]
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
if result[:translations]
|
|
88
|
+
translations = result[:translations]
|
|
89
|
+
# Use translations...
|
|
90
|
+
end
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Webhook Endpoint
|
|
94
|
+
|
|
95
|
+
The gem exposes a webhook endpoint for cache invalidation:
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
POST /blocks/webhooks/translations
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Headers:**
|
|
102
|
+
- `X-Blocks-Webhook-Secret`: Webhook secret (if configured)
|
|
103
|
+
|
|
104
|
+
**Body:**
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"module_name": "your_module",
|
|
108
|
+
"language": "en"
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
When a webhook is received, the cache for the specified module and language is automatically invalidated.
|
|
113
|
+
|
|
114
|
+
### Preloading Translations on Boot
|
|
115
|
+
|
|
116
|
+
To preload translations when Rails boots, set these environment variables:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
BLOCKS_TRANSLATION_MODULES=module1,module2,module3
|
|
120
|
+
BLOCKS_TRANSLATION_LANGUAGES=en,de
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Translations will be fetched and cached during Rails initialization (production environment only).
|
|
124
|
+
|
|
125
|
+
### I18n Integration (Optional)
|
|
126
|
+
|
|
127
|
+
To use Blocks SDK translations with Rails I18n instead of YAML files, configure a custom I18n backend:
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
# config/initializers/i18n.rb
|
|
131
|
+
require "blocks/sdk/rails/i18n_backend"
|
|
132
|
+
|
|
133
|
+
I18n.backend = Blocks::Sdk::Rails::I18nBackend.new(
|
|
134
|
+
service_manager: Blocks::Sdk.service_manager,
|
|
135
|
+
config: {
|
|
136
|
+
# Optional: default module name if not specified in scope
|
|
137
|
+
default_module_name: ENV["BLOCKS_DEFAULT_MODULE_NAME"]
|
|
138
|
+
}
|
|
139
|
+
)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Then use I18n as usual:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
I18n.t("key", module_name: "your_module", locale: "en")
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Then register it:
|
|
149
|
+
|
|
150
|
+
```ruby
|
|
151
|
+
Blocks::Sdk.service_manager.register_client("email", Blocks::Sdk::Clients::EmailClient)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Architecture
|
|
155
|
+
|
|
156
|
+
### Folder Structure
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
lib/blocks/sdk/
|
|
160
|
+
├── clients/ # Service clients
|
|
161
|
+
│ └── translations_client.rb
|
|
162
|
+
├── cache/ # Caching implementations
|
|
163
|
+
│ └── memory_cache.rb
|
|
164
|
+
├── rails/ # Rails integration
|
|
165
|
+
│ ├── controllers/ # API controllers
|
|
166
|
+
│ │ ├── api/
|
|
167
|
+
│ │ │ └── translations_controller.rb
|
|
168
|
+
│ │ └── webhooks_controller.rb
|
|
169
|
+
│ ├── route_mapper.rb # Route definitions
|
|
170
|
+
│ └── railtie.rb # Rails initialization
|
|
171
|
+
├── base_client.rb # Base client class
|
|
172
|
+
├── configuration.rb # Configuration management
|
|
173
|
+
├── service_manager.rb # Client and cache manager
|
|
174
|
+
└── sdk.rb # Main SDK module
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Key Components
|
|
178
|
+
|
|
179
|
+
- **BaseClient**: Base class for all service clients
|
|
180
|
+
- **ServiceManager**: Manages clients and caching
|
|
181
|
+
- **MemoryCache**: Thread-safe in-memory cache
|
|
182
|
+
- **Rails Controllers**: API endpoints for frontend and webhooks
|
|
183
|
+
- **Railtie**: Rails integration and initialization
|
|
184
|
+
|
|
185
|
+
## Development
|
|
186
|
+
|
|
187
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt.
|
|
188
|
+
|
|
189
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
190
|
+
|
|
191
|
+
## Contributing
|
|
192
|
+
|
|
193
|
+
Bug reports and pull requests are welcome on GitHub.
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Blocks SDK Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Blocks SDK is a Ruby gem that provides clients for Blocks Services. It follows a client pattern to allow easy integration with multiple services while maintaining a consistent API.
|
|
6
|
+
|
|
7
|
+
## Folder Structure
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
lib/blocks/sdk/
|
|
11
|
+
├── clients/ # Service clients
|
|
12
|
+
│ ├── translations_client.rb # UILM translation service client
|
|
13
|
+
│ └── example_client.rb # Template for creating new clients
|
|
14
|
+
├── cache/ # Caching implementations
|
|
15
|
+
│ └── memory_cache.rb # Thread-safe in-memory cache
|
|
16
|
+
├── rails/ # Rails integration
|
|
17
|
+
│ ├── controllers/ # API controllers
|
|
18
|
+
│ │ ├── api/
|
|
19
|
+
│ │ │ └── translations_controller.rb # FE translation endpoint
|
|
20
|
+
│ │ └── webhooks_controller.rb # Webhook handler
|
|
21
|
+
│ ├── i18n_backend.rb # Optional I18n backend integration
|
|
22
|
+
│ ├── initializer.rb # Initializer template
|
|
23
|
+
│ ├── railtie.rb # Rails initialization
|
|
24
|
+
│ ├── route_mapper.rb # Route definitions
|
|
25
|
+
│ └── routes.rb # Route helper (backward compat)
|
|
26
|
+
├── base_client.rb # Base class for all clients
|
|
27
|
+
├── configuration.rb # Configuration management
|
|
28
|
+
├── service_manager.rb # Client and cache manager
|
|
29
|
+
├── rails.rb # Rails module loader
|
|
30
|
+
├── sdk.rb # Main SDK module
|
|
31
|
+
└── version.rb # Gem version
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Component Details
|
|
35
|
+
|
|
36
|
+
### BaseClient
|
|
37
|
+
|
|
38
|
+
Base class for all service clients. Provides:
|
|
39
|
+
- Service name registration
|
|
40
|
+
- Configuration management
|
|
41
|
+
- Interface definition (`fetch`, `handle_webhook`)
|
|
42
|
+
|
|
43
|
+
### Clients
|
|
44
|
+
|
|
45
|
+
Service-specific implementations:
|
|
46
|
+
- **TranslationsClient**: Fetches translations from UILM API
|
|
47
|
+
- Future clients: Email, Notification, etc.
|
|
48
|
+
|
|
49
|
+
### ServiceManager
|
|
50
|
+
|
|
51
|
+
Central manager for:
|
|
52
|
+
- Registering clients
|
|
53
|
+
- Fetching data with caching
|
|
54
|
+
- Handling webhooks and cache invalidation
|
|
55
|
+
|
|
56
|
+
### MemoryCache
|
|
57
|
+
|
|
58
|
+
Thread-safe in-memory cache:
|
|
59
|
+
- Key-based storage
|
|
60
|
+
- Service/module/language invalidation
|
|
61
|
+
- Thread-safe operations
|
|
62
|
+
|
|
63
|
+
### Rails Integration
|
|
64
|
+
|
|
65
|
+
- **Railtie**: Auto-loads routes and initializes on boot
|
|
66
|
+
- **Controllers**: API endpoints for FE and webhooks
|
|
67
|
+
- **RouteMapper**: Defines routes under `/blocks` namespace
|
|
68
|
+
|
|
69
|
+
## Data Flow
|
|
70
|
+
|
|
71
|
+
### Frontend Request Flow
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
Frontend Request
|
|
75
|
+
↓
|
|
76
|
+
GET /blocks/api/translations?module_name=X&language=en
|
|
77
|
+
↓
|
|
78
|
+
TranslationsController#index
|
|
79
|
+
↓
|
|
80
|
+
ServiceManager#fetch
|
|
81
|
+
↓
|
|
82
|
+
Check MemoryCache
|
|
83
|
+
↓ (cache miss)
|
|
84
|
+
TranslationsClient#fetch
|
|
85
|
+
↓
|
|
86
|
+
UILM API
|
|
87
|
+
↓
|
|
88
|
+
Cache result
|
|
89
|
+
↓
|
|
90
|
+
Return to Frontend
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Webhook Flow
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
Blocks Service Webhook
|
|
97
|
+
↓
|
|
98
|
+
POST /blocks/webhooks/translations
|
|
99
|
+
↓
|
|
100
|
+
WebhooksController#create
|
|
101
|
+
↓
|
|
102
|
+
ServiceManager#handle_webhook
|
|
103
|
+
↓
|
|
104
|
+
TranslationsClient#handle_webhook
|
|
105
|
+
↓
|
|
106
|
+
MemoryCache#invalidate
|
|
107
|
+
↓
|
|
108
|
+
Cache cleared for module/language
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Boot-time Loading Flow
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
Rails Boot
|
|
115
|
+
↓
|
|
116
|
+
Railtie initializer
|
|
117
|
+
↓
|
|
118
|
+
Blocks::Sdk.load_translations_on_boot
|
|
119
|
+
↓
|
|
120
|
+
For each module/language:
|
|
121
|
+
ServiceManager#fetch
|
|
122
|
+
↓
|
|
123
|
+
Cache translations
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Extension Points
|
|
127
|
+
|
|
128
|
+
### Adding a New Client
|
|
129
|
+
|
|
130
|
+
1. Create client class inheriting from `BaseClient`
|
|
131
|
+
2. Implement `fetch` and `handle_webhook` methods
|
|
132
|
+
3. Register with `ServiceManager.register_client`
|
|
133
|
+
4. Add controller/route if needed
|
|
134
|
+
|
|
135
|
+
### Adding a New Endpoint
|
|
136
|
+
|
|
137
|
+
1. Create controller in `rails/controllers/`
|
|
138
|
+
2. Add route in `route_mapper.rb`
|
|
139
|
+
3. Controller uses `ServiceManager` to fetch data
|
|
140
|
+
|
|
141
|
+
## Configuration
|
|
142
|
+
|
|
143
|
+
Configuration is managed through:
|
|
144
|
+
- Environment variables (primary)
|
|
145
|
+
- `Blocks::Sdk.configure` block (secondary)
|
|
146
|
+
- Defaults in `Configuration` class
|
|
147
|
+
|
|
148
|
+
## Caching Strategy
|
|
149
|
+
|
|
150
|
+
- **Cache Key Format**: `service:module_name:language`
|
|
151
|
+
- **Cache Invalidation**: Via webhooks or manual deletion
|
|
152
|
+
- **Cache Storage**: In-memory (thread-safe)
|
|
153
|
+
- **Cache Lifetime**: Until invalidation or app restart
|
|
154
|
+
|
|
155
|
+
## Thread Safety
|
|
156
|
+
|
|
157
|
+
- `MemoryCache` uses `Mutex` for thread-safe operations
|
|
158
|
+
- `ServiceManager` is singleton, shared across threads
|
|
159
|
+
- Clients are stateless, can be instantiated per request
|
|
160
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Blocks
|
|
2
|
+
module Sdk
|
|
3
|
+
# Base class for all Blocks Service clients
|
|
4
|
+
# Subclasses should implement the service-specific logic
|
|
5
|
+
class BaseClient
|
|
6
|
+
class << self
|
|
7
|
+
attr_accessor :service_name
|
|
8
|
+
|
|
9
|
+
def inherited(subclass)
|
|
10
|
+
super
|
|
11
|
+
# Extract service name from class name (e.g., "TranslationsClient" -> "translations")
|
|
12
|
+
class_name = subclass.name.split("::").last
|
|
13
|
+
subclass.service_name = class_name.gsub(/Client$/, "").gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
14
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
15
|
+
.downcase
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
attr_reader :config
|
|
20
|
+
|
|
21
|
+
def initialize(config = {})
|
|
22
|
+
@config = config
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Override in subclasses to fetch data from the service
|
|
26
|
+
def fetch
|
|
27
|
+
raise NotImplementedError, "#{self.class} must implement #fetch"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Override in subclasses to handle webhook callbacks
|
|
31
|
+
def handle_webhook(payload)
|
|
32
|
+
raise NotImplementedError, "#{self.class} must implement #handle_webhook"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
protected
|
|
36
|
+
|
|
37
|
+
def service_name
|
|
38
|
+
self.class.service_name
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
require "thread"
|
|
2
|
+
|
|
3
|
+
module Blocks
|
|
4
|
+
module Sdk
|
|
5
|
+
module Cache
|
|
6
|
+
# Thread-safe in-memory cache for Blocks Service data
|
|
7
|
+
class MemoryCache
|
|
8
|
+
def initialize
|
|
9
|
+
@cache = {}
|
|
10
|
+
@mutex = Mutex.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Get cached value for a key
|
|
14
|
+
# @param key [String] Cache key
|
|
15
|
+
# @return [Object, nil] Cached value or nil if not found
|
|
16
|
+
def get(key)
|
|
17
|
+
@mutex.synchronize do
|
|
18
|
+
@cache[key]
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Set a value in cache
|
|
23
|
+
# @param key [String] Cache key
|
|
24
|
+
# @param value [Object] Value to cache
|
|
25
|
+
def set(key, value)
|
|
26
|
+
@mutex.synchronize do
|
|
27
|
+
@cache[key] = value
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Delete a specific key from cache
|
|
32
|
+
# @param key [String] Cache key to delete
|
|
33
|
+
def delete(key)
|
|
34
|
+
@mutex.synchronize do
|
|
35
|
+
@cache.delete(key)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Invalidate cache for a service/module/language combination
|
|
40
|
+
# @param service [String] Service name (e.g., "translations")
|
|
41
|
+
# @param module_name [String] Module name
|
|
42
|
+
# @param language [String] Language code
|
|
43
|
+
def invalidate(service:, module_name:, language: nil)
|
|
44
|
+
@mutex.synchronize do
|
|
45
|
+
if language
|
|
46
|
+
# Invalidate specific language
|
|
47
|
+
key = build_key(service, module_name, language)
|
|
48
|
+
@cache.delete(key)
|
|
49
|
+
else
|
|
50
|
+
# Invalidate all languages for this module
|
|
51
|
+
@cache.keys.each do |key|
|
|
52
|
+
if key.start_with?("#{service}:#{module_name}:")
|
|
53
|
+
@cache.delete(key)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Clear all cache
|
|
61
|
+
def clear
|
|
62
|
+
@mutex.synchronize do
|
|
63
|
+
@cache.clear
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Get all cache keys (useful for debugging)
|
|
68
|
+
def keys
|
|
69
|
+
@mutex.synchronize do
|
|
70
|
+
@cache.keys.dup
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Check if a key exists in cache
|
|
75
|
+
# @param key [String] Cache key
|
|
76
|
+
# @return [Boolean]
|
|
77
|
+
def exists?(key)
|
|
78
|
+
@mutex.synchronize do
|
|
79
|
+
@cache.key?(key)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def build_key(service, module_name, language)
|
|
86
|
+
"#{service}:#{module_name}:#{language}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|