Merge branch 'master' of https://github.com/google/google-api-ruby-client
[arvados.git] / lib / google / api_client / gzip.rb
1 require 'faraday'
2 require 'zlib'
3  
4 module Google
5   class APIClient
6     class Gzip < Faraday::Response::Middleware
7       include Google::APIClient::Logging
8  
9       def on_complete(env)
10         encoding = env[:response_headers]['content-encoding'].to_s.downcase
11         case encoding
12         when 'gzip'
13           logger.debug { "Decompressing gzip encoded response (#{env[:body].length} bytes)" }        
14           env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body])).read
15           env[:response_headers].delete('content-encoding')
16           logger.debug { "Decompressed (#{env[:body].length} bytes)" }        
17         when 'deflate'
18           logger.debug{ "Decompressing deflate encoded response (#{env[:body].length} bytes)" }        
19           env[:body] = Zlib::Inflate.inflate(env[:body])
20           env[:response_headers].delete('content-encoding')
21           logger.debug { "Decompressed (#{env[:body].length} bytes)" }        
22         end
23       end
24     end
25   end
26 end
27  
28 Faraday::Response.register_middleware :gzip => Google::APIClient::Gzip