Class: DhEasy::Core::Helper::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/dh_easy/core/helper/cookie.rb

Overview

Helper used for lower level cookie management.

Class Method Summary collapse

Class Method Details

.encode_to_header(cookie_hash) ⇒ String

Encode cookies as request cookie string.

Examples:

cookie_hash = {
  'aaa' => 111,
  'bbb' => 222
}
encode_to_header cookie_hash
# => 'aaa=111; bbb=222'

Parameters:

  • cookie_hash (Hash)

    Hash with cookies.

Returns:

  • (String)


140
141
142
# File 'lib/dh_easy/core/helper/cookie.rb', line 140

def encode_to_header cookie_hash
  cookie_hash.map{|k,v| "#{k}=#{v}"}.join '; '
end

.include?(base_cookie_hash, cookie_hash) ⇒ Boolean

Compare if cookie is included into base cookie

Examples:

Check a success match.

base_cookie_hash = {
  'aaa' => 111,
  'bbb' => 222,
  'ccc' => 333,
  'ddd' => 444
}
cookie_hash = {
  'bbb' => 222,
  'ddd' => 444
}
include? base_cookie_hash, cookie_hash
# => true

Check with fail match.

base_cookie_hash = {
  'aaa' => 111,
  'bbb' => 222,
  'ccc' => 333,
  'ddd' => 444
}
cookie_hash = {
  'bbb' => 555,
  'ddd' => 444
}
include? base_cookie_hash, cookie_hash
# => false

Parameters:

  • base_cookie_hash (Hash)

    Hash that represent universe.

  • cookie_hash (Hash)

    Hash that represents to compare.

Returns:

  • (Boolean)


199
200
201
202
203
204
# File 'lib/dh_easy/core/helper/cookie.rb', line 199

def include? base_cookie_hash, cookie_hash
  cookie_hash.each do |key, value|
    return false unless base_cookie_hash.has_key?(key) && base_cookie_hash[key] == value
  end
  true
end

.parse_from_request(cookies, cookie_hash = {}) ⇒ Hash

Parse request cookies on different formats.

Examples:

Parse from string.

parse_from_request 'aaa=111; bbb=222'
# => {'aaa' => 111, 'bbb' => 222}

Parse from array.

cookies = [
  'aaa=111',
  'bbb=222'
]
parse_from_response cookies
# => {'aaa' => 111, 'bbb' => 222}

Parse with `cookie_hash`.

cookie_hash = {'ccc' => 333}
parse_from_request 'aaa=111; bbb=222', cookie_hash
cookie_hash
# => {'aaa' => 1, 'bbb' => 2, 'ccc' => 333}

Parameters:

  • cookies (String, Hash, Array)

    Cookies to parse.

  • cookie_hash (Hash) (defaults to: {})

    ({}) External hash to store parsed cookies.

Returns:

  • (Hash)


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 cookies, cookie_hash = {}
  # Retrieve from hash
  if cookies.is_a? Hash
    cookie_hash.merge! cookies
    return cookie_hash
  end

  # Extract from string
  cookies = cookies.split '; ' if cookies.is_a? String

  # Extract from array
  cookies&.each do |raw_cookie|
    key, value = raw_cookie.split('=', 2)
    cookie_hash[key] = value
  end
  cookie_hash
end

.parse_from_response(cookies, cookie_hash = {}) ⇒ Hash

Parse response cookies on different formats.

Examples:

Parse from string

parse_from_response 'aaa=111; bbb=222'
# => {'aaa' => 111, 'bbb' => 222}

Parse from array.

cookies = [
  'aaa=111; Expires=Thu, Jan 01 1970 00:00:00 UTC; path=/',
  'bbb=222; path=/',
  'ccc=333; path=/; expires=Wed, Jan 01 3000 00:00:00 UTC'
]
parse_from_response cookies
# => {'bbb' => 222, 'ccc' => 333}

Parse with `cookie_hash`.

cookie_hash = {'ccc' => 333}
parse_from_response 'aaa=111; bbb=222', cookie_hash
cookie_hash
# => {'aaa' => 111, 'bbb' => 222, 'ccc' => 333}

Parameters:

  • cookies (String, Hash, Array)

    Cookies to parse.

  • cookie_hash (Hash) (defaults to: {})

    ({}) External hash to store parsed cookies.

Returns:

  • (Hash)


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 cookies, cookie_hash = {}
  # Retrieve from hash
  if cookies.is_a? Hash
    cookie_hash.merge! cookies
    return cookie_hash
  end
  # Retrieve from String
  cookies = cookies.split '; ' if cookies.is_a? String

  # Extract from array
  info = cookie = expires = key = value = nil
  cookies&.each do |raw_cookie|
    # Extract cookie data
    key_pair = raw_cookie.scan(/(?:;\s+([^\=]+)=([^;]*))/i) || []
    cookie = key_pair.inject(Hash.new){|h,i|h[i[0].to_s.downcase] = i[1]; h}
    cookie[:key], cookie[:value] = raw_cookie.match(/^\s*(?<key>[^\=]+)\=(?<value>[^;]*)/i)&.captures

    # Check cookie expire
    expires = cookie['expires'].nil? ? nil : Time.parse(cookie['expires'])
    if !expires.nil? && Time.now > expires
      cookie_hash.delete cookie[:key]
      next
    end

    # Save cookie
    cookie_hash[cookie[:key]] = cookie[:value]
  end
  cookie_hash
end

.update(request_cookies, response_cookies) ⇒ String

Apply request and response cookies as a string with request format.

Examples:

request_cookies = 'aaa=111; ddd=444'
response_cookies = [
  'aaa=111; Expires=Thu, Jan 01 1970 00:00:00 UTC; path=/',
  'bbb=222; path=/',
  'ccc=333; path=/; expires=Wed, Jan 01 3000 00:00:00 UTC'
]
update_as_hash , response_cookies
# => 'bbb=222; ccc=333; ddd=444'

Parameters:

  • request_cookies (String, Array, Hash)

    Cookies to parse.

  • response_cookies (String, Array, Hash)

    Cookies to parse.

Returns:

  • (String)


160
161
162
163
# File 'lib/dh_easy/core/helper/cookie.rb', line 160

def update request_cookies, response_cookies
  cookie_hash = update_as_hash request_cookies, response_cookies
  encode_to_header cookie_hash
end

.update_as_hash(request_cookies, response_cookies) ⇒ Hash

Apply request and response cookies as a hash.

Examples:

request_cookies = 'aaa=111; ddd=444'
response_cookies = [
  'aaa=111; Expires=Thu, Jan 01 1970 00:00:00 UTC; path=/',
  'bbb=222; path=/',
  'ccc=333; path=/; expires=Wed, Jan 01 3000 00:00:00 UTC'
]
update_as_hash , response_cookies
# => {'bbb' => 222, 'ccc' => 333, 'ddd' => 444}

Parameters:

  • request_cookies (String, Array, Hash)

    Cookies to parse.

  • response_cookies (String, Array, Hash)

    Cookies to parse.

Returns:

  • (Hash)


120
121
122
123
124
125
# File 'lib/dh_easy/core/helper/cookie.rb', line 120

def update_as_hash request_cookies, response_cookies
  cookie_hash = {}
  parse_from_request request_cookies, cookie_hash
  parse_from_response response_cookies, cookie_hash
  cookie_hash
end