Class: Mechanize::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/mechanize/util.rb

Constant Summary collapse

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)
WEBrick::HTTPUtils::DefaultMimeTypes

Class Method Summary collapse

Class Method Details

.build_query_string(parameters, enc = nil) ⇒ Object

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



16
17
18
19
20
21
# File 'lib/mechanize/util.rb', line 16

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? ? String.new : s << '&') << [CGI.escape(k.to_s), CGI.escape(v.to_s)].join('=')
  } || ''
end

.detect_charset(src) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/mechanize/util.rb', line 128

def self.detect_charset(src)
  if src
    guess_encoding(src).name.upcase
  else
    Encoding::ISO8859_1.name
  end
end

.each_parameter(parameters, &block) ⇒ Object

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.



42
43
44
45
46
47
48
# File 'lib/mechanize/util.rb', line 42

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) ⇒ Object

Converts string s from code to UTF-8.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mechanize/util.rb', line 73

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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mechanize/util.rb', line 89

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) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mechanize/util.rb', line 136

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) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/mechanize/util.rb', line 152

def self.uri_unescape str
  @parser ||= begin
                URI::Parser.new
              rescue NameError
                URI
              end

  @parser.unescape str
end