Class: Node::Red::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/node/red/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, access_token: nil) ⇒ Client

Initialize a new Node-RED Admin API client

Parameters:

  • base_url (String)

    The base URL of the Node-RED instance (e.g., “localhost:1880”)

  • access_token (String, nil) (defaults to: nil)

    Optional access token for authentication



16
17
18
19
# File 'lib/node/red/client.rb', line 16

def initialize(base_url, access_token: nil)
  @base_url = base_url.chomp("/")
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/node/red/client.rb', line 10

def access_token
  @access_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/node/red/client.rb', line 10

def base_url
  @base_url
end

Instance Method Details

#add_flow(flow) ⇒ Hash

Add a flow to the active configuration

Parameters:

  • flow (Hash)

    The flow configuration

Returns:

  • (Hash)

    The created flow with ID



116
117
118
# File 'lib/node/red/client.rb', line 116

def add_flow(flow)
  post("/flow", flow)
end

#auth_loginHash

Get the active authentication scheme

Returns:

  • (Hash)

    Authentication scheme information



26
27
28
# File 'lib/node/red/client.rb', line 26

def 
  get("/auth/login")
end

#auth_revoke(token) ⇒ Hash

Revoke an access token

Parameters:

  • token (String)

    The token to revoke

Returns:

  • (Hash)

    Revocation response



55
56
57
58
# File 'lib/node/red/client.rb', line 55

def auth_revoke(token)
  body = { token: token }
  post("/auth/revoke", body)
end

#auth_token(client_id:, grant_type:, scope:, username:, password:) ⇒ Hash

Exchange credentials for access token

Parameters:

  • client_id (String)

    The client ID

  • grant_type (String)

    The grant type (e.g., “password”)

  • scope (String)

    The scope (e.g., “*” for full access)

  • username (String)

    The username

  • password (String)

    The password

Returns:

  • (Hash)

    Token response with access_token



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/node/red/client.rb', line 38

def auth_token(client_id:, grant_type:, scope:, username:, password:)
  body = {
    client_id: client_id,
    grant_type: grant_type,
    scope: scope,
    username: username,
    password: password
  }
  response = post("/auth/token", body, use_auth: false)
  @access_token = response["access_token"] if response["access_token"]
  response
end

#delete_flow(id) ⇒ Hash

Delete an individual flow configuration

Parameters:

  • id (String)

    The flow ID

Returns:

  • (Hash)

    Deletion response



141
142
143
# File 'lib/node/red/client.rb', line 141

def delete_flow(id)
  delete("/flow/#{id}")
end

#delete_node_module(module_name) ⇒ Hash

Remove a node module

Parameters:

  • module_name (String)

    The module name

Returns:

  • (Hash)

    Deletion response



185
186
187
# File 'lib/node/red/client.rb', line 185

def delete_node_module(module_name)
  delete("/nodes/#{module_name}")
end

#deploy_flows(flows, rev: nil) ⇒ Hash

Set the active flow configuration

Parameters:

  • flows (Array<Hash>)

    The flow configuration

  • rev (String, nil) (defaults to: nil)

    Optional revision identifier

Returns:

  • (Hash)

    Response with revision ID



97
98
99
100
101
# File 'lib/node/red/client.rb', line 97

def deploy_flows(flows, rev: nil)
  body = { flows: flows }
  body[:rev] = rev if rev
  post("/flows", body)
end

#diagnosticsHash

Get the runtime diagnostics

Returns:

  • (Hash)

    Runtime diagnostics



72
73
74
# File 'lib/node/red/client.rb', line 72

def diagnostics
  get("/diagnostics")
end

#flow(id) ⇒ Hash

Get an individual flow configuration

Parameters:

  • id (String)

    The flow ID

Returns:

  • (Hash)

    Flow configuration



124
125
126
# File 'lib/node/red/client.rb', line 124

def flow(id)
  get("/flow/#{id}")
end

#flowsArray<Hash>

Get the active flow configuration

Returns:

  • (Array<Hash>)

    Array of flow configurations



81
82
83
# File 'lib/node/red/client.rb', line 81

def flows
  get("/flows")
end

#flows_stateHash

Get the active flow’s runtime state

Returns:

  • (Hash)

    Flow runtime state



88
89
90
# File 'lib/node/red/client.rb', line 88

def flows_state
  get("/flows/state")
end

#install_node(module_name) ⇒ Hash

Install a new node module

Parameters:

  • module_name (String)

    The name of the node module to install

Returns:

  • (Hash)

    Installation response



158
159
160
161
# File 'lib/node/red/client.rb', line 158

def install_node(module_name)
  body = { module: module_name }
  post("/nodes", body)
end

#node_module(module_name) ⇒ Hash

Get a node module’s information

Parameters:

  • module_name (String)

    The module name

Returns:

  • (Hash)

    Module information



167
168
169
# File 'lib/node/red/client.rb', line 167

def node_module(module_name)
  get("/nodes/#{module_name}")
end

#node_set(module_name, set_name) ⇒ Hash

Get a node module set information

Parameters:

  • module_name (String)

    The module name

  • set_name (String)

    The set name

Returns:

  • (Hash)

    Node set information



194
195
196
# File 'lib/node/red/client.rb', line 194

def node_set(module_name, set_name)
  get("/nodes/#{module_name}/#{set_name}")
end

#nodesArray<Hash>

Get a list of the installed nodes

Returns:

  • (Array<Hash>)

    List of installed nodes



150
151
152
# File 'lib/node/red/client.rb', line 150

def nodes
  get("/nodes")
end

#set_flows_state(state) ⇒ Hash

Set the active flow’s runtime state

Parameters:

  • state (String)

    The desired state (“start” or “stop”)

Returns:

  • (Hash)

    State change response



107
108
109
110
# File 'lib/node/red/client.rb', line 107

def set_flows_state(state)
  body = { state: state }
  post("/flows/state", body)
end

#settingsHash

Get the runtime settings

Returns:

  • (Hash)

    Runtime settings



65
66
67
# File 'lib/node/red/client.rb', line 65

def settings
  get("/settings")
end

#update_flow(id, flow) ⇒ Hash

Update an individual flow configuration

Parameters:

  • id (String)

    The flow ID

  • flow (Hash)

    The updated flow configuration

Returns:

  • (Hash)

    The updated flow



133
134
135
# File 'lib/node/red/client.rb', line 133

def update_flow(id, flow)
  put("/flow/#{id}", flow)
end

#update_node_module(module_name, enabled:) ⇒ Hash

Enable or disable a node module

Parameters:

  • module_name (String)

    The module name

  • enabled (Boolean)

    Whether to enable or disable the module

Returns:

  • (Hash)

    Update response



176
177
178
179
# File 'lib/node/red/client.rb', line 176

def update_node_module(module_name, enabled:)
  body = { enabled: enabled }
  put("/nodes/#{module_name}", body)
end

#update_node_set(module_name, set_name, enabled:) ⇒ Hash

Enable or disable a node set

Parameters:

  • module_name (String)

    The module name

  • set_name (String)

    The set name

  • enabled (Boolean)

    Whether to enable or disable the set

Returns:

  • (Hash)

    Update response



204
205
206
207
# File 'lib/node/red/client.rb', line 204

def update_node_set(module_name, set_name, enabled:)
  body = { enabled: enabled }
  put("/nodes/#{module_name}/#{set_name}", body)
end