class Cookie::Parser

Attributes

domain[R]
expires[R]
http_only[R]
max_age[R]
name[R]
path[R]
secure[R]
value[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/parsers/http/cookie.rb, line 51
def initialize(options = {})
  @time         = options[:time]
  @default_path = options[:default_path] || ''
end

Public Instance Methods

attribute!(chunk) click to toggle source
# File lib/parsers/http/cookie.rb, line 98
def attribute!(chunk)
  key = chunk[0].downcase

  case key
  when 'expires'  then self.expires! chunk[1]
  when 'max-age'  then self.max_age! chunk[1]
  when 'domain'   then self.domain!  chunk[1]
  when 'path'     then self.path!    chunk[1]
  when 'secure'   then @secure    = true
  when 'httponly' then @http_only = true
  end
end
clear!() click to toggle source
# File lib/parsers/http/cookie.rb, line 67
def clear!
  %w(@name @value @expires @max_age @domain @path @secure @http_only).each do |var|
    if self.instance_variable_defined? var
      self.remove_instance_variable var
    end
  end
end
domain!(val) click to toggle source
# File lib/parsers/http/cookie.rb, line 131
def domain!(val)
  raise ParserError.new 'Domain cannot have a blank value' if val.empty?
  @domain = (val[0] == '.' ? val[1..-1] : val).downcase
end
expires!(val) click to toggle source
# File lib/parsers/http/cookie.rb, line 111
def expires!(val)
  raise ParserError.new 'Expires cannot have a blank value' if val.empty?
  @expires = val
end
max_age!(val) click to toggle source
# File lib/parsers/http/cookie.rb, line 116
def max_age!(val)
  raise ParserError.new 'Max-Age cannot have a blank value' if val.empty?

  unless val =~ /^-?\d+$/
    raise ParserError.new "Expected integer for Max-Age instead of #{val}"
  end

  val = val.to_i
  if val <= 0
    @max_age = Time.at(0)
  else
    @max_age = (@time.nil? ? val : @time + val)
  end
end
name_and_value!(chunk) click to toggle source
# File lib/parsers/http/cookie.rb, line 89
def name_and_value!(chunk)
  raise ParserError.new "Name cannot be blank"             if chunk.nil? or chunk.empty?
  raise ParserError.new "Name/value pair must include '='" if chunk.size == 1
  @name  = chunk[0]
  @value = chunk[1]

  raise ParserError.new "Name cannot be blank" if @name.empty?
end
parse!(raw_value = nil) click to toggle source
# File lib/parsers/http/cookie.rb, line 75
def parse!(raw_value = nil)
  raise ParserError.new "nil is unparseable" if raw_value.nil?
  self.clear!

  chunks = raw_value.to_s.split ';'
  chunks.map! do |c|
    si = c.index '='
    si.nil? ? [c.strip] : [ c[0...si].strip, c[si + 1..-1].strip ]
  end
  self.name_and_value! chunks.shift
  chunks.each {|c| self.attribute! c }
  return self
end
path!(val) click to toggle source
# File lib/parsers/http/cookie.rb, line 136
def path!(val)
  if val.empty? || val[0] != '/'
    @path = @default_path
  else
    @path = val
  end
end
to_h() click to toggle source
# File lib/parsers/http/cookie.rb, line 56
def to_h
  h = { name: @name, value: @value }
  h[:expires]   = @expires   if defined?(@expires) and !defined?(@max_age)
  h[:max_age]   = @max_age   if defined? @max_age
  h[:domain]    = @domain    if defined? @domain
  h[:path]      = @path      if defined? @path
  h[:secure]    = @secure    if defined? @secure
  h[:http_only] = @http_only if defined? @http_only
  return h
end