Module: Artery::Backend::ClassMethods
- Defined in:
- lib/artery/backend.rb
Instance Method Summary collapse
-
#publish(route, data) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#request(route, data = nil, options = {}) {|handler| ... } ⇒ Object
rubocop:disable Metrics/AbcSize.
- #subscribe(route, options = {}) ⇒ Object
Instance Method Details
#publish(route, data) ⇒ Object
rubocop:enable Metrics/AbcSize
94 95 96 97 |
# File 'lib/artery/backend.rb', line 94 def publish(route, data) backend.publish(route, data.to_json) Artery::Instrumentation.instrument(:publish, route: route, data: data) end |
#request(route, data = nil, options = {}) {|handler| ... } ⇒ Object
rubocop:disable Metrics/AbcSize
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/artery/backend.rb', line 52 def request(route, data = nil, = {}) raise ArgumentError, 'You must provide block to handle response' unless block_given? handler = Multiblock.wrapper uri = Routing.uri(route) yield(handler) data ||= {} Artery::Instrumentation.instrument(:request, stage: :sent, route: uri.to_route, data: data) request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) backend.request(uri.to_route, data.to_json, ) do || duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start) * 1000 if .is_a?(Error) # timeout case Artery::Instrumentation.instrument(:request, stage: :error, route: uri.to_route, error: ., duration_ms: duration_ms) handler.call :error, else Artery::Instrumentation.instrument(:request, stage: :response, route: uri.to_route, data: , duration_ms: duration_ms) begin ||= '{}' response = JSON.parse().with_indifferent_access if response.key?(:error) handler.call :error, RequestError.new(uri, response, request: { route: uri.to_route, data: data.to_json }, response: ) else handler.call :success, response end rescue JSON::ParserError => e Artery.handle_error FormatError.new(uri, , original_exception: e, request: { route: uri.to_route, data: data.to_json }, response: ) end end end end |
#subscribe(route, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/artery/backend.rb', line 40 def subscribe(route, = {}) backend.subscribe(route, ) do |json, reply, from| json = '{}' if json.blank? yield(JSON.parse(json).with_indifferent_access, reply, from) rescue StandardError => e Artery.handle_error FormatError.new(from, json, original_exception: e, subscription: { route: from, data: json }) end end |