Parent

Mechanize::Util

Constants

CODE_DIC
NEW_RUBY_ENCODING

true if RUBY_VERSION is 1.9.0 or later

ENCODING_ERRORS

contains encoding error classes to raise

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
    # File lib/mechanize/util.rb, line 30
30:   def self.build_query_string(parameters, enc=nil)
31:     parameters.map { |k,v|
32:       # WEBrick::HTTP.escape* has some problems about m17n on ruby-1.9.*.
33:       [CGI.escape(k.to_s), CGI.escape(v.to_s)].join("=") if k
34:     }.compact.join('&')
35:   end
detect_charset(src) click to toggle source
    # File lib/mechanize/util.rb, line 78
78:   def self.detect_charset(src)
79:     case enc = src && NKF.guess(src)
80:     when Integer
81:       # Ruby <= 1.8
82:       CODE_DIC[enc]
83:     else
84:       # Ruby >= 1.9
85:       enc && enc.to_s.upcase
86:     end || "ISO-8859-1"
87:   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
38:   def self.from_native_charset(s, code, ignore_encoding_error=false, log=nil)
39:     return s unless s && code
40:     return s unless Mechanize.html_parser == Nokogiri::HTML
41: 
42:     begin
43:       encode_to(code, s)
44:     rescue *ENCODING_ERRORS => ex
45:       log.debug("from_native_charset: #{ex.class}: form encoding: #{code.inspect} string: #{s}") if log
46:       if ignore_encoding_error
47:         s
48:       else
49:         raise
50:       end
51:     end
52:   end
html_unescape(s) click to toggle source
    # File lib/mechanize/util.rb, line 64
64:   def self.html_unescape(s)
65:     return s unless s
66:     s.gsub(/&(\w+|#[0-9]+);/) { |match|
67:       number = case match
68:                when /&(\w+);/
69:                  Mechanize.html_parser::NamedCharacters[$1]
70:                when /&#([0-9]+);/
71:                  $1.to_i
72:                end
73: 
74:       number ? ([number].pack('U') rescue match) : match
75:     }
76:   end
uri_escape(str, unsafe = nil) click to toggle source
     # File lib/mechanize/util.rb, line 89
 89:   def self.uri_escape str, unsafe = nil
 90:     @parser ||= begin
 91:                   URI::Parser.new
 92:                 rescue NameError
 93:                   URI
 94:                 end
 95: 
 96:     if URI == @parser then
 97:       unsafe ||= URI::UNSAFE
 98:     else
 99:       unsafe ||= @parser.regexp[:UNSAFE]
100:     end
101: 
102:     @parser.escape str, unsafe
103:   end
uri_unescape(str) click to toggle source
     # File lib/mechanize/util.rb, line 105
105:   def self.uri_unescape str
106:     @parser ||= begin
107:                   URI::Parser.new
108:                 rescue NameError
109:                   URI
110:                 end
111: 
112:     @parser.unescape str
113:   end

Private Class Methods

encode_to(encoding, str) click to toggle source

inner convert method of Util.from_native_charset

    # File lib/mechanize/util.rb, line 55
55:   def self.encode_to(encoding, str)
56:     if NEW_RUBY_ENCODING
57:       str.encode(encoding)
58:     else
59:       Iconv.conv(encoding.to_s, "UTF-8", str)
60:     end
61:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.