File: call.rb

package info (click to toggle)
ruby-httpx 1.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,816 kB
  • sloc: ruby: 12,209; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# frozen_string_literal: true

module HTTPX
  module Plugins
    module GRPC
      # Encapsulates call information
      class Call
        attr_writer :decoder

        def initialize(response)
          @response = response
          @decoder = ->(z) { z }
          @consumed = false
          @grpc_response = nil
        end

        def inspect
          "#{self.class}(#{grpc_response})"
        end

        def to_s
          grpc_response.to_s
        end

        def metadata
          response.headers
        end

        def trailing_metadata
          return unless @consumed

          @response.trailing_metadata
        end

        private

        def grpc_response
          @grpc_response ||= if @response.respond_to?(:each)
            Enumerator.new do |y|
              Message.stream(@response).each do |message|
                y << @decoder.call(message)
              end
              @consumed = true
            end
          else
            @consumed = true
            @decoder.call(Message.unary(@response))
          end
        end

        def respond_to_missing?(meth, *args, &blk)
          grpc_response.respond_to?(meth, *args) || super
        end

        def method_missing(meth, *args, &blk)
          return grpc_response.__send__(meth, *args, &blk) if grpc_response.respond_to?(meth)

          super
        end
      end
    end
  end
end