Class: Mechanize::CookieJar
- Inherits:
-
HTTP::CookieJar
- Object
- HTTP::CookieJar
- Mechanize::CookieJar
- Defined in:
- lib/mechanize/cookie_jar.rb
Instance Method Summary collapse
Instance Method Details
#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, *) input.respond_to?(:write) or return ::File.open(input, 'r') { |io| load(io, *) } opthash = { :format => :yaml, :session => false, } case .size when 0 when 1 case = .first when Symbol opthash[:format] = else if hash = Hash.try_convert() opthash.update(hash) end end when 2 opthash[:format], = if hash = Hash.try_convert() opthash.update(hash) end else raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + .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 { || add() } when Hash data.each { |domain, paths| paths.each { |path, names| names.each { |, | add() } } } else @logger.warn "incompatible YAML cookie data discarded" if @logger return self end end |
#save(output, *options) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mechanize/cookie_jar.rb', line 67 def save(output, *) output.respond_to?(:write) or return ::File.open(output, 'w') { |io| save(io, *) } opthash = { :format => :yaml, :session => false, } case .size when 0 when 1 case = .first when Symbol opthash[:format] = else opthash.update() if end when 2 opthash[:format], = opthash.update() if else raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + .size) end return super(output, opthash) if opthash[:format] != :yaml session = opthash[:session] nstore = HashStore.new each { || next if !session && .session? if .max_age = .dup .expires = .expires # convert max_age to expires end nstore.add() } yaml = YAML.dump(nstore.instance_variable_get(:@jar)) # a gross hack yaml.gsub!(%r{^( [^ ].*: !ruby/object:)HTTP::Cookie$}) { $1 + 'Mechanize::Cookie' } yaml.gsub!(%r{^( expires: )(?:|!!null|(.+?)) *$}) { $1 + ($2 ? Time.parse($2).httpdate : '') } output.write yaml self end |