Class: DhEasy::Core::Helper::Cookie
- Inherits:
-
Object
- Object
- DhEasy::Core::Helper::Cookie
- Defined in:
- lib/dh_easy/core/helper/cookie.rb
Overview
Helper used for lower level cookie management.
Class Method Summary collapse
-
.encode_to_header(cookie_hash) ⇒ String
Encode cookies as request cookie string.
-
.include?(base_cookie_hash, cookie_hash) ⇒ Boolean
Compare if cookie is included into base cookie.
-
.parse_from_request(cookies, cookie_hash = {}) ⇒ Hash
Parse request cookies on different formats.
-
.parse_from_response(cookies, cookie_hash = {}) ⇒ Hash
Parse response cookies on different formats.
-
.update(request_cookies, response_cookies) ⇒ String
Apply request and response cookies as a string with request format.
-
.update_as_hash(request_cookies, response_cookies) ⇒ Hash
Apply request and response cookies as a hash.
Class Method Details
.encode_to_header(cookie_hash) ⇒ String
Encode cookies as request cookie string.
140 141 142 |
# File 'lib/dh_easy/core/helper/cookie.rb', line 140 def encode_to_header .map{|k,v| "#{k}=#{v}"}.join '; ' end |
.include?(base_cookie_hash, cookie_hash) ⇒ Boolean
Compare if cookie is included into base cookie
199 200 201 202 203 204 |
# File 'lib/dh_easy/core/helper/cookie.rb', line 199 def include? , .each do |key, value| return false unless .has_key?(key) && [key] == value end true end |
.parse_from_request(cookies, cookie_hash = {}) ⇒ Hash
Parse request cookies on different formats.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dh_easy/core/helper/cookie.rb', line 31 def parse_from_request , = {} # Retrieve from hash if .is_a? Hash .merge! return end # Extract from string = .split '; ' if .is_a? String # Extract from array &.each do || key, value = .split('=', 2) [key] = value end end |
.parse_from_response(cookies, cookie_hash = {}) ⇒ Hash
Parse response cookies on different formats.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dh_easy/core/helper/cookie.rb', line 74 def parse_from_response , = {} # Retrieve from hash if .is_a? Hash .merge! return end # Retrieve from String = .split '; ' if .is_a? String # Extract from array info = = expires = key = value = nil &.each do || # Extract cookie data key_pair = .scan(/(?:;\s+([^\=]+)=([^;]*))/i) || [] = key_pair.inject(Hash.new){|h,i|h[i[0].to_s.downcase] = i[1]; h} [:key], [:value] = .match(/^\s*(?<key>[^\=]+)\=(?<value>[^;]*)/i)&.captures # Check cookie expire expires = ['expires'].nil? ? nil : Time.parse(['expires']) if !expires.nil? && Time.now > expires .delete [:key] next end # Save cookie [[:key]] = [:value] end end |
.update(request_cookies, response_cookies) ⇒ String
Apply request and response cookies as a string with request format.
160 161 162 163 |
# File 'lib/dh_easy/core/helper/cookie.rb', line 160 def update , = update_as_hash , encode_to_header end |
.update_as_hash(request_cookies, response_cookies) ⇒ Hash
Apply request and response cookies as a hash.
120 121 122 123 124 125 |
# File 'lib/dh_easy/core/helper/cookie.rb', line 120 def update_as_hash , = {} parse_from_request , parse_from_response , end |