Class: Mbrowser::Cookie
- Inherits:
-
Object
- Object
- Mbrowser::Cookie
- Defined in:
- lib/mbrowser/cookie.rb
Constant Summary collapse
- COOKIE_FILE =
'/tmp/mbrowser_cookie.yml'
Class Method Summary collapse
- .dump_cookie_to_file ⇒ Object
- .export_cookies(domain) ⇒ Object
- .import_cookies(curl) ⇒ Object
- .load_cookie_from_disk ⇒ Object
Class Method Details
.dump_cookie_to_file ⇒ Object
11 12 13 |
# File 'lib/mbrowser/cookie.rb', line 11 def 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 domain if $session_cookies.empty? = $session_cookies.select{ |suffix_domain,value| domain.include? suffix_domain}.inject({}){|m,n| m.merge!(n[1]);m} = .clone.keys.map{ |key| "#{[key].empty? ? "" : "#{key}=#{[key]};"}" }.join(" ") 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 curl = curl.header_str.split("\r\n").map{|v| v if v =~ /^Set-Cookie:.*/ }.compact = .map do |v| v = v[11..-1] = v.split(";").inject({}){|m,item| m.merge!({item.split("=")[0].strip.to_sym=>item.split("=")[1..-1].join("=").to_s});m} domain = [:domain] domain ||= curl.domain .delete(:domain) .delete(:expires) .delete(:path) .delete(:HttpOnly) $session_cookies[domain] ||= {} .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 end |
.load_cookie_from_disk ⇒ Object
7 8 9 |
# File 'lib/mbrowser/cookie.rb', line 7 def $session_cookies = YAML::load_file(COOKIE_FILE) rescue {} end |