Method: Mechanize::CookieJar#load

Defined in:
lib/mechanize/cookie_jar.rb

#load(input, *options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/mechanize/cookie_jar.rb', line 121

def load(input, *options)
  input.respond_to?(:write) or
    return ::File.open(input, 'r') { |io| load(io, *options) }

  opthash = {
    :format => :yaml,
    :session => false,
  }
  case options.size
  when 0
  when 1
    case options = options.first
    when Symbol
      opthash[:format] = options
    else
      if hash = Hash.try_convert(options)
        opthash.update(hash)
      end
    end
  when 2
    opthash[:format], options = options
    if hash = Hash.try_convert(options)
      opthash.update(hash)
    end
  else
    raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
  end

  return super(input, opthash) if opthash[:format] != :yaml

  begin
    data = load_yaml(input)
  rescue ArgumentError
    @logger.warn "unloadable YAML cookie data discarded" if @logger
    return self
  end

  case data
  when Array
    # Forward compatibility
    data.each { |cookie|
      add(cookie)
    }
  when Hash
    data.each { |domain, paths|
      paths.each { |path, names|
        names.each { |cookie_name, cookie|
          add(cookie)
        }
      }
    }
  else
    @logger.warn "incompatible YAML cookie data discarded" if @logger
    return self
  end
end