class LibWebSocket::Cookie

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

Constants

NAME
QUOTED_STRING
TOKEN
VALUE

Attributes

pairs[RW]

Public Class Methods

new(hash = {}) click to toggle source
# File lib/libwebsocket/cookie.rb, line 15
def initialize(hash = {})
  hash.each do |k,v|
    instance_variable_set("@#{k}",v)
  end
end

Public Instance Methods

parse(string = nil) click to toggle source

Parse cookie string to array

# 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_s() click to toggle source

Convert cookie array to string

# 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