Sha256: c7f33ba7b21c75af5ca1d3ddb160228146fc66ce5f0cd305d87ac5df2266ae48

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

class RubyForge
  class CookieManager
    class << self
      def load(path)
        cm = YAML.load_file(path) rescue CookieManager.new(path)
        cm = CookieManager.new(path) unless cm.is_a?(CookieManager)
        cm.clean_stale_cookies
      end
    end

    attr_accessor :cookies_file
    def initialize(cookies_file = nil)
      @jar = Hash.new { |hash,domain_name|
        hash[domain_name.downcase] = {}
      }
      @cookies_file = cookies_file
    end

    def [](uri)
      # FIXME we need to do more matching on hostname....  This is not
      # bulletproof
      @jar[uri.host.downcase]
    end

    def empty?
      @jar.empty? || @jar.all? { |k,v| v.empty? }
    end

    def save!
      File.open(@cookies_file, 'wb') { |f|
        f.write(YAML.dump(self))
      }
    end

    def add(uri, cookie)
      no_dot_domain = cookie.domain.gsub(/^\./, '')
      return unless uri.host =~ /#{no_dot_domain}$/i
      @jar[no_dot_domain][cookie.name] = cookie
      clean_stale_cookies
    end

    def clean_stale_cookies
      @jar.each do |domain, cookies|
        cookies.each do |name, cookie|
          cookies.delete(name) if cookie.expires < Time.now
        end
      end
      self
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
mack-0.8.3 lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb
mack-0.8.3.1 lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb
rubyforge-1.0.1 lib/rubyforge/cookie_manager.rb
rubyforge-1.0.0 lib/rubyforge/cookie_manager.rb