Class: HTTPX::Transcoder::GRPCEncoding::Inflater

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/grpc/grpc_encoding.rb

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Inflater

Returns a new instance of Inflater.



38
39
40
41
# File 'lib/httpx/plugins/grpc/grpc_encoding.rb', line 38

def initialize(response)
  @response = response
  @grpc_encodings = nil
end

Instance Method Details

#call(message, &blk) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/httpx/plugins/grpc/grpc_encoding.rb', line 43

def call(message, &blk)
  data = "".b

  until message.empty?
    compressed, size = message.unpack("CL>")

    encoded_data = message.byteslice(5..size + 5 - 1)

    if compressed == 1
      grpc_encodings.reverse_each do |encoding|
        decoder = @response.body.class.initialize_inflater_by_encoding(encoding, @response, bytesize: encoded_data.bytesize)
        encoded_data = decoder.call(encoded_data)

        blk.call(encoded_data) if blk

        data << encoded_data
      end
    else
      blk.call(encoded_data) if blk

      data << encoded_data
    end

    message = message.byteslice((size + 5)..-1)
  end

  data
end