class Faraday::Encoding

Public Class Methods

mappings() click to toggle source
# File lib/faraday/encoding.rb, line 5
def self.mappings
  {
    'utf8' => 'utf-8'
  }
end

Public Instance Methods

call(environment) click to toggle source
# File lib/faraday/encoding.rb, line 11
def call(environment)
  @app.call(environment).on_complete do |env|
    @env = env
    if encoding = content_charset
      env[:body] = env[:body].dup if env[:body].frozen?
      env[:body].force_encoding(encoding)
    end
  end
end

Private Instance Methods

content_charset() click to toggle source

@return [Encoding|NilClass] returns Encoding or nil

# File lib/faraday/encoding.rb, line 24
def content_charset
  ::Encoding.find encoding_name rescue nil
end
content_type() click to toggle source

@return [String]

# File lib/faraday/encoding.rb, line 45
def content_type
  @env[:response_headers][:content_type]
end
encoding_name() click to toggle source

@return [String] returns a string representing encoding name if it is find in the CONTENT TYPE header

# File lib/faraday/encoding.rb, line 29
def encoding_name
  if /charset=([^;|$]+)/.match(content_type)
    mapped_encoding(Regexp.last_match(1))
  end
end
mapped_encoding(encoding_name) click to toggle source

@param [String] encoding_name @return [String] tries to find a mapping for the encoding name ex: returns 'utf-8' for encoding_name 'utf8' if mapping is not found - return the same input parameter `encoding_name` Look at `self.mappings` to see which mappings are available

# File lib/faraday/encoding.rb, line 40
def mapped_encoding(encoding_name)
  self.class.mappings.fetch(encoding_name, encoding_name)
end