Class: JSONRPC::RoutesDsl Private

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonrpc/railtie/routes_dsl.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

DSL context for defining JSON-RPC routes within a jsonrpc block

Examples:

Simple method routing

jsonrpc '/api/v1' do
  method 'ping', to: 'system#ping'
  method 'status', to: 'system#status'
end

Single-level namespace

jsonrpc '/api/v1' do
  namespace 'users' do
    method 'create', to: 'users#create'  # becomes users.create
    method 'list', to: 'users#index'     # becomes users.list
  end
end

Nested namespaces (smart home control system)

jsonrpc '/' do
  # Handle batch requests with dedicated controller
  batch to: 'batch#handle'

  method 'on', to: 'main#on'
  method 'off', to: 'main#off'

  namespace 'lights' do
    method 'on', to: 'lights#on'    # becomes lights.on
    method 'off', to: 'lights#off'  # becomes lights.off
  end

  namespace 'climate' do
    method 'on', to: 'climate#on'   # becomes climate.on
    method 'off', to: 'climate#off' # becomes climate.off

    namespace 'fan' do
      method 'on', to: 'fan#on'     # becomes climate.fan.on
      method 'off', to: 'fan#off'   # becomes climate.fan.off
    end
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(mapper, path_prefix = '/') ⇒ RoutesDsl

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new routes DSL context

Parameters:

  • mapper (ActionDispatch::Routing::Mapper)

    the Rails route mapper

  • path_prefix (String) (defaults to: '/')

    the base path for JSON-RPC requests



52
53
54
55
56
# File 'lib/jsonrpc/railtie/routes_dsl.rb', line 52

def initialize(mapper, path_prefix = '/')
  @mapper = mapper
  @path_prefix = path_prefix
  @namespace_stack = []
end

Instance Method Details

#batch(to:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Define a route for handling JSON-RPC batch requests

Examples:

Map batch requests to a controller action

batch to: 'batches#handle'

Parameters:

  • to (String)

    the Rails controller action (e.g., ‘batches#handle’)



91
92
93
94
95
96
97
98
# File 'lib/jsonrpc/railtie/routes_dsl.rb', line 91

def batch(to:)
  constraint = JSONRPC::BatchConstraint.new

  @mapper.post @path_prefix, {
    to: to,
    constraints: constraint
  }
end

#method(jsonrpc_method, to:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Define a JSON-RPC method route

Examples:

Map a JSON-RPC method to controller action

method 'user.create', to: 'users#create'

Method within a namespace

namespace 'posts' do
  method 'create', to: 'posts#create'  # becomes posts.create
end

Parameters:

  • jsonrpc_method (String)

    the JSON-RPC method name

  • to (String)

    the Rails controller action (e.g., ‘users#create’)



73
74
75
76
77
78
79
80
81
# File 'lib/jsonrpc/railtie/routes_dsl.rb', line 73

def method(jsonrpc_method, to:)
  full_method_name = build_full_method_name(jsonrpc_method)
  constraint = JSONRPC::MethodConstraint.new(full_method_name)

  @mapper.post @path_prefix, {
    to: to,
    constraints: constraint
  }
end

#namespace(name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Create a namespace for grouping related JSON-RPC methods

Namespaces can be nested to create hierarchical method names. Each level of nesting adds a dot-separated prefix to the method names.

Examples:

Single-level namespace

namespace 'posts' do
  method 'create', to: 'posts#create' # becomes posts.create
  method 'delete', to: 'posts#delete' # becomes posts.list
end

Nested namespaces

namespace 'climate' do
  method 'on', to: 'climate#on'   # becomes climate.on
  method 'off', to: 'climate#off' # becomes climate.off

  namespace 'fan' do
    method 'on', to: 'fan#on'     # becomes climate.fan.on
    method 'off', to: 'fan#off'   # becomes climate.fan.off
  end
end

Parameters:

  • name (String)

    the namespace name



126
127
128
129
130
# File 'lib/jsonrpc/railtie/routes_dsl.rb', line 126

def namespace(name, &)
  @namespace_stack.push(name)
  instance_eval(&)
  @namespace_stack.pop
end