Class: CookieJar::Jar

Inherits:
Object
  • Object
show all
Defined in:
lib/nerdz/http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_ns(string) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nerdz/http.rb', line 29

def self.from_ns(string)
  jar = self.new

  string.each_line {|l|
    if l =~ (/^((?:^[^^]+))\s+(TRUE|FALSE)\s+(\S+)\s+(TRUE|FALSE)\s+(\d+)\s+(\S+)\s+(\S+)\s*$/)
      jar.add_cookie Cookie.new(domain: $1, path: $3, secure: $4 == 'TRUE' ? true : false,
                 expires_at: Time.at($5.to_i), name: $6, value: $7)
    end
  }

  jar
end

.load_ns(file) ⇒ Object



25
26
27
# File 'lib/nerdz/http.rb', line 25

def self.load_ns(file)
  self.from_ns(File.read(file))
end

Instance Method Details

#[](name) ⇒ Object



58
59
60
61
62
# File 'lib/nerdz/http.rb', line 58

def [](name)
  self.get_cookies('http://www.megaupload.com/').select {|cookie|
    cookie.name == name
  }.first
end

#save_ns(file) ⇒ Object



52
53
54
55
56
# File 'lib/nerdz/http.rb', line 52

def save_ns(file)
  File.open(file, 'w') {|f|
    f.write(self.to_ns)
  }
end

#to_nsObject



42
43
44
45
46
47
48
49
50
# File 'lib/nerdz/http.rb', line 42

def to_ns
  "# Netscape HTTP Cookie File\n" +
  "# http://curl.haxx.se/rfc/cookie_spec.html\n" +
  "# This file was generated by nerdz! Edit at your own risk.\n\n" +
  to_a.map {|cookie|
    "#{cookie.domain}\t#{cookie.domain[0] == ?. ? 'TRUE' : 'FALSE'}\t#{cookie.path} #{cookie.secure ? 'TRUE' : 'FALSE'}"+
      "\t#{cookie.expires_at.to_i}\t#{cookie.name}\t#{cookie.value}\n" if !cookie.session?
  }.compact.join
end