21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / lib / google / api_client / charset.rb
1 require 'faraday'
2 require 'zlib'
3  
4 module Google
5   class APIClient
6     class Charset < Faraday::Middleware
7       include Google::APIClient::Logging
8
9       def charset_for_content_type(type)
10         if type
11           m = type.match(/(?:charset|encoding)="?([a-z0-9-]+)"?/i)
12           if m
13             return Encoding.find(m[1])
14           end
15         end
16         nil
17       end
18
19       def adjust_encoding(env)
20         charset = charset_for_content_type(env[:response_headers]['content-type'])
21         if charset && env[:body].encoding != charset
22           env[:body].force_encoding(charset)
23         end
24       end
25       
26       def on_complete(env)
27         adjust_encoding(env)
28       end
29     end
30   end
31 end
32  
33 Faraday::Response.register_middleware :charset => Google::APIClient::Charset