Class: Mechanize::Util

### Home Classes Methods

### In Files

- lib/mechanize/util.rb

### Parent

Object

### Methods

- ::build_query_string
- ::detect_charset
- ::from_native_charset
- ::html_unescape
- ::uri_escape
- ::uri_unescape

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::Util

### Constants

CODE_DIC
DefaultMimeTypes

  default mime type data for Page::Image#mime_type. You can use another Apache-compatible mimetab.

      mimetab = WEBrick::HTTPUtils.load_mime_types('/etc/mime.types')
      Mechanize::Util::DefaultMimeTypes.replace(mimetab)

ENCODING_ERRORS

  contains encoding error classes to raise

NEW_RUBY_ENCODING

  true if RUBY_VERSION is 1.9.0 or later

### Public Class Methods

build_query_string(parameters, enc = nil) click to toggle source

    # File lib/mechanize/util.rb, line 30
    def self.build_query_string(parameters, enc = nil)
      parameters.map { |k,v|
        # WEBrick::HTTP.escape* has some problems about m17n on ruby-1.9.*.
        [CGI.escape(k.to_s), CGI.escape(v.to_s)].join("=") if k
      }.compact.join('&')
    end

detect_charset(src) click to toggle source

    # File lib/mechanize/util.rb, line 78
    def self.detect_charset(src)
      case enc = src && NKF.guess(src)
      when Integer
        # Ruby <= 1.8
        CODE_DIC[enc]
      else
        # Ruby >= 1.9
        enc && enc.to_s.upcase
      end || "ISO-8859-1"
    end

from_native_charset(s, code, ignore_encoding_error = false, log = nil) click to toggle source

Converts string `s` from `code` to UTF-8.

    # File lib/mechanize/util.rb, line 38
    def self.from_native_charset(s, code, ignore_encoding_error = false, log = nil)
      return s unless s && code
      return s unless Mechanize.html_parser == Nokogiri::HTML

      begin
        encode_to(code, s)
      rescue *ENCODING_ERRORS => ex
        log.debug("from_native_charset: #{ex.class}: form encoding: #{code.inspect} string: #{s}") if log
        if ignore_encoding_error
          s
        else
          raise
        end
      end
    end

html_unescape(s) click to toggle source

    # File lib/mechanize/util.rb, line 64
    def self.html_unescape(s)
      return s unless s
      s.gsub(/&(\w+|#[0-9]+);/) { |match|
        number = case match
                 when /&(\w+);/
                   Mechanize.html_parser::NamedCharacters[$1]
                 when /&#([0-9]+);/
                   $1.to_i
                 end

        number ? ([number].pack('U') rescue match) : match
      }
    end

uri_escape(str, unsafe = nil) click to toggle source

    # File lib/mechanize/util.rb, line 89
    def self.uri_escape str, unsafe = nil
      @parser ||= begin
                    URI::Parser.new
                  rescue NameError
                    URI
                  end

      if URI == @parser then
        unsafe ||= URI::UNSAFE
      else
        unsafe ||= @parser.regexp[:UNSAFE]
      end

      @parser.escape str, unsafe
    end

uri_unescape(str) click to toggle source

    # File lib/mechanize/util.rb, line 105
    def self.uri_unescape str
      @parser ||= begin
                    URI::Parser.new
                  rescue NameError
                    URI
                  end

      @parser.unescape str
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.