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.



19
20
21
# File 'lib/twirp/service_dsl.rb', line 19

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.



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

def package_name
  @package.to_s
end

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

Configure service rpc methods.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/twirp/service_dsl.rb', line 29

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.



69
70
71
# File 'lib/twirp/service_dsl.rb', line 69

def rpcs
  @rpcs || {}
end

#service(name) ⇒ Object

Configure service name.



24
25
26
# File 'lib/twirp/service_dsl.rb', line 24

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]”



63
64
65
# File 'lib/twirp/service_dsl.rb', line 63

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.



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

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