Class: Mechanize::CookieJar

### Home Classes Methods

### In Files

- lib/mechanize/cookie_jar.rb

### Parent

HTTP::CookieJar

### Methods

- #load
- #save

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::ElementNotFoundError
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::CookieJar

### Public Instance Methods

load(input, *options) click to toggle source

    # File lib/mechanize/cookie_jar.rb, line 120
    def load(input, *options)
      input.respond_to?(:write) or
        return 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 = YAML.load(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

save(output, *options) click to toggle source

    # File lib/mechanize/cookie_jar.rb, line 66
    def save(output, *options)
      output.respond_to?(:write) or
        return open(output, 'w') { |io| save(io, *options) }

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

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

      session = opthash[:session]
      nstore = HashStore.new

      each { |cookie|
        next if !session && cookie.session?

        if cookie.max_age
          cookie = cookie.dup
          cookie.expires = cookie.expires # convert max_age to expires
        end
        nstore.add(cookie)
      }

      yaml = YAML.dump(nstore.instance_variable_get(:@jar))

      # a gross hack
      yaml.gsub!(%{^(    [^ ].*: !ruby/object:)HTTP::Cookie$}) {
        $1 + 'Mechanize::Cookie'
      }
      yaml.gsub!(%{^(      expires: )(?:|!!null|(.+?)) *$}) {
        $1 + ($2 ? Time.parse($2).httpdate : '')
      }

      output.write yaml

      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.