Class: Protein::Client
- Inherits:
-
Object
- Object
- Protein::Client
- Defined in:
- lib/protein/client.rb
Class Method Summary collapse
- .call(request) ⇒ Object
- .call!(request) ⇒ Object
- .proto(proto) ⇒ Object
- .push(request) ⇒ Object
- .route(router) ⇒ Object
- .router ⇒ Object
- .service(service) ⇒ Object
- .transport(transport, opts = {}) ⇒ Object
- .transport_class ⇒ Object
Class Method Details
.call(request) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/protein/client.rb', line 33 def call(request) service_class = router.resolve_by_request(request) raise(ArgumentError, "called to non-responding service") unless service_class.response? service_name = service_class.service_name request_class = service_class.request_class request_buf = request_class.encode(request) request_payload = Payload::Request.encode(service_name, request_buf) response_payload = transport_class.call(request_payload) response_buf, errors = Payload::Response.decode(response_payload) service_instance = service_class.new(request) if response_buf response = service_class.response_class.decode(response_buf) service_instance.resolve(response) elsif errors service_instance.reject(errors) end service_instance end |
.call!(request) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/protein/client.rb', line 57 def call!(request) service_instance = call(request) if service_instance.failure? raise(CallError, service_instance.errors) end service_instance.response end |
.proto(proto) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/protein/client.rb', line 13 def proto(proto) service = Class.new(Service) service.proto(proto) @router ||= Class.new(Router) @router.service(service) end |
.push(request) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/protein/client.rb', line 66 def push(request) service_class = router.resolve_by_request(request) raise(ArgumentError, "pushed to responding service") if service_class.response? service_name = service_class.service_name request_class = service_class.request_class request_buf = request_class.encode(request) request_payload = Payload::Request.encode(service_name, request_buf) transport_class.push(request_payload) nil end |
.route(router) ⇒ Object
4 5 6 |
# File 'lib/protein/client.rb', line 4 def route(router) @router = Router.define(router) end |
.router ⇒ Object
21 22 23 |
# File 'lib/protein/client.rb', line 21 def router GetConst.call(@router) end |
.service(service) ⇒ Object
8 9 10 11 |
# File 'lib/protein/client.rb', line 8 def service(service) @router ||= Class.new(Router) @router.service(service) end |
.transport(transport, opts = {}) ⇒ Object
25 26 27 |
# File 'lib/protein/client.rb', line 25 def transport(transport, opts = {}) @transport_class = Transport.define(transport, opts) end |
.transport_class ⇒ Object
29 30 31 |
# File 'lib/protein/client.rb', line 29 def transport_class GetConst.call(@transport_class) end |