Class: Google::APIClient::Charset

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
Logging
Defined in:
lib/google/api_client/charset.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Instance Method Details

#adjust_encoding(env) ⇒ Object



19
20
21
22
23
24
# File 'lib/google/api_client/charset.rb', line 19

def adjust_encoding(env)
  charset = charset_for_content_type(env[:response_headers]['content-type'])
  if charset && env[:body].encoding != charset
    env[:body].force_encoding(charset)
  end
end

#charset_for_content_type(type) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/google/api_client/charset.rb', line 9

def charset_for_content_type(type)
  if type
    m = type.match(/(?:charset|encoding)="?([a-z0-9-]+)"?/i)
    if m
      return Encoding.find(m[1])
    end
  end
  nil
end

#on_complete(env) ⇒ Object



26
27
28
# File 'lib/google/api_client/charset.rb', line 26

def on_complete(env)
  adjust_encoding(env)
end