hyraft-rule 0.1.0.alpha2 → 1.0.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 +4 -4
- data/CHANGELOG.md +10 -2
- data/README.md +140 -56
- data/exe/hyr-rule +0 -0
- data/exe/hyr-rule-db +0 -0
- data/exe/hyraft-rule +0 -0
- data/exe/hyraft-rule-db +3 -2
- data/hyraft-rule.gemspec +16 -15
- data/lib/hyraft/rule/base/in_api_command.rb +208 -0
- data/lib/hyraft/rule/base/out_data_command.rb +264 -0
- data/lib/hyraft/rule/circuit/engine_command.rb +169 -0
- data/lib/hyraft/rule/circuit/port_command.rb +70 -0
- data/lib/hyraft/rule/command.rb +46 -37
- data/lib/hyraft/rule/connectors/cable_command.rb +133 -0
- data/lib/hyraft/rule/jwt/jwt_command.rb +133 -0
- data/lib/hyraft/rule/router/api_routes_command.rb +93 -0
- data/lib/hyraft/rule/router/web_routes_command.rb +213 -0
- data/lib/hyraft/rule/template/template_command.rb +347 -0
- data/lib/hyraft/rule/version.rb +1 -1
- data/lib/hyraft/rule.rb +9 -17
- metadata +16 -27
- data/lib/hyraft/rule/adapter_exhaust/data_gateway_command.rb +0 -132
- data/lib/hyraft/rule/adapter_request/remove_adapter_command.rb +0 -76
- data/lib/hyraft/rule/adapter_request/web_adapter_command.rb +0 -211
- data/lib/hyraft/rule/assemble_command.rb +0 -98
- data/lib/hyraft/rule/disassemble_command.rb +0 -139
- data/lib/hyraft/rule/engine/circuit_command.rb +0 -82
- data/lib/hyraft/rule/engine/port_command.rb +0 -60
- data/lib/hyraft/rule/engine/source_command.rb +0 -70
- data/lib/hyraft/rule/template_command.rb +0 -102
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3ceb5f5d19fd16ef3f5f54d282e81d1e15db13cac65137688889280762ab47a1
|
|
4
|
+
data.tar.gz: 14b7bd672260e3ad84089ed5c3f13282808317b6a564a6f1669a401b820f629f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b76d6da0d1df616cb966c523b900b93e471536a18caed301b605d5adac7ade0535d0e48e5d03076a101a27287cbb36bd8aabdb95fc196ee4d27a4145f1f40f8a
|
|
7
|
+
data.tar.gz: f1e7505c49b11ec307cfc6592bdc8af86cc6f4eb12b7b5bb1dbbb2f6869b8b3f0f1e4137c5b92fa2eb819a06d43eafb1111031845a89c070c7ab75653d5603d9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
-
## [
|
|
3
|
+
## [1.0.0] - 2026-7-12
|
|
4
|
+
### Fixed
|
|
5
|
+
Supports Ruby 4 and above only.
|
|
4
6
|
|
|
5
|
-
-
|
|
7
|
+
## [0.1.0.alpha2] - 2025-11-30
|
|
8
|
+
### Fixed
|
|
9
|
+
- Fixed README documentation
|
|
10
|
+
- Add safety flag to disassemble command
|
|
11
|
+
|
|
12
|
+
## [0.1.0.alpha1] - 2025-11-26
|
|
13
|
+
- Initial release
|
data/README.md
CHANGED
|
@@ -29,77 +29,128 @@ This is a standalone command-line interface (CLI) for the Hyraft framework.
|
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
## Usage (
|
|
32
|
+
## Usage ( in-api )
|
|
33
33
|
|
|
34
|
+
Generate:
|
|
34
35
|
|
|
35
36
|
```bash
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
hyraft-rule in-api articles title content
|
|
38
|
+
hyraft-rule in-api users name email role
|
|
39
|
+
hyraft-rule in-api products name price stock
|
|
40
|
+
hyraft-rule in-api posts title body author
|
|
39
41
|
```
|
|
42
|
+
## Usage ( out-data )
|
|
43
|
+
|
|
44
|
+
Generate:
|
|
40
45
|
|
|
41
46
|
```bash
|
|
42
|
-
# Default web-app
|
|
43
|
-
hyr-rule assemble articles
|
|
44
|
-
hyr-rule disassemble articles
|
|
45
47
|
|
|
46
|
-
#
|
|
48
|
+
# Generate storage for any resource
|
|
49
|
+
hyraft-rule out-data articles title content
|
|
50
|
+
hyraft-rule out-data users name email password
|
|
51
|
+
hyraft-rule out-data products name price stock quantity
|
|
52
|
+
hyraft-rule out-data comments body author_id post_id
|
|
53
|
+
hyraft-rule out-data posts title content published
|
|
54
|
+
hyraft-rule out-data categories name description
|
|
55
|
+
hyraft-rule out-data tags name
|
|
47
56
|
|
|
48
|
-
|
|
57
|
+
```
|
|
49
58
|
|
|
50
59
|
|
|
51
|
-
hyr-rule assemble admin-app/users
|
|
52
|
-
hyr-rule disassemble admin-app/users
|
|
53
60
|
|
|
54
|
-
hyr-rule assemble api-app/products
|
|
55
|
-
hyr-rule disassemble api-app/products
|
|
56
|
-
```
|
|
57
61
|
|
|
62
|
+
## Usage ( Engine )
|
|
63
|
+
|
|
64
|
+
Generate:
|
|
58
65
|
|
|
66
|
+
```bash
|
|
67
|
+
# Generate engine with attributes
|
|
68
|
+
hyraft-rule engine articles title content
|
|
59
69
|
|
|
60
|
-
|
|
70
|
+
# With custom target directory
|
|
71
|
+
hyraft-rule engine articles title content ./myapp
|
|
72
|
+
|
|
73
|
+
# Without attributes (will show placeholder)
|
|
74
|
+
hyraft-rule engine articles
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Usage ( Port )
|
|
61
78
|
|
|
62
79
|
Generate:
|
|
63
80
|
|
|
64
81
|
```bash
|
|
82
|
+
# Basic usage
|
|
83
|
+
hyraft-rule port articles
|
|
65
84
|
|
|
66
|
-
|
|
85
|
+
# Different resources
|
|
86
|
+
hyraft-rule port users
|
|
87
|
+
hyraft-rule port products
|
|
88
|
+
hyraft-rule port comments
|
|
67
89
|
|
|
68
|
-
|
|
90
|
+
# With custom path
|
|
91
|
+
hyraft-rule port articles ./my_project
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Usage ( Cable )
|
|
95
|
+
|
|
96
|
+
Generate:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
# Generate cable for articles
|
|
100
|
+
hyraft-rule cable articles title content
|
|
101
|
+
|
|
102
|
+
# Generate cable for users
|
|
103
|
+
hyraft-rule cable users name email password
|
|
69
104
|
|
|
70
|
-
|
|
105
|
+
# Generate cable for products
|
|
106
|
+
hyraft-rule cable products name price stock
|
|
71
107
|
|
|
108
|
+
# Generate cable for comments
|
|
109
|
+
hyraft-rule cable comments body author_id post_id
|
|
72
110
|
```
|
|
73
111
|
|
|
74
|
-
## Usage (
|
|
112
|
+
## Usage ( JWT key )
|
|
75
113
|
|
|
76
114
|
Generate:
|
|
77
115
|
|
|
78
116
|
```bash
|
|
79
117
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
118
|
+
hyraft-rule jwt generate
|
|
119
|
+
hyraft-rule jwt g
|
|
120
|
+
hyraft-rule jwt show
|
|
121
|
+
hyraft-rule jwt help
|
|
83
122
|
|
|
84
|
-
# Only remove the specific web adapter
|
|
85
|
-
hyr-rule remove-adapter articles
|
|
86
|
-
hyr-rule remove-adapter admin-app/users
|
|
87
123
|
```
|
|
88
124
|
|
|
89
|
-
## Usage (
|
|
125
|
+
## Usage ( Routes )
|
|
90
126
|
|
|
91
127
|
Generate:
|
|
92
128
|
|
|
93
129
|
```bash
|
|
94
|
-
|
|
95
|
-
|
|
130
|
+
|
|
131
|
+
# Add traditional routes for articles
|
|
132
|
+
hyraft-rule routes articles tra
|
|
133
|
+
|
|
134
|
+
# Add SPA routes for articles
|
|
135
|
+
hyraft-rule routes articles spa
|
|
136
|
+
|
|
137
|
+
# Default is tra
|
|
138
|
+
hyraft-rule routes articles
|
|
139
|
+
|
|
96
140
|
```
|
|
97
141
|
|
|
142
|
+
## Usage ( API Routes )
|
|
143
|
+
|
|
144
|
+
Generate:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
98
147
|
|
|
148
|
+
hyr-rule api-routes articles
|
|
149
|
+
hyraft-rule api-routes articles
|
|
150
|
+
```
|
|
99
151
|
|
|
100
152
|
|
|
101
153
|
## Usage ( Templates )
|
|
102
|
-
- hyr-rule
|
|
103
154
|
|
|
104
155
|
Generate:
|
|
105
156
|
|
|
@@ -112,8 +163,8 @@ hyr-rule template admin-app/users
|
|
|
112
163
|
hyraft-rule template admin-app/users
|
|
113
164
|
```
|
|
114
165
|
|
|
166
|
+
|
|
115
167
|
## Usage ( For Help )
|
|
116
|
-
- hyr-rule
|
|
117
168
|
|
|
118
169
|
Generate:
|
|
119
170
|
|
|
@@ -135,37 +186,37 @@ hyraft-rule -h
|
|
|
135
186
|
|
|
136
187
|
|
|
137
188
|
## Usage ( Database )
|
|
138
|
-
-
|
|
189
|
+
- hyraft-rule-db
|
|
139
190
|
|
|
140
191
|
|
|
141
192
|
Schema:
|
|
142
193
|
|
|
143
194
|
|
|
144
195
|
```bash
|
|
145
|
-
|
|
146
|
-
|
|
196
|
+
hyraft-rule-db generate create_users
|
|
197
|
+
hyraft-rule-db generate add_email_to_users
|
|
147
198
|
|
|
148
199
|
- Migration Generation Schema Examples:
|
|
149
200
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
201
|
+
hyraft-rule-db generate create_users
|
|
202
|
+
hyraft-rule-db generate create_articles
|
|
203
|
+
hyraft-rule-db generate create_products
|
|
204
|
+
hyraft-rule-db generate create_categories
|
|
205
|
+
hyraft-rule-db generate add_image_file_to_articles
|
|
206
|
+
hyraft-rule-db generate add_price_to_products
|
|
207
|
+
hyraft-rule-db generate remove_status_from_posts
|
|
208
|
+
hyraft-rule-db generate drop_old_tables
|
|
158
209
|
|
|
159
210
|
|
|
160
211
|
Examples:
|
|
161
|
-
|
|
212
|
+
hyraft-rule-db generate create_articles title content:text
|
|
162
213
|
|
|
163
|
-
|
|
164
|
-
|
|
214
|
+
hyraft-rule-db generate create_products name:string price:decimal description:text user_id:int
|
|
215
|
+
hyraft-rule-db generate create_users username:string age:int email:string active:bool created_at:datetime
|
|
165
216
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
217
|
+
hyraft-rule-db generate create_products name:string price:decimal description:text user_id:int
|
|
218
|
+
hyraft-rule-db generate create_users username age:int email active:bool
|
|
219
|
+
hyraft-rule-db generate create_articles title content:text published_at:datetime views:int
|
|
169
220
|
|
|
170
221
|
|
|
171
222
|
```
|
|
@@ -185,18 +236,18 @@ Migrations:
|
|
|
185
236
|
|
|
186
237
|
Examples:
|
|
187
238
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
239
|
+
hyraft-rule-db migrate
|
|
240
|
+
hyraft-rule-db status
|
|
241
|
+
hyraft-rule-db rollback
|
|
242
|
+
hyraft-rule-db reset
|
|
243
|
+
hyraft-rule-db help
|
|
193
244
|
|
|
194
245
|
Environment Usage:
|
|
195
246
|
|
|
196
|
-
|
|
197
|
-
APP_ENV=test
|
|
198
|
-
APP_ENV=development
|
|
199
|
-
APP_ENV=production
|
|
247
|
+
hyraft-rule-db migrate # Current environment (run: hyr s thin)
|
|
248
|
+
APP_ENV=test hyraft-rule-db migrate # Test environment (run: APP_ENV=test hyr s thin)
|
|
249
|
+
APP_ENV=development hyraft-rule-db migrate # Development environment (run: APP_ENV=development hyr s thin)
|
|
250
|
+
APP_ENV=production hyraft-rule-db migrate # Production environment (run: APP_ENV=production hyr s thin)
|
|
200
251
|
|
|
201
252
|
Quick Reference:
|
|
202
253
|
|
|
@@ -230,6 +281,39 @@ After checking out the repo, run `bundle install` to install dependencies.
|
|
|
230
281
|
|
|
231
282
|
Then, run `rake test` to run the tests.
|
|
232
283
|
|
|
284
|
+
```bash
|
|
285
|
+
|
|
286
|
+
rake test
|
|
287
|
+
|
|
288
|
+
comment this: FileUtils.rm_rf(TEST_APP_DIR) located at test_helper.rb
|
|
289
|
+
|
|
290
|
+
- To generate files
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
# Run single test file
|
|
294
|
+
rake test TEST=test/hyraft/rule/circuit/port_command_test.rb
|
|
295
|
+
|
|
296
|
+
or
|
|
297
|
+
|
|
298
|
+
ruby -Ilib:test test/hyraft/rule/circuit/port_command_test.rb
|
|
299
|
+
|
|
300
|
+
# Run specific test method
|
|
301
|
+
rake test TEST=test/hyraft/rule/circuit/port_command_test.rb -n test_generates_port_file
|
|
302
|
+
|
|
303
|
+
# Run with pattern
|
|
304
|
+
rake test TEST=test/hyraft/rule/circuit/port_command_test.rb -n /test_generates/
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
rake test TEST=test/hyraft/rule/base/in_api_command_test.rb
|
|
309
|
+
rake test TEST=test/hyraft/rule/circuit/engine_command_test.rb
|
|
310
|
+
rake test TEST=test/hyraft/rule/connectors/cable_command_test.rb
|
|
311
|
+
rake test TEST=test/hyraft/rule/base/out_data_command_test.rb
|
|
312
|
+
|
|
313
|
+
rake test TEST=test/hyraft/rule/jwt/jwt_command_test.rb
|
|
314
|
+
|
|
315
|
+
```
|
|
316
|
+
|
|
233
317
|
|
|
234
318
|
## Contributing
|
|
235
319
|
|
data/exe/hyr-rule
CHANGED
|
File without changes
|
data/exe/hyr-rule-db
CHANGED
|
File without changes
|
data/exe/hyraft-rule
CHANGED
|
File without changes
|
data/exe/hyraft-rule-db
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# frozen_string_literal: true
|
|
3
|
+
#hyarft-rule-db.rb
|
|
3
4
|
|
|
4
5
|
require 'fileutils'
|
|
5
6
|
require 'sequel'
|
|
@@ -66,7 +67,7 @@ $LOAD_PATH.unshift(APP_ROOT) unless $LOAD_PATH.include?(APP_ROOT)
|
|
|
66
67
|
|
|
67
68
|
begin
|
|
68
69
|
require File.join(APP_ROOT, 'boot')
|
|
69
|
-
require File.join(APP_ROOT, '
|
|
70
|
+
require File.join(APP_ROOT, 'setup', 'database', 'sequel_connection')
|
|
70
71
|
rescue LoadError => e
|
|
71
72
|
puts "❌ Error loading dependencies: #{e.message}"
|
|
72
73
|
exit 1
|
|
@@ -81,7 +82,7 @@ rescue => e
|
|
|
81
82
|
exit 1
|
|
82
83
|
end
|
|
83
84
|
|
|
84
|
-
MIGRATIONS_DIR = File.join(APP_ROOT, '
|
|
85
|
+
MIGRATIONS_DIR = File.join(APP_ROOT, 'setup', 'database', 'migrations')
|
|
85
86
|
|
|
86
87
|
# -------------------------------
|
|
87
88
|
# Generate Migration Methods
|
data/hyraft-rule.gemspec
CHANGED
|
@@ -12,16 +12,17 @@ Gem::Specification.new do |spec|
|
|
|
12
12
|
spec.description = "This is a standalone command-line interface (CLI) for the Hyraft framework."
|
|
13
13
|
spec.homepage = "https://github.com/demjhonsilver/hyraft-rule"
|
|
14
14
|
spec.license = "MIT"
|
|
15
|
-
spec.required_ruby_version = ">=
|
|
16
|
-
|
|
15
|
+
spec.required_ruby_version = ">= 4.0.0"
|
|
17
16
|
|
|
18
17
|
|
|
19
18
|
spec.metadata = {
|
|
19
|
+
"homepage_uri" => "https://github.com/demjhonsilver/hyraft-rule",
|
|
20
20
|
"documentation_uri" => "https://www.rubydoc.info/gems/hyraft-rule",
|
|
21
|
-
"source_code_uri"
|
|
21
|
+
"source_code_uri" => "https://github.com/demjhonsilver/hyraft-rule/tree/main",
|
|
22
|
+
"changelog_uri" => "https://github.com/demjhonsilver/hyraft-rule/blob/main/CHANGELOG.md"
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
|
|
25
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
25
26
|
|
|
26
27
|
# Manual file listing without git dependency
|
|
27
28
|
# In hyraft-rule.gemspec - update the files section
|
|
@@ -33,15 +34,15 @@ Gem::Specification.new do |spec|
|
|
|
33
34
|
"lib/hyraft/rule.rb",
|
|
34
35
|
"lib/hyraft/rule/version.rb",
|
|
35
36
|
"lib/hyraft/rule/command.rb",
|
|
36
|
-
"lib/hyraft/rule/
|
|
37
|
-
"lib/hyraft/rule/
|
|
38
|
-
"lib/hyraft/rule/
|
|
39
|
-
"lib/hyraft/rule/
|
|
40
|
-
"lib/hyraft/rule/
|
|
41
|
-
"lib/hyraft/rule/
|
|
42
|
-
"lib/hyraft/rule/
|
|
43
|
-
"lib/hyraft/rule/
|
|
44
|
-
|
|
37
|
+
"lib/hyraft/rule/base/in_api_command.rb",
|
|
38
|
+
"lib/hyraft/rule/base/out_data_command.rb",
|
|
39
|
+
"lib/hyraft/rule/connectors/cable_command.rb",
|
|
40
|
+
"lib/hyraft/rule/circuit/port_command.rb",
|
|
41
|
+
"lib/hyraft/rule/circuit/engine_command.rb",
|
|
42
|
+
"lib/hyraft/rule/router/web_routes_command.rb",
|
|
43
|
+
"lib/hyraft/rule/router/api_routes_command.rb",
|
|
44
|
+
"lib/hyraft/rule/template/template_command.rb",
|
|
45
|
+
"lib/hyraft/rule/jwt/jwt_command.rb",
|
|
45
46
|
"LICENSE.txt",
|
|
46
47
|
"README.md",
|
|
47
48
|
"CHANGELOG.md",
|
|
@@ -55,5 +56,5 @@ Gem::Specification.new do |spec|
|
|
|
55
56
|
|
|
56
57
|
# Runtime dependencies
|
|
57
58
|
spec.add_development_dependency "rake", "~> 13.3"
|
|
58
|
-
spec.add_development_dependency "minitest", "~> 5.26"
|
|
59
|
+
# spec.add_development_dependency "minitest", "~> 5.26"
|
|
59
60
|
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# lib/hyraft/rule/base/in_api_command.rb
|
|
2
|
+
|
|
3
|
+
# frozen_string_literal: true
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
require 'fileutils'
|
|
7
|
+
|
|
8
|
+
module Hyraft
|
|
9
|
+
module Rule
|
|
10
|
+
module Base
|
|
11
|
+
class InApiCommand
|
|
12
|
+
def self.start(args)
|
|
13
|
+
resource_name = args[0]
|
|
14
|
+
return show_usage unless resource_name
|
|
15
|
+
|
|
16
|
+
plural_name = resource_name.downcase
|
|
17
|
+
attributes = []
|
|
18
|
+
|
|
19
|
+
if args.length > 1
|
|
20
|
+
last_arg = args[-1]
|
|
21
|
+
if last_arg.include?('/') || last_arg == '.' || last_arg == '..' || last_arg.start_with?('./')
|
|
22
|
+
target_dir = last_arg
|
|
23
|
+
attributes = args[1..-2] if args.length > 2
|
|
24
|
+
else
|
|
25
|
+
target_dir = "."
|
|
26
|
+
attributes = args[1..-1]
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
target_dir = "."
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
singular_name = plural_name.chomp('s')
|
|
33
|
+
class_name = resource_name.split('_').map(&:capitalize).join
|
|
34
|
+
resource_class = singular_name.capitalize
|
|
35
|
+
|
|
36
|
+
filename = "#{plural_name}_request.rb"
|
|
37
|
+
request_dir = File.join(target_dir, "base-adapters", "in-api")
|
|
38
|
+
full_path = File.join(request_dir, filename)
|
|
39
|
+
|
|
40
|
+
FileUtils.mkdir_p(request_dir)
|
|
41
|
+
|
|
42
|
+
# Ruby 4.0 Box syntax for template
|
|
43
|
+
template = %{
|
|
44
|
+
# base-adapters/in-api/#{plural_name}_request.rb
|
|
45
|
+
|
|
46
|
+
pathway 'connectors/cable/#{plural_name}_cable'
|
|
47
|
+
|
|
48
|
+
require 'json'
|
|
49
|
+
|
|
50
|
+
class #{class_name}Request
|
|
51
|
+
include HtmlSafety
|
|
52
|
+
|
|
53
|
+
def create(env)
|
|
54
|
+
input = env['BODY']
|
|
55
|
+
|
|
56
|
+
if input.nil? || input.empty?
|
|
57
|
+
return json_response({ error: 'Empty body' }, 400)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
begin
|
|
61
|
+
data = JSON.parse(input, symbolize_names: true)
|
|
62
|
+
#{attributes.map { |a| "#{a} = data[:#{a}].to_s" }.join("\n ")}
|
|
63
|
+
|
|
64
|
+
if #{attributes.map { |a| "#{a}.empty?" }.join(" || ")}
|
|
65
|
+
return json_response({ error: '#{attributes.map(&:capitalize).join(' and ')} required' }, 422)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
#{attributes.map { |a| "safe_#{a} = safety(#{a})" }.join("\n ")}
|
|
69
|
+
|
|
70
|
+
result = #{class_name}Cable.create_#{singular_name}(#{attributes.map { |a| "safe_#{a}" }.join(", ")})
|
|
71
|
+
|
|
72
|
+
safe_result = {
|
|
73
|
+
id: result[:id],
|
|
74
|
+
#{attributes.map { |a| "#{a}: safety(result[:#{a}].to_s)," }.join("\n ")}
|
|
75
|
+
created_at: result[:created_at],
|
|
76
|
+
updated_at: result[:updated_at]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
json_response({
|
|
80
|
+
data: safe_result,
|
|
81
|
+
message: '#{resource_class} created successfully!'
|
|
82
|
+
}, 201)
|
|
83
|
+
|
|
84
|
+
rescue JSON::ParserError
|
|
85
|
+
json_response({ error: 'Invalid JSON' }, 400)
|
|
86
|
+
rescue StandardError => e
|
|
87
|
+
json_response({ error: safety(e.message) }, 422)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def index(env)
|
|
92
|
+
items = #{class_name}Cable.list_#{plural_name}
|
|
93
|
+
all_items = items.is_a?(Array) ? items.reverse : []
|
|
94
|
+
|
|
95
|
+
safe_items = all_items.map do |item|
|
|
96
|
+
{
|
|
97
|
+
id: item[:id],
|
|
98
|
+
#{attributes.map { |a| "#{a}: safety(item[:#{a}].to_s)," }.join("\n ")}
|
|
99
|
+
created_at: item[:created_at],
|
|
100
|
+
updated_at: item[:updated_at]
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
json_response({ data: safe_items }, 200)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def show(env, id)
|
|
108
|
+
begin
|
|
109
|
+
result = #{class_name}Cable.get_#{singular_name}(id)
|
|
110
|
+
|
|
111
|
+
if result
|
|
112
|
+
safe_result = {
|
|
113
|
+
id: result[:id],
|
|
114
|
+
#{attributes.map { |a| "#{a}: safety(result[:#{a}].to_s)," }.join("\n ")}
|
|
115
|
+
created_at: result[:created_at],
|
|
116
|
+
updated_at: result[:updated_at]
|
|
117
|
+
}
|
|
118
|
+
json_response({ data: safe_result }, 200)
|
|
119
|
+
else
|
|
120
|
+
json_response({ error: '#{resource_class} not found' }, 404)
|
|
121
|
+
end
|
|
122
|
+
rescue StandardError => e
|
|
123
|
+
json_response({ error: safety(e.message) }, 404)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def update(env, id)
|
|
128
|
+
input = env['BODY']
|
|
129
|
+
|
|
130
|
+
if input.nil? || input.empty?
|
|
131
|
+
return json_response({ error: 'Empty body' }, 400)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
begin
|
|
135
|
+
data = JSON.parse(input, symbolize_names: true)
|
|
136
|
+
#{attributes.map { |a| "#{a} = data[:#{a}].to_s" }.join("\n ")}
|
|
137
|
+
|
|
138
|
+
if #{attributes.map { |a| "#{a}.empty?" }.join(" || ")}
|
|
139
|
+
return json_response({ error: '#{attributes.map(&:capitalize).join(' and ')} required' }, 422)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
#{attributes.map { |a| "safe_#{a} = safety(#{a})" }.join("\n ")}
|
|
143
|
+
|
|
144
|
+
result = #{class_name}Cable.update_#{singular_name}(id, #{attributes.map { |a| "safe_#{a}" }.join(", ")})
|
|
145
|
+
|
|
146
|
+
safe_result = {
|
|
147
|
+
id: result[:id],
|
|
148
|
+
#{attributes.map { |a| "#{a}: safety(result[:#{a}].to_s)," }.join("\n ")}
|
|
149
|
+
created_at: result[:created_at],
|
|
150
|
+
updated_at: result[:updated_at]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
json_response({ data: safe_result, message: '#{resource_class} updated successfully!' }, 200)
|
|
154
|
+
|
|
155
|
+
rescue JSON::ParserError
|
|
156
|
+
json_response({ error: 'Invalid JSON' }, 400)
|
|
157
|
+
rescue StandardError => e
|
|
158
|
+
if e.message == "Not found"
|
|
159
|
+
json_response({ error: safety(e.message) }, 404)
|
|
160
|
+
else
|
|
161
|
+
json_response({ error: safety(e.message) }, 422)
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def delete(env, id)
|
|
167
|
+
begin
|
|
168
|
+
#{class_name}Cable.delete_#{singular_name}(id)
|
|
169
|
+
json_response({ data: { deleted: true, id: id, message: '#{resource_class} deleted successfully!' } }, 200)
|
|
170
|
+
rescue StandardError => e
|
|
171
|
+
if e.message == "Not found"
|
|
172
|
+
json_response({ error: safety(e.message) }, 404)
|
|
173
|
+
else
|
|
174
|
+
json_response({ error: safety(e.message) }, 422)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
private
|
|
180
|
+
|
|
181
|
+
def json_response(data, status)
|
|
182
|
+
body = data.to_json
|
|
183
|
+
[status, { 'Content-Type' => 'application/json' }, [body]]
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
}.strip
|
|
187
|
+
|
|
188
|
+
File.write(full_path, template)
|
|
189
|
+
|
|
190
|
+
puts "\e[94m✓ Created API request: #{full_path}\e[0m"
|
|
191
|
+
puts "\e[38;5;214mResource: #{plural_name}"
|
|
192
|
+
puts "Attributes: #{attributes.join(', ')}\e[0m" if attributes.any?
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def self.show_usage
|
|
196
|
+
puts "Usage: hyraft-rule in-api <resource_name> [attributes...] [target_dir]"
|
|
197
|
+
puts ""
|
|
198
|
+
puts "Examples:"
|
|
199
|
+
puts " hyraft-rule in-api articles title content"
|
|
200
|
+
puts " hyraft-rule in-api admins username password email"
|
|
201
|
+
puts " hyraft-rule in-api comments body author_id"
|
|
202
|
+
puts ""
|
|
203
|
+
puts "This creates: base-adapters/in-api/<resource_name>_request.rb"
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|