node-red 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/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +10 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +159 -0
- data/Rakefile +12 -0
- data/examples/basic_usage.rb +77 -0
- data/examples/flow_management.rb +137 -0
- data/examples/node_management.rb +104 -0
- data/lib/node/red/client.rb +320 -0
- data/lib/node/red/errors.rb +17 -0
- data/lib/node/red/version.rb +7 -0
- data/lib/node/red.rb +11 -0
- data/sig/node/red.rbs +6 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2a448f70ca26bba12cb8a9d4f6a25f93b2956e3144c70f2b215a5b0d1efa320f
|
|
4
|
+
data.tar.gz: abd51032ec4f34f15f1ef1e5b00dea2c71a7bc4135b519f1838145bb00be42cb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1421fecb786a1a74a5db7f094717a28070468a904ad8b67f43c97250c928424a831d76a30fb340f11c369dabb631f55cc0f129edaf9d9cd62604337bbb6ec92b
|
|
7
|
+
data.tar.gz: 9dcd66078d904b340be8f97faae4730112fc8f41eecf5b975e74efffd1af96033a79bc1efce9be944519724c144841d02308403d9561b198971ed27050d55bac
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
## [0.1.0] - 2025-10-28
|
|
2
|
+
|
|
3
|
+
- Initial release
|
|
4
|
+
- Full implementation of Node-RED Admin HTTP API wrapper
|
|
5
|
+
- Authentication endpoints (login, token exchange, revoke)
|
|
6
|
+
- Settings and diagnostics endpoints
|
|
7
|
+
- Complete flow management (CRUD operations, state management)
|
|
8
|
+
- Complete node management (install, update, delete modules and sets)
|
|
9
|
+
- Custom error classes for better error handling
|
|
10
|
+
- Comprehensive documentation and examples
|
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 Davide Santangelo
|
|
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,159 @@
|
|
|
1
|
+
# Node::Red
|
|
2
|
+
|
|
3
|
+
A Ruby client library for the Node-RED Admin HTTP API. This gem provides a comprehensive wrapper around the Node-RED Admin API, allowing you to programmatically manage flows, nodes, settings, and authentication.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add node-red
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install node-red
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Basic Setup
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require 'node/red'
|
|
25
|
+
|
|
26
|
+
# Create a client instance
|
|
27
|
+
client = Node::Red::Client.new('http://localhost:1880')
|
|
28
|
+
|
|
29
|
+
# Or with authentication
|
|
30
|
+
client = Node::Red::Client.new('http://localhost:1880', access_token: 'your-access-token')
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Authentication
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
# Get authentication scheme
|
|
37
|
+
auth_info = client.auth_login
|
|
38
|
+
|
|
39
|
+
# Exchange credentials for access token
|
|
40
|
+
token_response = client.auth_token(
|
|
41
|
+
client_id: 'node-red-admin',
|
|
42
|
+
grant_type: 'password',
|
|
43
|
+
scope: '*',
|
|
44
|
+
username: 'admin',
|
|
45
|
+
password: 'password'
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# The access token is automatically set on the client
|
|
49
|
+
puts client.access_token
|
|
50
|
+
|
|
51
|
+
# Revoke a token
|
|
52
|
+
client.auth_revoke(token_response['access_token'])
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Settings & Diagnostics
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
# Get runtime settings
|
|
59
|
+
settings = client.settings
|
|
60
|
+
puts settings['httpNodeRoot']
|
|
61
|
+
|
|
62
|
+
# Get runtime diagnostics
|
|
63
|
+
diagnostics = client.diagnostics
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Flow Management
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
# Get all flows
|
|
70
|
+
flows = client.flows
|
|
71
|
+
|
|
72
|
+
# Deploy new flows
|
|
73
|
+
new_flows = [
|
|
74
|
+
{ id: 'tab1', type: 'tab', label: 'Flow 1' },
|
|
75
|
+
{ id: 'node1', type: 'inject', z: 'tab1', name: 'test' }
|
|
76
|
+
]
|
|
77
|
+
response = client.deploy_flows(new_flows)
|
|
78
|
+
|
|
79
|
+
# Get flow runtime state
|
|
80
|
+
state = client.flows_state
|
|
81
|
+
|
|
82
|
+
# Set flow runtime state
|
|
83
|
+
client.set_flows_state('stop') # or 'start'
|
|
84
|
+
|
|
85
|
+
# Add a single flow
|
|
86
|
+
new_flow = { label: 'New Flow', type: 'tab' }
|
|
87
|
+
created_flow = client.add_flow(new_flow)
|
|
88
|
+
|
|
89
|
+
# Get a specific flow
|
|
90
|
+
flow = client.flow('flow-id')
|
|
91
|
+
|
|
92
|
+
# Update a flow
|
|
93
|
+
updated_flow = client.update_flow('flow-id', { label: 'Updated Flow', type: 'tab' })
|
|
94
|
+
|
|
95
|
+
# Delete a flow
|
|
96
|
+
client.delete_flow('flow-id')
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Node Management
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
# Get all installed nodes
|
|
103
|
+
nodes = client.nodes
|
|
104
|
+
|
|
105
|
+
# Install a new node module
|
|
106
|
+
client.install_node('node-red-contrib-example')
|
|
107
|
+
|
|
108
|
+
# Get node module information
|
|
109
|
+
module_info = client.node_module('node-red-contrib-example')
|
|
110
|
+
|
|
111
|
+
# Enable/disable a node module
|
|
112
|
+
client.update_node_module('node-red-contrib-example', enabled: false)
|
|
113
|
+
client.update_node_module('node-red-contrib-example', enabled: true)
|
|
114
|
+
|
|
115
|
+
# Remove a node module
|
|
116
|
+
client.delete_node_module('node-red-contrib-example')
|
|
117
|
+
|
|
118
|
+
# Get node set information
|
|
119
|
+
set_info = client.node_set('node-red', 'core')
|
|
120
|
+
|
|
121
|
+
# Enable/disable a node set
|
|
122
|
+
client.update_node_set('node-red', 'core', enabled: true)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Error Handling
|
|
126
|
+
|
|
127
|
+
The library provides specific error classes for different error scenarios:
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
begin
|
|
131
|
+
client.flow('non-existent-id')
|
|
132
|
+
rescue Node::Red::NotFoundError => e
|
|
133
|
+
puts "Flow not found: #{e.message}"
|
|
134
|
+
rescue Node::Red::AuthenticationError => e
|
|
135
|
+
puts "Authentication failed: #{e.message}"
|
|
136
|
+
rescue Node::Red::ServerError => e
|
|
137
|
+
puts "Server error: #{e.message}"
|
|
138
|
+
rescue Node::Red::ApiError => e
|
|
139
|
+
puts "API error: #{e.message}"
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
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.
|
|
146
|
+
|
|
147
|
+
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).
|
|
148
|
+
|
|
149
|
+
## Contributing
|
|
150
|
+
|
|
151
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davidesantangelo/node-red. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/davidesantangelo/node-red/blob/master/CODE_OF_CONDUCT.md).
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
156
|
+
|
|
157
|
+
## Code of Conduct
|
|
158
|
+
|
|
159
|
+
Everyone interacting in the Node::Red project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/davidesantangelo/node-red/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Example usage of the Node-RED Admin API Ruby client
|
|
4
|
+
#
|
|
5
|
+
# Make sure you have a Node-RED instance running at http://localhost:1880
|
|
6
|
+
# before running this example.
|
|
7
|
+
|
|
8
|
+
require "node/red"
|
|
9
|
+
|
|
10
|
+
# Initialize the client
|
|
11
|
+
client = Node::Red::Client.new("http://localhost:1880")
|
|
12
|
+
|
|
13
|
+
begin
|
|
14
|
+
# Example 1: Get runtime settings
|
|
15
|
+
puts "=== Runtime Settings ==="
|
|
16
|
+
settings = client.settings
|
|
17
|
+
puts "HTTP Node Root: #{settings["httpNodeRoot"]}"
|
|
18
|
+
puts "Version: #{settings["version"]}"
|
|
19
|
+
puts
|
|
20
|
+
|
|
21
|
+
# Example 2: Get all flows
|
|
22
|
+
puts "=== Current Flows ==="
|
|
23
|
+
flows = client.flows
|
|
24
|
+
puts "Total flows: #{flows.length}"
|
|
25
|
+
flows.each do |flow|
|
|
26
|
+
puts " - Flow: #{flow["label"]} (#{flow["id"]})" if flow["type"] == "tab"
|
|
27
|
+
end
|
|
28
|
+
puts
|
|
29
|
+
|
|
30
|
+
# Example 3: Get all installed nodes
|
|
31
|
+
puts "=== Installed Nodes ==="
|
|
32
|
+
nodes = client.nodes
|
|
33
|
+
puts "Total node modules: #{nodes.length}"
|
|
34
|
+
nodes.take(5).each do |node|
|
|
35
|
+
puts " - #{node["id"]} (v#{node["version"]})"
|
|
36
|
+
end
|
|
37
|
+
puts
|
|
38
|
+
|
|
39
|
+
# Example 4: Get flow runtime state
|
|
40
|
+
puts "=== Flow Runtime State ==="
|
|
41
|
+
state = client.flows_state
|
|
42
|
+
puts "State: #{state["state"]}"
|
|
43
|
+
puts
|
|
44
|
+
|
|
45
|
+
# Example 5: Get diagnostics (if available)
|
|
46
|
+
puts "=== Diagnostics ==="
|
|
47
|
+
begin
|
|
48
|
+
diagnostics = client.diagnostics
|
|
49
|
+
puts "Node.js version: #{diagnostics["nodejs"]}"
|
|
50
|
+
puts "OS: #{diagnostics["os"]}"
|
|
51
|
+
rescue Node::Red::NotFoundError
|
|
52
|
+
puts "Diagnostics endpoint not available (requires Node-RED 1.1.0+)"
|
|
53
|
+
end
|
|
54
|
+
puts
|
|
55
|
+
|
|
56
|
+
# Example with authentication (uncomment to use)
|
|
57
|
+
# puts "=== Authentication Example ==="
|
|
58
|
+
# token_response = client.auth_token(
|
|
59
|
+
# client_id: "node-red-admin",
|
|
60
|
+
# grant_type: "password",
|
|
61
|
+
# scope: "*",
|
|
62
|
+
# username: "admin",
|
|
63
|
+
# password: "password"
|
|
64
|
+
# )
|
|
65
|
+
# puts "Access token obtained: #{token_response['access_token'][0..20]}..."
|
|
66
|
+
rescue Node::Red::AuthenticationError => e
|
|
67
|
+
puts "Authentication required: #{e.message}"
|
|
68
|
+
rescue Node::Red::NotFoundError => e
|
|
69
|
+
puts "Resource not found: #{e.message}"
|
|
70
|
+
rescue Node::Red::ServerError => e
|
|
71
|
+
puts "Server error: #{e.message}"
|
|
72
|
+
rescue Node::Red::ApiError => e
|
|
73
|
+
puts "API error: #{e.message}"
|
|
74
|
+
rescue Errno::ECONNREFUSED
|
|
75
|
+
puts "Error: Could not connect to Node-RED at http://localhost:1880"
|
|
76
|
+
puts "Make sure Node-RED is running before executing this example."
|
|
77
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Example: Managing flows with Node-RED Admin API
|
|
4
|
+
#
|
|
5
|
+
# This example demonstrates creating, updating, and deleting flows
|
|
6
|
+
|
|
7
|
+
require "node/red"
|
|
8
|
+
|
|
9
|
+
# Initialize the client
|
|
10
|
+
client = Node::Red::Client.new("http://localhost:1880")
|
|
11
|
+
|
|
12
|
+
begin
|
|
13
|
+
# Get current flows
|
|
14
|
+
puts "=== Getting Current Flows ==="
|
|
15
|
+
current_flows = client.flows
|
|
16
|
+
puts "Current number of flows: #{current_flows.length}"
|
|
17
|
+
puts
|
|
18
|
+
|
|
19
|
+
# Add a new flow
|
|
20
|
+
puts "=== Adding a New Flow ==="
|
|
21
|
+
new_flow = {
|
|
22
|
+
label: "Example Flow",
|
|
23
|
+
type: "tab",
|
|
24
|
+
disabled: false,
|
|
25
|
+
info: "This is an example flow created via the API"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
created_flow = client.add_flow(new_flow)
|
|
29
|
+
flow_id = created_flow["id"]
|
|
30
|
+
puts "Created flow with ID: #{flow_id}"
|
|
31
|
+
puts "Flow label: #{created_flow["label"]}"
|
|
32
|
+
puts
|
|
33
|
+
|
|
34
|
+
# Get the specific flow
|
|
35
|
+
puts "=== Getting Specific Flow ==="
|
|
36
|
+
flow_details = client.flow(flow_id)
|
|
37
|
+
puts "Flow: #{flow_details["label"]}"
|
|
38
|
+
puts "Type: #{flow_details["type"]}"
|
|
39
|
+
puts
|
|
40
|
+
|
|
41
|
+
# Update the flow
|
|
42
|
+
puts "=== Updating Flow ==="
|
|
43
|
+
updated_flow = {
|
|
44
|
+
id: flow_id,
|
|
45
|
+
label: "Updated Example Flow",
|
|
46
|
+
type: "tab",
|
|
47
|
+
disabled: false,
|
|
48
|
+
info: "This flow has been updated"
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
result = client.update_flow(flow_id, updated_flow)
|
|
52
|
+
puts "Flow updated: #{result["label"]}"
|
|
53
|
+
puts
|
|
54
|
+
|
|
55
|
+
# Example: Deploy flows with nodes
|
|
56
|
+
puts "=== Deploying Complete Flow Configuration ==="
|
|
57
|
+
|
|
58
|
+
# Create a simple flow with an inject and debug node
|
|
59
|
+
complete_flows = [
|
|
60
|
+
{
|
|
61
|
+
id: "flow1",
|
|
62
|
+
type: "tab",
|
|
63
|
+
label: "Test Flow"
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: "inject1",
|
|
67
|
+
type: "inject",
|
|
68
|
+
z: "flow1",
|
|
69
|
+
name: "Test Inject",
|
|
70
|
+
props: [{ p: "payload" }],
|
|
71
|
+
repeat: "",
|
|
72
|
+
crontab: "",
|
|
73
|
+
once: false,
|
|
74
|
+
onceDelay: 0.1,
|
|
75
|
+
topic: "",
|
|
76
|
+
payload: "Hello from Ruby!",
|
|
77
|
+
payloadType: "str",
|
|
78
|
+
x: 150,
|
|
79
|
+
y: 100
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
id: "debug1",
|
|
83
|
+
type: "debug",
|
|
84
|
+
z: "flow1",
|
|
85
|
+
name: "Test Debug",
|
|
86
|
+
active: true,
|
|
87
|
+
tosidebar: true,
|
|
88
|
+
console: false,
|
|
89
|
+
tostatus: false,
|
|
90
|
+
complete: "payload",
|
|
91
|
+
targetType: "msg",
|
|
92
|
+
x: 350,
|
|
93
|
+
y: 100,
|
|
94
|
+
wires: []
|
|
95
|
+
}
|
|
96
|
+
]
|
|
97
|
+
|
|
98
|
+
# NOTE: Be careful with deploy_flows as it replaces ALL flows
|
|
99
|
+
# Uncomment the following lines to deploy
|
|
100
|
+
# deployment = client.deploy_flows(complete_flows)
|
|
101
|
+
# puts "Flows deployed with revision: #{deployment['rev']}"
|
|
102
|
+
puts "Flow deployment commented out for safety"
|
|
103
|
+
puts
|
|
104
|
+
|
|
105
|
+
# Check flow state
|
|
106
|
+
puts "=== Flow Runtime State ==="
|
|
107
|
+
state = client.flows_state
|
|
108
|
+
puts "Current state: #{state["state"]}"
|
|
109
|
+
puts
|
|
110
|
+
|
|
111
|
+
# Stop flows (uncomment to use)
|
|
112
|
+
# puts "=== Stopping Flows ==="
|
|
113
|
+
# client.set_flows_state('stop')
|
|
114
|
+
# puts "Flows stopped"
|
|
115
|
+
#
|
|
116
|
+
# sleep 2
|
|
117
|
+
#
|
|
118
|
+
# puts "=== Starting Flows ==="
|
|
119
|
+
# client.set_flows_state('start')
|
|
120
|
+
# puts "Flows started"
|
|
121
|
+
# puts
|
|
122
|
+
|
|
123
|
+
# Clean up - delete the flow we created
|
|
124
|
+
puts "=== Deleting Flow ==="
|
|
125
|
+
client.delete_flow(flow_id)
|
|
126
|
+
puts "Flow #{flow_id} deleted"
|
|
127
|
+
puts
|
|
128
|
+
|
|
129
|
+
puts "Example completed successfully!"
|
|
130
|
+
rescue Node::Red::NotFoundError => e
|
|
131
|
+
puts "Error: #{e.message}"
|
|
132
|
+
rescue Node::Red::ApiError => e
|
|
133
|
+
puts "API Error: #{e.message}"
|
|
134
|
+
rescue Errno::ECONNREFUSED
|
|
135
|
+
puts "Error: Could not connect to Node-RED at http://localhost:1880"
|
|
136
|
+
puts "Make sure Node-RED is running before executing this example."
|
|
137
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Example: Managing nodes with Node-RED Admin API
|
|
4
|
+
#
|
|
5
|
+
# This example demonstrates installing, updating, and removing node modules
|
|
6
|
+
|
|
7
|
+
require "node/red"
|
|
8
|
+
|
|
9
|
+
# Initialize the client
|
|
10
|
+
client = Node::Red::Client.new("http://localhost:1880")
|
|
11
|
+
|
|
12
|
+
begin
|
|
13
|
+
# List all installed nodes
|
|
14
|
+
puts "=== Installed Node Modules ==="
|
|
15
|
+
nodes = client.nodes
|
|
16
|
+
|
|
17
|
+
# Group by module
|
|
18
|
+
modules = nodes.group_by { |n| n["module"] }.keys.compact
|
|
19
|
+
puts "Total modules installed: #{modules.length}"
|
|
20
|
+
puts
|
|
21
|
+
|
|
22
|
+
# Show first 10 modules
|
|
23
|
+
puts "Sample of installed modules:"
|
|
24
|
+
modules.take(10).each do |module_name|
|
|
25
|
+
puts " - #{module_name}"
|
|
26
|
+
end
|
|
27
|
+
puts
|
|
28
|
+
|
|
29
|
+
# Get information about a specific module
|
|
30
|
+
puts "=== Node-RED Core Module Info ==="
|
|
31
|
+
begin
|
|
32
|
+
core_module = client.node_module("node-red")
|
|
33
|
+
puts "Module: #{core_module["name"]}"
|
|
34
|
+
puts "Version: #{core_module["version"]}"
|
|
35
|
+
puts "Nodes in module: #{core_module["nodes"]&.length || 0}"
|
|
36
|
+
rescue Node::Red::NotFoundError
|
|
37
|
+
puts "Core module information not available"
|
|
38
|
+
end
|
|
39
|
+
puts
|
|
40
|
+
|
|
41
|
+
# Install a node module (commented out for safety)
|
|
42
|
+
puts "=== Installing a Node Module ==="
|
|
43
|
+
puts "Example (commented out for safety):"
|
|
44
|
+
puts " client.install_node('node-red-contrib-example')"
|
|
45
|
+
puts
|
|
46
|
+
# Uncomment to actually install:
|
|
47
|
+
# begin
|
|
48
|
+
# result = client.install_node('node-red-contrib-example')
|
|
49
|
+
# puts "Module installed: #{result['name']}"
|
|
50
|
+
# rescue Node::Red::ApiError => e
|
|
51
|
+
# puts "Installation failed: #{e.message}"
|
|
52
|
+
# end
|
|
53
|
+
|
|
54
|
+
# Get node set information
|
|
55
|
+
puts "=== Node Set Information ==="
|
|
56
|
+
begin
|
|
57
|
+
# Example: Get info about the inject node set
|
|
58
|
+
node_set = client.node_set("node-red", "common")
|
|
59
|
+
puts "Node set: #{node_set["id"]}"
|
|
60
|
+
puts "Enabled: #{node_set["enabled"]}"
|
|
61
|
+
puts "Types: #{node_set["types"]&.join(", ")}"
|
|
62
|
+
rescue Node::Red::NotFoundError
|
|
63
|
+
puts "Node set not found"
|
|
64
|
+
end
|
|
65
|
+
puts
|
|
66
|
+
|
|
67
|
+
# Enable/Disable a node module (commented out for safety)
|
|
68
|
+
puts "=== Enable/Disable Node Module ==="
|
|
69
|
+
puts "Example (commented out for safety):"
|
|
70
|
+
puts " # Disable a module"
|
|
71
|
+
puts " client.update_node_module('node-red-contrib-example', enabled: false)"
|
|
72
|
+
puts
|
|
73
|
+
puts " # Enable a module"
|
|
74
|
+
puts " client.update_node_module('node-red-contrib-example', enabled: true)"
|
|
75
|
+
puts
|
|
76
|
+
|
|
77
|
+
# Enable/Disable a node set (commented out for safety)
|
|
78
|
+
puts "=== Enable/Disable Node Set ==="
|
|
79
|
+
puts "Example (commented out for safety):"
|
|
80
|
+
puts " # Disable a node set"
|
|
81
|
+
puts " client.update_node_set('node-red', 'common', enabled: false)"
|
|
82
|
+
puts
|
|
83
|
+
puts " # Enable a node set"
|
|
84
|
+
puts " client.update_node_set('node-red', 'common', enabled: true)"
|
|
85
|
+
puts
|
|
86
|
+
|
|
87
|
+
# Remove a node module (commented out for safety)
|
|
88
|
+
puts "=== Removing a Node Module ==="
|
|
89
|
+
puts "Example (commented out for safety):"
|
|
90
|
+
puts " client.delete_node_module('node-red-contrib-example')"
|
|
91
|
+
puts
|
|
92
|
+
|
|
93
|
+
puts "Example completed successfully!"
|
|
94
|
+
puts
|
|
95
|
+
puts "Note: Dangerous operations (install/delete/enable/disable) are commented out"
|
|
96
|
+
puts "for safety. Uncomment them in the source code to test these features."
|
|
97
|
+
rescue Node::Red::AuthenticationError => e
|
|
98
|
+
puts "Authentication required: #{e.message}"
|
|
99
|
+
rescue Node::Red::ApiError => e
|
|
100
|
+
puts "API Error: #{e.message}"
|
|
101
|
+
rescue Errno::ECONNREFUSED
|
|
102
|
+
puts "Error: Could not connect to Node-RED at http://localhost:1880"
|
|
103
|
+
puts "Make sure Node-RED is running before executing this example."
|
|
104
|
+
end
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Node
|
|
8
|
+
module Red
|
|
9
|
+
class Client
|
|
10
|
+
attr_reader :base_url, :access_token
|
|
11
|
+
|
|
12
|
+
# Initialize a new Node-RED Admin API client
|
|
13
|
+
#
|
|
14
|
+
# @param base_url [String] The base URL of the Node-RED instance (e.g., "http://localhost:1880")
|
|
15
|
+
# @param access_token [String, nil] Optional access token for authentication
|
|
16
|
+
def initialize(base_url, access_token: nil)
|
|
17
|
+
@base_url = base_url.chomp("/")
|
|
18
|
+
@access_token = access_token
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# ==================== Authentication Methods ====================
|
|
22
|
+
|
|
23
|
+
# Get the active authentication scheme
|
|
24
|
+
#
|
|
25
|
+
# @return [Hash] Authentication scheme information
|
|
26
|
+
def auth_login
|
|
27
|
+
get("/auth/login")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Exchange credentials for access token
|
|
31
|
+
#
|
|
32
|
+
# @param client_id [String] The client ID
|
|
33
|
+
# @param grant_type [String] The grant type (e.g., "password")
|
|
34
|
+
# @param scope [String] The scope (e.g., "*" for full access)
|
|
35
|
+
# @param username [String] The username
|
|
36
|
+
# @param password [String] The password
|
|
37
|
+
# @return [Hash] Token response with access_token
|
|
38
|
+
def auth_token(client_id:, grant_type:, scope:, username:, password:)
|
|
39
|
+
body = {
|
|
40
|
+
client_id: client_id,
|
|
41
|
+
grant_type: grant_type,
|
|
42
|
+
scope: scope,
|
|
43
|
+
username: username,
|
|
44
|
+
password: password
|
|
45
|
+
}
|
|
46
|
+
response = post("/auth/token", body, use_auth: false)
|
|
47
|
+
@access_token = response["access_token"] if response["access_token"]
|
|
48
|
+
response
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Revoke an access token
|
|
52
|
+
#
|
|
53
|
+
# @param token [String] The token to revoke
|
|
54
|
+
# @return [Hash] Revocation response
|
|
55
|
+
def auth_revoke(token)
|
|
56
|
+
body = { token: token }
|
|
57
|
+
post("/auth/revoke", body)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# ==================== Settings & Diagnostics ====================
|
|
61
|
+
|
|
62
|
+
# Get the runtime settings
|
|
63
|
+
#
|
|
64
|
+
# @return [Hash] Runtime settings
|
|
65
|
+
def settings
|
|
66
|
+
get("/settings")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Get the runtime diagnostics
|
|
70
|
+
#
|
|
71
|
+
# @return [Hash] Runtime diagnostics
|
|
72
|
+
def diagnostics
|
|
73
|
+
get("/diagnostics")
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# ==================== Flow Management ====================
|
|
77
|
+
|
|
78
|
+
# Get the active flow configuration
|
|
79
|
+
#
|
|
80
|
+
# @return [Array<Hash>] Array of flow configurations
|
|
81
|
+
def flows
|
|
82
|
+
get("/flows")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Get the active flow's runtime state
|
|
86
|
+
#
|
|
87
|
+
# @return [Hash] Flow runtime state
|
|
88
|
+
def flows_state
|
|
89
|
+
get("/flows/state")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Set the active flow configuration
|
|
93
|
+
#
|
|
94
|
+
# @param flows [Array<Hash>] The flow configuration
|
|
95
|
+
# @param rev [String, nil] Optional revision identifier
|
|
96
|
+
# @return [Hash] Response with revision ID
|
|
97
|
+
def deploy_flows(flows, rev: nil)
|
|
98
|
+
body = { flows: flows }
|
|
99
|
+
body[:rev] = rev if rev
|
|
100
|
+
post("/flows", body)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Set the active flow's runtime state
|
|
104
|
+
#
|
|
105
|
+
# @param state [String] The desired state ("start" or "stop")
|
|
106
|
+
# @return [Hash] State change response
|
|
107
|
+
def set_flows_state(state)
|
|
108
|
+
body = { state: state }
|
|
109
|
+
post("/flows/state", body)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Add a flow to the active configuration
|
|
113
|
+
#
|
|
114
|
+
# @param flow [Hash] The flow configuration
|
|
115
|
+
# @return [Hash] The created flow with ID
|
|
116
|
+
def add_flow(flow)
|
|
117
|
+
post("/flow", flow)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Get an individual flow configuration
|
|
121
|
+
#
|
|
122
|
+
# @param id [String] The flow ID
|
|
123
|
+
# @return [Hash] Flow configuration
|
|
124
|
+
def flow(id)
|
|
125
|
+
get("/flow/#{id}")
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Update an individual flow configuration
|
|
129
|
+
#
|
|
130
|
+
# @param id [String] The flow ID
|
|
131
|
+
# @param flow [Hash] The updated flow configuration
|
|
132
|
+
# @return [Hash] The updated flow
|
|
133
|
+
def update_flow(id, flow)
|
|
134
|
+
put("/flow/#{id}", flow)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Delete an individual flow configuration
|
|
138
|
+
#
|
|
139
|
+
# @param id [String] The flow ID
|
|
140
|
+
# @return [Hash] Deletion response
|
|
141
|
+
def delete_flow(id)
|
|
142
|
+
delete("/flow/#{id}")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# ==================== Node Management ====================
|
|
146
|
+
|
|
147
|
+
# Get a list of the installed nodes
|
|
148
|
+
#
|
|
149
|
+
# @return [Array<Hash>] List of installed nodes
|
|
150
|
+
def nodes
|
|
151
|
+
get("/nodes")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Install a new node module
|
|
155
|
+
#
|
|
156
|
+
# @param module_name [String] The name of the node module to install
|
|
157
|
+
# @return [Hash] Installation response
|
|
158
|
+
def install_node(module_name)
|
|
159
|
+
body = { module: module_name }
|
|
160
|
+
post("/nodes", body)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Get a node module's information
|
|
164
|
+
#
|
|
165
|
+
# @param module_name [String] The module name
|
|
166
|
+
# @return [Hash] Module information
|
|
167
|
+
def node_module(module_name)
|
|
168
|
+
get("/nodes/#{module_name}")
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Enable or disable a node module
|
|
172
|
+
#
|
|
173
|
+
# @param module_name [String] The module name
|
|
174
|
+
# @param enabled [Boolean] Whether to enable or disable the module
|
|
175
|
+
# @return [Hash] Update response
|
|
176
|
+
def update_node_module(module_name, enabled:)
|
|
177
|
+
body = { enabled: enabled }
|
|
178
|
+
put("/nodes/#{module_name}", body)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Remove a node module
|
|
182
|
+
#
|
|
183
|
+
# @param module_name [String] The module name
|
|
184
|
+
# @return [Hash] Deletion response
|
|
185
|
+
def delete_node_module(module_name)
|
|
186
|
+
delete("/nodes/#{module_name}")
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Get a node module set information
|
|
190
|
+
#
|
|
191
|
+
# @param module_name [String] The module name
|
|
192
|
+
# @param set_name [String] The set name
|
|
193
|
+
# @return [Hash] Node set information
|
|
194
|
+
def node_set(module_name, set_name)
|
|
195
|
+
get("/nodes/#{module_name}/#{set_name}")
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Enable or disable a node set
|
|
199
|
+
#
|
|
200
|
+
# @param module_name [String] The module name
|
|
201
|
+
# @param set_name [String] The set name
|
|
202
|
+
# @param enabled [Boolean] Whether to enable or disable the set
|
|
203
|
+
# @return [Hash] Update response
|
|
204
|
+
def update_node_set(module_name, set_name, enabled:)
|
|
205
|
+
body = { enabled: enabled }
|
|
206
|
+
put("/nodes/#{module_name}/#{set_name}", body)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
private
|
|
210
|
+
|
|
211
|
+
# Perform a GET request
|
|
212
|
+
#
|
|
213
|
+
# @param path [String] The API path
|
|
214
|
+
# @return [Hash, Array] Parsed JSON response
|
|
215
|
+
def get(path)
|
|
216
|
+
request(:get, path)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Perform a POST request
|
|
220
|
+
#
|
|
221
|
+
# @param path [String] The API path
|
|
222
|
+
# @param body [Hash] The request body
|
|
223
|
+
# @param use_auth [Boolean] Whether to use authentication
|
|
224
|
+
# @return [Hash] Parsed JSON response
|
|
225
|
+
def post(path, body, use_auth: true)
|
|
226
|
+
request(:post, path, body: body, use_auth: use_auth)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Perform a PUT request
|
|
230
|
+
#
|
|
231
|
+
# @param path [String] The API path
|
|
232
|
+
# @param body [Hash] The request body
|
|
233
|
+
# @return [Hash] Parsed JSON response
|
|
234
|
+
def put(path, body)
|
|
235
|
+
request(:put, path, body: body)
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Perform a DELETE request
|
|
239
|
+
#
|
|
240
|
+
# @param path [String] The API path
|
|
241
|
+
# @return [Hash] Parsed JSON response
|
|
242
|
+
def delete(path)
|
|
243
|
+
request(:delete, path)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# Perform an HTTP request
|
|
247
|
+
#
|
|
248
|
+
# @param method [Symbol] HTTP method (:get, :post, :put, :delete)
|
|
249
|
+
# @param path [String] The API path
|
|
250
|
+
# @param body [Hash, nil] Optional request body
|
|
251
|
+
# @param use_auth [Boolean] Whether to use authentication
|
|
252
|
+
# @return [Hash, Array] Parsed JSON response
|
|
253
|
+
def request(method, path, body: nil, use_auth: true)
|
|
254
|
+
uri = URI.join(@base_url, path)
|
|
255
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
256
|
+
http.use_ssl = uri.scheme == "https"
|
|
257
|
+
|
|
258
|
+
request = case method
|
|
259
|
+
when :get
|
|
260
|
+
Net::HTTP::Get.new(uri)
|
|
261
|
+
when :post
|
|
262
|
+
Net::HTTP::Post.new(uri)
|
|
263
|
+
when :put
|
|
264
|
+
Net::HTTP::Put.new(uri)
|
|
265
|
+
when :delete
|
|
266
|
+
Net::HTTP::Delete.new(uri)
|
|
267
|
+
else
|
|
268
|
+
raise ArgumentError, "Unsupported HTTP method: #{method}"
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
request["Content-Type"] = "application/json"
|
|
272
|
+
request["Accept"] = "application/json"
|
|
273
|
+
request["Authorization"] = "Bearer #{@access_token}" if use_auth && @access_token
|
|
274
|
+
|
|
275
|
+
request.body = body.to_json if body
|
|
276
|
+
|
|
277
|
+
response = http.request(request)
|
|
278
|
+
handle_response(response)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Handle the HTTP response
|
|
282
|
+
#
|
|
283
|
+
# @param response [Net::HTTPResponse] The HTTP response
|
|
284
|
+
# @return [Hash, Array] Parsed JSON response
|
|
285
|
+
# @raise [ApiError, AuthenticationError, NotFoundError, ServerError] Various error types
|
|
286
|
+
def handle_response(response)
|
|
287
|
+
case response
|
|
288
|
+
when Net::HTTPSuccess
|
|
289
|
+
response.body && !response.body.empty? ? JSON.parse(response.body) : {}
|
|
290
|
+
when Net::HTTPUnauthorized
|
|
291
|
+
raise AuthenticationError, "Authentication failed: #{response.code} #{response.message}"
|
|
292
|
+
when Net::HTTPNotFound
|
|
293
|
+
raise NotFoundError, "Resource not found: #{response.code} #{response.message}"
|
|
294
|
+
when Net::HTTPClientError
|
|
295
|
+
error_message = parse_error_message(response)
|
|
296
|
+
raise ApiError, "API error: #{response.code} #{error_message}"
|
|
297
|
+
when Net::HTTPServerError
|
|
298
|
+
raise ServerError, "Server error: #{response.code} #{response.message}"
|
|
299
|
+
else
|
|
300
|
+
raise ApiError, "Unexpected response: #{response.code} #{response.message}"
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Parse error message from response
|
|
305
|
+
#
|
|
306
|
+
# @param response [Net::HTTPResponse] The HTTP response
|
|
307
|
+
# @return [String] Error message
|
|
308
|
+
def parse_error_message(response)
|
|
309
|
+
return response.message unless response.body && !response.body.empty?
|
|
310
|
+
|
|
311
|
+
begin
|
|
312
|
+
error_data = JSON.parse(response.body)
|
|
313
|
+
error_data["message"] || error_data["error"] || response.message
|
|
314
|
+
rescue JSON::ParserError
|
|
315
|
+
response.message
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Node
|
|
4
|
+
module Red
|
|
5
|
+
# Base error class for Node-RED API errors
|
|
6
|
+
class ApiError < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Raised when authentication fails
|
|
9
|
+
class AuthenticationError < ApiError; end
|
|
10
|
+
|
|
11
|
+
# Raised when a resource is not found
|
|
12
|
+
class NotFoundError < ApiError; end
|
|
13
|
+
|
|
14
|
+
# Raised when the server returns a 5xx error
|
|
15
|
+
class ServerError < ApiError; end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/node/red.rb
ADDED
data/sig/node/red.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: node-red
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Davide Santangelo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-10-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A comprehensive Ruby wrapper for the Node-RED Admin HTTP API, providing
|
|
14
|
+
programmatic access to flow management, node management, settings, and authentication.
|
|
15
|
+
email:
|
|
16
|
+
- davide.santangelo@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- ".rspec"
|
|
22
|
+
- ".rubocop.yml"
|
|
23
|
+
- CHANGELOG.md
|
|
24
|
+
- CODE_OF_CONDUCT.md
|
|
25
|
+
- LICENSE.txt
|
|
26
|
+
- README.md
|
|
27
|
+
- Rakefile
|
|
28
|
+
- examples/basic_usage.rb
|
|
29
|
+
- examples/flow_management.rb
|
|
30
|
+
- examples/node_management.rb
|
|
31
|
+
- lib/node/red.rb
|
|
32
|
+
- lib/node/red/client.rb
|
|
33
|
+
- lib/node/red/errors.rb
|
|
34
|
+
- lib/node/red/version.rb
|
|
35
|
+
- sig/node/red.rbs
|
|
36
|
+
homepage: https://github.com/davidesantangelo/node-red
|
|
37
|
+
licenses:
|
|
38
|
+
- MIT
|
|
39
|
+
metadata:
|
|
40
|
+
homepage_uri: https://github.com/davidesantangelo/node-red
|
|
41
|
+
source_code_uri: https://github.com/davidesantangelo/node-red
|
|
42
|
+
changelog_uri: https://github.com/davidesantangelo/node-red/blob/master/CHANGELOG.md
|
|
43
|
+
post_install_message:
|
|
44
|
+
rdoc_options: []
|
|
45
|
+
require_paths:
|
|
46
|
+
- lib
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: 3.1.0
|
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '0'
|
|
57
|
+
requirements: []
|
|
58
|
+
rubygems_version: 3.3.26
|
|
59
|
+
signing_key:
|
|
60
|
+
specification_version: 4
|
|
61
|
+
summary: Ruby client library for Node-RED Admin HTTP API
|
|
62
|
+
test_files: []
|