Class: JSONRPC::MethodConstraint Private

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonrpc/railtie/method_constraint.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.

This constraint allows Rails routes to be matched based on the JSON-RPC method name in the request, enabling method-specific routing.

Examples:

Using in Rails routes

post '/', to: 'jsonrpc#echo', constraints: JSONRPC::Railtie::MethodConstraint.new('echo')
post '/', to: 'jsonrpc#ping', constraints: JSONRPC::Railtie::MethodConstraint.new('ping')

Instance Method Summary collapse

Constructor Details

#initialize(jsonrpc_method_name) ⇒ MethodConstraint

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 method constraint

Parameters:

  • jsonrpc_method_name (String)

    The JSON-RPC method name to match against



18
19
20
# File 'lib/jsonrpc/railtie/method_constraint.rb', line 18

def initialize(jsonrpc_method_name)
  @jsonrpc_method_name = jsonrpc_method_name.to_s
end

Instance Method Details

#matches?(request) ⇒ Boolean

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.

Check if the request matches the configured method name

Parameters:

  • request (ActionDispatch::Request)

    The Rails request object

Returns:

  • (Boolean)

    true if the JSON-RPC method matches, false otherwise



28
29
30
31
32
33
34
# File 'lib/jsonrpc/railtie/method_constraint.rb', line 28

def matches?(request)
  jsonrpc_request = request.env['jsonrpc.request']

  return false unless jsonrpc_request

  jsonrpc_request.method == @jsonrpc_method_name
end

#to_sString

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.

Returns a string representation of the constraint

Returns:

  • (String)

    A string showing the configured JSON-RPC method name



40
41
42
# File 'lib/jsonrpc/railtie/method_constraint.rb', line 40

def to_s
  "#<JSONRPC::MethodConstraint method=\"#{@jsonrpc_method_name}\">"
end