Class: LibWebSocket::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/libwebsocket/cookie.rb,
lib/libwebsocket/cookie/request.rb,
lib/libwebsocket/cookie/response.rb

Overview

A base class for LibWebSocket::Cookie::Request and LibWebSocket::Cookie::Response.

Direct Known Subclasses

Request, Response

Defined Under Namespace

Classes: Request, Response

Constant Summary collapse

TOKEN =

Cookie token

/[^;,\s"]+/
NAME =

Cookie name

/[^;,\s"=]+/
QUOTED_STRING =

Cookie quoted value

/"(?:\\"|[^"])+"/
VALUE =

Cookie unquoted value

/(?:#{TOKEN}|#{QUOTED_STRING})/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Cookie

Returns a new instance of Cookie.



15
16
17
18
19
# File 'lib/libwebsocket/cookie.rb', line 15

def initialize(hash = {})
  hash.each do |k,v|
    instance_variable_set("@#{k}",v)
  end
end

Instance Attribute Details

#pairsObject

Returns the value of attribute pairs.



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

def pairs
  @pairs
end

Instance Method Details

#parse(string = nil) ⇒ Object

Parse cookie string to array



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/libwebsocket/cookie.rb', line 22

def parse(string = nil)
  self.pairs = []

  return if string.nil? || string == ''

  while string.slice!(/\s*(#{NAME})\s*(?:=\s*(#{VALUE}))?;?/)
    attr, value = $1, $2
    if !value.nil?
      value.gsub!(/^"/, '')
      value.gsub!(/"$/, '')
      value.gsub!(/\\"/, '"')
    end
    self.pairs.push([attr, value])
  end

  return self
end

#to_sObject

Convert cookie array to string



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/libwebsocket/cookie.rb', line 41

def to_s
  pairs = []

  self.pairs.each do |pair|
    string = ''
    string += pair[0]

    unless pair[1].nil?
      string += '='
      string += (!pair[1].match(/^#{VALUE}$/) ? "\"#{pair[1]}\"" : pair[1])
    end

    pairs.push(string)
  end

  return pairs.join("; ")
end