Class: Mbrowser::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/mbrowser/cookie.rb

Constant Summary collapse

'/tmp/mbrowser_cookie.yml'

Class Method Summary collapse

Class Method Details



11
12
13
# File 'lib/mbrowser/cookie.rb', line 11

def dump_cookie_to_file
	File.open(COOKIE_FILE, 'w') {|f| f.write $session_cookies.to_yaml } 
end

.export_cookies(domain) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/mbrowser/cookie.rb', line 15

def export_cookies domain
	load_cookie_from_disk if $session_cookies.empty?
	cookie_store = $session_cookies.select{ |suffix_domain,value| domain.include? suffix_domain}.inject({}){|m,n| m.merge!(n[1]);m}
	cookies_str = cookie_store.clone.keys.map{ |key|
	 "#{cookie_store[key].empty? ? "" : "#{key}=#{cookie_store[key]};"}"
	  }.join(" ")
	cookies_str  
end

.import_cookies(curl) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mbrowser/cookie.rb', line 24

def import_cookies curl
	cookie_headers= curl.header_str.split("\r\n").map{|v|  v if v =~ /^Set-Cookie:.*/ }.compact
	
	cookie_hashs = cookie_headers.map do |v| 
		v = v[11..-1]
		cookie_hash = v.split(";").inject({}){|m,item| m.merge!({item.split("=")[0].strip.to_sym=>item.split("=")[1..-1].join("=").to_s});m}
		domain = cookie_hash[:domain]
		domain ||= curl.domain
		cookie_hash.delete(:domain)
	  cookie_hash.delete(:expires)
	  cookie_hash.delete(:path)
	  cookie_hash.delete(:HttpOnly)
	  $session_cookies[domain] ||= {}
	  cookie_hash.each do |key,value|
	  	if value.to_s.downcase.strip != "deleted"
				$session_cookies[domain][key] = value.to_s
	  	else
				$session_cookies[domain].delete key
	  	end
	  end
	end
  dump_cookie_to_file
end


7
8
9
# File 'lib/mbrowser/cookie.rb', line 7

def load_cookie_from_disk
	$session_cookies = YAML::load_file(COOKIE_FILE) rescue {}
end