Module: PacTrac::Http::Cookie

Defined in:
lib/pactrac/http/cookie.rb

Class Method Summary collapse

Class Method Details

.from_raw(raw) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/pactrac/http/cookie.rb', line 12

def from_raw(raw)
  cookies = {}
  raw.get_fields('Set-Cookie').each do |c|
    key, value = from_response_header_value(c)
    cookies[key] = value
  end
  cookies
end

.from_request_header_value(header_value) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/pactrac/http/cookie.rb', line 48

def from_request_header_value(header_value)
  header_value.split(/\s*;\s*/).reject{ |v| v.strip == '' }.map { |p| 
    p.split(/\s*=\s*/)[0,2].map {|t| URI.unescape(t) }
  }.inject({}) { |hash, (key, value)|
    hash[key] = value
    hash
  }
end

.from_response(response) ⇒ Object



8
9
10
# File 'lib/pactrac/http/cookie.rb', line 8

def from_response(response)
  from_raw(response.raw)
end

.from_response_header_value(header_value) ⇒ Object



21
22
23
24
# File 'lib/pactrac/http/cookie.rb', line 21

def from_response_header_value(header_value)
  header_value.split(/\s*;\s*/)[0].split(/\s*=\s*/)[0,2].map {|t|
    URI.unescape(t) }
end

.to_request_header(cookies) ⇒ Object



44
45
46
# File 'lib/pactrac/http/cookie.rb', line 44

def to_request_header(cookies)
  "Cookie: #{to_request_header_value(cookies)}"
end

.to_request_header_value(cookies) ⇒ Object



38
39
40
41
42
# File 'lib/pactrac/http/cookie.rb', line 38

def to_request_header_value(cookies)
  cookies.map { |key, value|
    "#{URI.escape(key, ',;=')}=#{URI.escape(value, ',;=')}"
  }.join('; ')
end

.update_from_raw(raw, cookies) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/pactrac/http/cookie.rb', line 30

def update_from_raw(raw, cookies)
  c = cookies.clone
  from_raw(raw).each do |key, value|
    c[key] = value
  end
  c
end

.update_from_response(response, cookies) ⇒ Object



26
27
28
# File 'lib/pactrac/http/cookie.rb', line 26

def update_from_response(response, cookies)
  update_from_raw(response.raw, cookies)
end