Module: Twirp::ServiceDSL

Included in:
Client, Service
Defined in:
lib/twirp/service_dsl.rb

Instance Method Summary collapse

Instance Method Details

#package(name) ⇒ Object

Configure service package name.



6
7
8
# File 'lib/twirp/service_dsl.rb', line 6

def package(name)
  @package = name.to_s
end

#package_nameObject

Get configured package name as String. An empty value means that there’s no package.



37
38
39
# File 'lib/twirp/service_dsl.rb', line 37

def package_name
  @package.to_s
end

#rpc(rpc_method, input_class, output_class, opts) ⇒ Object

Configure service rpc methods.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twirp/service_dsl.rb', line 16

def rpc(rpc_method, input_class, output_class, opts)
  raise ArgumentError.new("rpc_method can not be empty") if rpc_method.to_s.empty?
  raise ArgumentError.new("input_class must be a Protobuf Message class") unless input_class.is_a?(Class)
  raise ArgumentError.new("output_class must be a Protobuf Message class") unless output_class.is_a?(Class)
  raise ArgumentError.new("opts[:ruby_method] is mandatory") unless opts && opts[:ruby_method]

  rpcdef = {
    rpc_method: rpc_method.to_sym, # as defined in the Proto file.
    input_class: input_class, # google/protobuf Message class to serialize the input (proto request).
    output_class: output_class, # google/protobuf Message class to serialize the output (proto response).
    ruby_method: opts[:ruby_method].to_sym, # method on the handler or client to handle this rpc requests.
  }

  @rpcs ||= {}
  @rpcs[rpc_method.to_s] = rpcdef

  rpc_define_method(rpcdef) if respond_to? :rpc_define_method # hook for the client to implement the methods on the class
end

#rpcsObject

Get raw definitions for rpc methods. This values are used as base env for handler methods.



56
57
58
# File 'lib/twirp/service_dsl.rb', line 56

def rpcs
  @rpcs || {}
end

#service(name) ⇒ Object

Configure service name.



11
12
13
# File 'lib/twirp/service_dsl.rb', line 11

def service(name)
  @service = name.to_s
end

#service_full_nameObject

Service name with package prefix, which should uniquelly identifiy the service, for example “example.v3.Haberdasher” for package “example.v3” and service “Haberdasher”. This can be used as a path prefix to route requests to the service, because a Twirp URL is: “#base_url/##service_full_name/#{method]”



50
51
52
# File 'lib/twirp/service_dsl.rb', line 50

def service_full_name
  package_name.empty? ? service_name : "#{package_name}.#{service_name}"
end

#service_nameObject

Service name as String. Defaults to the class name.



42
43
44
# File 'lib/twirp/service_dsl.rb', line 42

def service_name
  (@service || self.name.split("::").last).to_s
end