Class: HTTP::CookieJar::YAMLSaver

### Home Classes Methods

### In Files

- lib/http/cookie_jar/yaml_saver.rb

### Parent

HTTP::CookieJar::AbstractSaver

### Methods

- ::new
- #load
- #save

### Files

- LICENSE.txt

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Cookie
- HTTP::Cookie::Scanner
- HTTP::CookieJar
- HTTP::CookieJar::AbstractSaver
- HTTP::CookieJar::AbstractStore
- HTTP::CookieJar::CookiestxtSaver
- HTTP::CookieJar::HashStore
- HTTP::CookieJar::MozillaStore
- HTTP::CookieJar::YAMLSaver
- Array
- Hash
- String

No matching classes.

# HTTP::CookieJar::YAMLSaver

YAMLSaver saves and loads cookies in the YAML format. It can load a YAML file saved by Mechanize, but the saving format is not compatible with older versions of Mechanize (< 2.7).

### Public Class Methods

new(**options) click to toggle source

There is no option keyword supported at the moment.

    # File lib/http/cookie_jar/yaml_saver.rb, line 16

### Public Instance Methods

load(io, jar) click to toggle source

    # File lib/http/cookie_jar/yaml_saver.rb, line 22
    def load(io, jar)
      begin
        data = YAML.load(io)
      rescue ArgumentError => e
        case e.message
        when %{\Aundefined class/module Mechanize::}
          # backward compatibility with Mechanize::Cookie
          begin
            io.rewind # hopefully
            yaml = io.read
            # a gross hack
            yaml.gsub!(%{^(    [^ ].*:) !ruby/object:Mechanize::Cookie$}, "\\1")
            data = YAML.load(yaml)
          rescue Errno::ESPIPE
            @logger.warn "could not rewind the stream for conversion" if @logger
          rescue ArgumentError
          end
        end
      end

      case data
      when Array
        data.each { |cookie|
          jar.add(cookie)
        }
      when Hash
        # backward compatibility with Mechanize::Cookie
        data.each { |domain, paths|
          paths.each { |path, names|
            names.each { |cookie_name, cookie_hash|
              if cookie_hash.respond_to?(:ivars)
                # YAML::Object of Syck
                cookie_hash = cookie_hash.ivars
              end
              cookie = HTTP::Cookie.new({}.tap { |hash|
                  cookie_hash.each_pair { |key, value|
                    hash[key.to_sym] = value
                  }
                })
              jar.add(cookie)
            }
          }
        }
      else
        @logger.warn "incompatible YAML cookie data discarded" if @logger
        return
      end
    end

save(io, jar) click to toggle source

    # File lib/http/cookie_jar/yaml_saver.rb, line 18
    def save(io, jar)
      YAML.dump(@session ? jar.to_a : jar.reject(&:session?), io)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.