class Mechanize::Util

Constants

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)

Public Class Methods

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

Builds a query string from a given enumerable object parameters. This method uses ::each_parameter as preprocessor, which see.

# File lib/mechanize/util.rb, line 15
def build_query_string(parameters, enc = nil)
  each_parameter(parameters).inject(nil) { |s, (k, v)|
    # WEBrick::HTTP.escape* has some problems about m17n on ruby-1.9.*.
    (s.nil? ? '' : s << '&') << [CGI.escape(k.to_s), CGI.escape(v.to_s)].join('=')
  } || ''
end
detect_charset(src) click to toggle source
# File lib/mechanize/util.rb, line 102
def self.detect_charset(src)
  if src
    NKF.guess(src).name.upcase
  else
    Encoding::ISO8859_1.name
  end
end
each_parameter(parameters, &block) click to toggle source

Parses an enumerable object parameters and iterates over the key-value pairs it contains.

parameters may be a hash, or any enumerable object which iterates over [key, value] pairs, typically an array of arrays.

If a key is paired with an array-like object, the pair is expanded into multiple occurrences of the key, one for each element of the array. e.g. { a: [1, 2] } => [:a, 1], [:a, 2]

If a key is paired with a hash-like object, the pair is expanded into hash-like multiple pairs, one for each pair of the hash. e.g. { a: { x: 1, y: 2 } } => ['a', 1], ['a', 2]

An array-like value is allowed to be specified as hash value. e.g. { a: { q: [1, 2] } } => ['a', 1], ['a', 2]

For a non-array-like, non-hash-like value, the key-value pair is yielded as is.

# File lib/mechanize/util.rb, line 41
def each_parameter(parameters, &block)
  return to_enum(__method__, parameters) if block.nil?

  parameters.each { |key, value|
    each_parameter_1(key, value, &block)
  }
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 72
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
    s.encode(code)
  rescue EncodingError => 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 88
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 110
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 126
def self.uri_unescape str
  @parser ||= begin
                URI::Parser.new
              rescue NameError
                URI
              end

  @parser.unescape str
end