Class: RailsTwirp::RouteSet::ServiceRouteSet
- Inherits:
-
Object
- Object
- RailsTwirp::RouteSet::ServiceRouteSet
- Defined in:
- lib/rails_twirp/route_set.rb
Instance Attribute Summary collapse
-
#rpcs ⇒ Object
readonly
Returns the value of attribute rpcs.
Instance Method Summary collapse
- #add_route(name, mapping) ⇒ Object
-
#initialize(service_class) ⇒ ServiceRouteSet
constructor
A new instance of ServiceRouteSet.
- #to_service ⇒ Object
Constructor Details
#initialize(service_class) ⇒ ServiceRouteSet
Returns a new instance of ServiceRouteSet.
26 27 28 29 |
# File 'lib/rails_twirp/route_set.rb', line 26 def initialize(service_class) @service_class = service_class @rpcs = {} end |
Instance Attribute Details
#rpcs ⇒ Object (readonly)
Returns the value of attribute rpcs.
24 25 26 |
# File 'lib/rails_twirp/route_set.rb', line 24 def rpcs @rpcs end |
Instance Method Details
#add_route(name, mapping) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/rails_twirp/route_set.rb', line 31 def add_route(name, mapping) if @rpcs[name] raise ArgumentError, "Invalid RPC, route already defined: #{name}" end @rpcs[name] = mapping end |
#to_service ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rails_twirp/route_set.rb', line 39 def to_service # Synthesize a handler that will process the requests # handler = Class.new @rpcs.each do |name, mapping| rpc_info = @service_class.rpcs[name] method_name = rpc_info[:ruby_method] # Stolen from Rails in ActionDispatch::Request#controller_class_for controller_name = mapping.controller.underscore const_name = controller_name.camelize << "Controller" action_name = mapping.action response_class = rpc_info[:output_class] handler.define_method(method_name) do |req, env| controller_class = ::ActiveSupport::Dependencies.constantize(const_name) controller_class.dispatch(action_name, req, response_class, env) end end service = @service_class.new(handler.new) service.before do |rack_env, env| env[:rack_env] = rack_env end service end |