wrappix 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/.rubocop.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +216 -0
- data/Rakefile +12 -0
- data/bin/wrappix +7 -0
- data/lib/wrappix/builder.rb +207 -0
- data/lib/wrappix/cli.rb +24 -0
- data/lib/wrappix/client.rb +39 -0
- data/lib/wrappix/configuration.rb +25 -0
- data/lib/wrappix/request.rb +74 -0
- data/lib/wrappix/templates/cache.rb +62 -0
- data/lib/wrappix/templates/client.rb +35 -0
- data/lib/wrappix/templates/collection.rb +32 -0
- data/lib/wrappix/templates/configuration.rb +67 -0
- data/lib/wrappix/templates/documentation.rb +241 -0
- data/lib/wrappix/templates/error.rb +25 -0
- data/lib/wrappix/templates/main.rb +52 -0
- data/lib/wrappix/templates/object.rb +33 -0
- data/lib/wrappix/templates/readme.rb +258 -0
- data/lib/wrappix/templates/request.rb +129 -0
- data/lib/wrappix/templates/resource.rb +115 -0
- data/lib/wrappix/templates/tests.rb +368 -0
- data/lib/wrappix/version.rb +5 -0
- data/lib/wrappix.rb +29 -0
- data/sig/wrappix.rbs +4 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f997a7533767b633f14e75e8af8e148d44cd109bfe9f388afe311ab17fe67c21
|
4
|
+
data.tar.gz: e593de626494696212257302c423f8b5fb144d36ab1396f353e94c33774e802d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ebdedd2da469565ab2ce1a0d851188849f8bc18c582ba64dd00bf37291fbf4058939e410de7459037cf9f80242eeb28a2bc852b9f249f82043c030a7cf1d5036
|
7
|
+
data.tar.gz: cc2fec8bad1f78a50f5af11722d00cadbd475d2c797b62aad3c5020c8f3b6655b4433d109be84444f9b93cce272d665f25f2a12f1254a1b856c4ebe8ea11d8e2
|
data/.rubocop.yml
ADDED
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 Bruno Costanzo
|
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,216 @@
|
|
1
|
+
# Wrappix
|
2
|
+
|
3
|
+
Wrappix is a code generator that creates Ruby API client libraries from a YAML configuration file. It helps you build structured wrappers for REST APIs with minimal effort.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- **Complete code generation**: Creates all classes and methods needed to interact with a REST API
|
8
|
+
- **Configurable**: Define resources, endpoints, and parameters in a simple YAML file
|
9
|
+
- **Multiple authentication types**: Supports OAuth, Basic Authentication, and API Keys
|
10
|
+
- **Automatic documentation**: Generates README with usage examples and detailed API reference
|
11
|
+
- **Error handling**: Integrated HTTP error management with detailed error objects
|
12
|
+
- **Smart object mapping**: Converts JSON responses into Ruby objects with nested attribute access
|
13
|
+
- **Built-in caching**: Optional caching system for authentication tokens
|
14
|
+
- **Elegant interface**: Fluent API inspired by Ruby best practices
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'wrappix'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
$ bundle install
|
28
|
+
```
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
$ gem install wrappix
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
Wrappix can be used in two primary ways:
|
39
|
+
|
40
|
+
1. Generate a standalone API wrapper gem
|
41
|
+
2. Create an API client within an existing project
|
42
|
+
|
43
|
+
### Quick Start
|
44
|
+
|
45
|
+
#### 1. Create a YAML configuration file
|
46
|
+
|
47
|
+
```yaml
|
48
|
+
# github_api.yml
|
49
|
+
api_name: github-api
|
50
|
+
base_url: https://api.github.com
|
51
|
+
auth_type: oauth
|
52
|
+
token_url: https://github.com/login/oauth/access_token
|
53
|
+
|
54
|
+
resources:
|
55
|
+
users:
|
56
|
+
endpoints:
|
57
|
+
- name: get
|
58
|
+
method: get
|
59
|
+
path: users/{username}
|
60
|
+
- name: list
|
61
|
+
method: get
|
62
|
+
path: users
|
63
|
+
params: true
|
64
|
+
|
65
|
+
repos:
|
66
|
+
endpoints:
|
67
|
+
- name: list
|
68
|
+
method: get
|
69
|
+
path: repos
|
70
|
+
params: true
|
71
|
+
- name: get
|
72
|
+
method: get
|
73
|
+
path: repos/{owner}/{repo}
|
74
|
+
- name: create
|
75
|
+
method: post
|
76
|
+
path: user/repos
|
77
|
+
```
|
78
|
+
|
79
|
+
#### 2. Generate the API client
|
80
|
+
|
81
|
+
```bash
|
82
|
+
$ wrappix build github_api.yml
|
83
|
+
```
|
84
|
+
|
85
|
+
#### 3. Use your generated API client
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
require 'github_api' # Note: require with underscore!
|
89
|
+
|
90
|
+
# Configure the client
|
91
|
+
GithubApi.configure do |config|
|
92
|
+
config.client_id = "your_client_id"
|
93
|
+
config.client_secret = "your_client_secret"
|
94
|
+
config.access_token = "your_access_token"
|
95
|
+
end
|
96
|
+
|
97
|
+
# Initialize the client
|
98
|
+
client = GithubApi.client
|
99
|
+
|
100
|
+
# Get a user
|
101
|
+
user = client.users.get("octocat")
|
102
|
+
puts user.name
|
103
|
+
puts user.location
|
104
|
+
puts user.public_repos
|
105
|
+
|
106
|
+
# List repositories with pagination
|
107
|
+
repos = client.repos.list({page: 1, per_page: 10})
|
108
|
+
repos.data.each do |repo|
|
109
|
+
puts "#{repo.name}: #{repo.description}"
|
110
|
+
end
|
111
|
+
|
112
|
+
# Check if there are more pages
|
113
|
+
if repos.next_href
|
114
|
+
puts "More repositories available"
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
## Configuration Reference
|
119
|
+
|
120
|
+
The YAML configuration file supports the following options:
|
121
|
+
|
122
|
+
### Global Options
|
123
|
+
|
124
|
+
| Option | Description | Required | Default |
|
125
|
+
|--------|-------------|----------|---------|
|
126
|
+
| `api_name` | Name of the API wrapper (used for file/module naming) | Yes | |
|
127
|
+
| `base_url` | Base URL of the API | Yes | |
|
128
|
+
| `auth_type` | Authentication type: `oauth`, `basic`, or `api_key` | No | None |
|
129
|
+
| `response_format` | Configuration for response mapping (see below) | No | Auto-detect |
|
130
|
+
|
131
|
+
### Authentication Options
|
132
|
+
|
133
|
+
#### OAuth
|
134
|
+
|
135
|
+
```yaml
|
136
|
+
auth_type: oauth
|
137
|
+
token_url: https://example.com/oauth/token
|
138
|
+
```
|
139
|
+
|
140
|
+
#### Basic Authentication
|
141
|
+
|
142
|
+
```yaml
|
143
|
+
auth_type: basic
|
144
|
+
```
|
145
|
+
|
146
|
+
#### API Key
|
147
|
+
|
148
|
+
```yaml
|
149
|
+
auth_type: api_key
|
150
|
+
api_key_header: X-API-Key # Custom header name (optional)
|
151
|
+
```
|
152
|
+
|
153
|
+
### Resources and Endpoints
|
154
|
+
|
155
|
+
Resources define the API endpoints that will be available in your client:
|
156
|
+
|
157
|
+
```yaml
|
158
|
+
resources:
|
159
|
+
users: # Resource name
|
160
|
+
endpoints:
|
161
|
+
- name: get # Method name in your code
|
162
|
+
method: get # HTTP method (get, post, put, patch, delete)
|
163
|
+
path: users/{id} # Path with parameters in braces
|
164
|
+
params: true # Whether it accepts query parameters (optional)
|
165
|
+
collection: false # Whether it returns a collection (optional)
|
166
|
+
|
167
|
+
- name: list
|
168
|
+
method: get
|
169
|
+
path: users
|
170
|
+
params: true
|
171
|
+
collection: true # Processes response as a collection
|
172
|
+
```
|
173
|
+
|
174
|
+
### Response Format Configuration
|
175
|
+
|
176
|
+
You can configure how responses are mapped to objects:
|
177
|
+
|
178
|
+
```yaml
|
179
|
+
response_format:
|
180
|
+
collection_root: "data" # JSON key containing the collection items
|
181
|
+
item_root: "user" # JSON key containing single items (optional)
|
182
|
+
pagination:
|
183
|
+
next_page_key: "next_href" # JSON key with URL to next page
|
184
|
+
total_count_key: "total" # JSON key with total count
|
185
|
+
limit_key: "limit" # JSON key with page size
|
186
|
+
```
|
187
|
+
|
188
|
+
## Generated Client Architecture
|
189
|
+
|
190
|
+
The generated client follows a clean architecture pattern:
|
191
|
+
|
192
|
+
- `Client`: Main entry point to the API
|
193
|
+
- `Resources`: Classes for each resource (users, repos, etc.)
|
194
|
+
- `Request`: Handles HTTP communication
|
195
|
+
- `Object`: Maps JSON responses to Ruby objects
|
196
|
+
- `Collection`: Handles collections of objects with pagination
|
197
|
+
- `Error`: Standardized error handling
|
198
|
+
- `Configuration`: Global configuration
|
199
|
+
|
200
|
+
## Customization
|
201
|
+
|
202
|
+
After generating your API client, you can further customize it by editing the generated files.
|
203
|
+
|
204
|
+
## Contributing
|
205
|
+
|
206
|
+
Contributions are welcome:
|
207
|
+
|
208
|
+
1. Fork the project
|
209
|
+
2. Create your feature branch (`git checkout -b feature/amazing`)
|
210
|
+
3. Commit your changes (`git commit -am 'Add amazing feature'`)
|
211
|
+
4. Push to the branch (`git push origin feature/amazing`)
|
212
|
+
5. Open a Pull Request
|
213
|
+
|
214
|
+
## License
|
215
|
+
|
216
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/wrappix
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
require "fileutils"
|
5
|
+
require_relative "templates/configuration"
|
6
|
+
require_relative "templates/client"
|
7
|
+
require_relative "templates/request"
|
8
|
+
require_relative "templates/error"
|
9
|
+
require_relative "templates/resource"
|
10
|
+
require_relative "templates/main"
|
11
|
+
require_relative "templates/readme"
|
12
|
+
require_relative "templates/cache"
|
13
|
+
require_relative "templates/collection"
|
14
|
+
require_relative "templates/object"
|
15
|
+
require_relative "templates/documentation"
|
16
|
+
require_relative "templates/tests"
|
17
|
+
|
18
|
+
module Wrappix
|
19
|
+
class Builder
|
20
|
+
attr_reader :file_paths
|
21
|
+
|
22
|
+
def initialize(config_file)
|
23
|
+
raise Errno::ENOENT, "No such file or directory - #{config_file}" unless File.exist?(config_file)
|
24
|
+
|
25
|
+
@config = YAML.load_file(config_file)
|
26
|
+
@api_name = @config["api_name"] || File.basename(Dir.pwd)
|
27
|
+
@normalized_api_name = @api_name.tr("-", "_")
|
28
|
+
@module_name = @api_name.split("-").map(&:capitalize).join
|
29
|
+
@file_paths = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def build
|
33
|
+
process_response_format
|
34
|
+
create_version_file
|
35
|
+
create_base_files
|
36
|
+
create_resource_files
|
37
|
+
create_readme
|
38
|
+
create_documentation
|
39
|
+
create_test_files
|
40
|
+
create_gemspec
|
41
|
+
create_compatibility_file
|
42
|
+
true
|
43
|
+
rescue Psych::SyntaxError => e
|
44
|
+
puts "Error de sintaxis en el archivo YAML: #{e.message}"
|
45
|
+
false
|
46
|
+
rescue StandardError => e
|
47
|
+
puts "Error durante la construcción: #{e.message}"
|
48
|
+
puts e.backtrace
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def create_version_file
|
55
|
+
create_file("lib/#{@normalized_api_name}/version.rb", <<~RUBY)
|
56
|
+
# frozen_string_literal: true
|
57
|
+
|
58
|
+
module #{@module_name}
|
59
|
+
VERSION = "0.1.0"
|
60
|
+
end
|
61
|
+
RUBY
|
62
|
+
end
|
63
|
+
|
64
|
+
def process_response_format
|
65
|
+
# Asegurarse de que existe una configuración global de formato de respuesta
|
66
|
+
@config["response_format"] ||= {
|
67
|
+
"collection_root" => "data",
|
68
|
+
"pagination" => {
|
69
|
+
"next_page_key" => "next_href",
|
70
|
+
"total_count_key" => "total",
|
71
|
+
"limit_key" => "limit"
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
# Para cada recurso, establecer valores predeterminados si no están definidos
|
76
|
+
resources = @config["resources"] || {}
|
77
|
+
resources.each_value do |resource_config|
|
78
|
+
resource_config["response_format"] ||= {}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def create_base_files
|
83
|
+
create_file("lib/#{@normalized_api_name}/configuration.rb",
|
84
|
+
Templates::Configuration.render(@module_name, @config))
|
85
|
+
create_file("lib/#{@normalized_api_name}/client.rb", Templates::Client.render(@module_name, @config))
|
86
|
+
create_file("lib/#{@normalized_api_name}/request.rb", Templates::Request.render(@module_name, @config))
|
87
|
+
create_file("lib/#{@normalized_api_name}/error.rb", Templates::Error.render(@module_name, @config))
|
88
|
+
create_file("lib/#{@normalized_api_name}/cache.rb", Templates::Cache.render(@module_name, @config))
|
89
|
+
create_file("lib/#{@normalized_api_name}/object.rb", Templates::Object.render(@module_name, @config))
|
90
|
+
create_file("lib/#{@normalized_api_name}/collection.rb", Templates::Collection.render(@module_name, @config))
|
91
|
+
update_main_file
|
92
|
+
end
|
93
|
+
|
94
|
+
def create_test_files
|
95
|
+
FileUtils.mkdir_p("test")
|
96
|
+
|
97
|
+
# Crear un archivo test_helper.rb básico
|
98
|
+
create_file("test/test_helper.rb", <<~RUBY)
|
99
|
+
require "minitest/autorun"
|
100
|
+
require "faraday"
|
101
|
+
require "json"
|
102
|
+
|
103
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
104
|
+
require "#{@normalized_api_name}"
|
105
|
+
|
106
|
+
# Configuración global para las pruebas
|
107
|
+
#{@module_name}.configure do |config|
|
108
|
+
config.base_url = "https://api.example.com"
|
109
|
+
# Otras configuraciones necesarias
|
110
|
+
end
|
111
|
+
RUBY
|
112
|
+
|
113
|
+
# Crear el archivo de tests principal con nombre normalizado
|
114
|
+
create_file(
|
115
|
+
"test/#{@normalized_api_name}_test.rb",
|
116
|
+
Templates::Tests.render(@api_name, @module_name, @config)
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
def create_gemspec
|
121
|
+
create_file("#{@api_name}.gemspec", <<~RUBY)
|
122
|
+
# frozen_string_literal: true
|
123
|
+
|
124
|
+
require_relative "lib/#{@normalized_api_name}/version"
|
125
|
+
|
126
|
+
Gem::Specification.new do |spec|
|
127
|
+
spec.name = "#{@api_name}"
|
128
|
+
spec.version = #{@module_name}::VERSION
|
129
|
+
spec.authors = ["Generated by Wrappix"]
|
130
|
+
spec.email = ["info@example.com"]
|
131
|
+
|
132
|
+
spec.summary = "API wrapper for #{@api_name}"
|
133
|
+
spec.description = "A Ruby client for the #{@api_name} API"
|
134
|
+
spec.homepage = "https://github.com/example/#{@api_name}"
|
135
|
+
spec.license = "MIT"
|
136
|
+
spec.required_ruby_version = ">= 2.6.0"
|
137
|
+
|
138
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
139
|
+
|
140
|
+
# Specify which files should be added to the gem when it is released.
|
141
|
+
spec.files = Dir["lib/**/*", "LICENSE.txt", "README.md"]
|
142
|
+
|
143
|
+
spec.add_dependency "faraday", "~> 2.0"
|
144
|
+
spec.add_dependency "faraday_middleware", "~> 1.2"
|
145
|
+
spec.add_dependency "ostruct", "~> 0.5"
|
146
|
+
|
147
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
148
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
149
|
+
end
|
150
|
+
RUBY
|
151
|
+
end
|
152
|
+
|
153
|
+
def create_resource_files
|
154
|
+
FileUtils.mkdir_p("lib/#{@normalized_api_name}/resources")
|
155
|
+
|
156
|
+
resources = @config["resources"] || {}
|
157
|
+
resources.each do |resource_name, resource_config|
|
158
|
+
create_file(
|
159
|
+
"lib/#{@normalized_api_name}/resources/#{resource_name}.rb",
|
160
|
+
Templates::Resource.render(@module_name, resource_name, resource_config)
|
161
|
+
)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def update_main_file
|
166
|
+
main_file = "lib/#{@normalized_api_name}.rb"
|
167
|
+
content = Templates::Main.render(@normalized_api_name, @module_name, @config)
|
168
|
+
create_file(main_file, content)
|
169
|
+
end
|
170
|
+
|
171
|
+
def create_compatibility_file
|
172
|
+
# Crear un archivo de compatibilidad con el nombre original (si es diferente)
|
173
|
+
return unless @api_name != @normalized_api_name
|
174
|
+
|
175
|
+
compat_file = "lib/#{@api_name}.rb"
|
176
|
+
content = <<~RUBY
|
177
|
+
# frozen_string_literal: true
|
178
|
+
|
179
|
+
# Archivo de compatibilidad - redirige al archivo normalizado
|
180
|
+
require_relative "#{@normalized_api_name}"
|
181
|
+
RUBY
|
182
|
+
create_file(compat_file, content)
|
183
|
+
end
|
184
|
+
|
185
|
+
def create_file(path, content)
|
186
|
+
FileUtils.mkdir_p(File.dirname(path))
|
187
|
+
|
188
|
+
File.open(path, "w") do |file|
|
189
|
+
file.write(content)
|
190
|
+
end
|
191
|
+
|
192
|
+
@file_paths[path] = true
|
193
|
+
puts "File created: #{path}"
|
194
|
+
|
195
|
+
true
|
196
|
+
end
|
197
|
+
|
198
|
+
def create_readme
|
199
|
+
create_file("README.md", Templates::Readme.render(@api_name, @module_name, @config))
|
200
|
+
end
|
201
|
+
|
202
|
+
def create_documentation
|
203
|
+
FileUtils.mkdir_p("docs")
|
204
|
+
create_file("docs/api.md", Templates::Documentation.render(@api_name, @module_name, @config))
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
data/lib/wrappix/cli.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../wrappix"
|
4
|
+
require "thor"
|
5
|
+
|
6
|
+
module Wrappix
|
7
|
+
class CLI < Thor
|
8
|
+
desc "build CONFIG_FILE", "Genera archivos del wrapper basados en CONFIG_FILE"
|
9
|
+
def build(config_file)
|
10
|
+
unless File.exist?(config_file)
|
11
|
+
say "Error: El archivo de configuración no existe", :red
|
12
|
+
exit(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
if Wrappix.build(config_file)
|
16
|
+
say "Wrapper generado correctamente", :green
|
17
|
+
true
|
18
|
+
else
|
19
|
+
say "Error al generar el wrapper", :red
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "faraday_middleware"
|
5
|
+
|
6
|
+
module Wrappix
|
7
|
+
class Client
|
8
|
+
def initialize(config = Wrappix.configuration)
|
9
|
+
@config = config
|
10
|
+
@connection = build_connection
|
11
|
+
setup_resources
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def build_connection
|
17
|
+
Faraday.new(url: @config.base_url) do |conn|
|
18
|
+
conn.headers = @config.headers
|
19
|
+
conn.request :json
|
20
|
+
conn.response :json, content_type: /\bjson$/
|
21
|
+
conn.adapter Faraday.default_adapter
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup_resources
|
26
|
+
@config.resources.each do |name, options|
|
27
|
+
define_resource_method(name, options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_resource_method(name, options)
|
32
|
+
resource = Resource.new(self, name, options)
|
33
|
+
|
34
|
+
self.class.define_method(name) do
|
35
|
+
resource
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|