class Cookie

Attributes

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

Public Class Methods

new(data = {}) click to toggle source
# File lib/parsers/http/cookie.rb, line 6
def initialize(data = {})
  @name      = data[:name]      if data.key? :name
  @value     = data[:value]     if data.key? :value
  @expires   = data[:expires]   if data.key? :expires
  @max_age   = data[:max_age]   if data.key? :max_age
  @path      = data[:path]      if data.key? :path
  @domain    = data[:domain]    if data.key? :domain
  @secure    = data[:secure]    if data.key? :secure
  @http_only = data[:http_only] if data.key? :http_only
end
parse(raw_value, options = {}) click to toggle source
# File lib/parsers/http/cookie.rb, line 44
def self.parse(raw_value, options = {})
  self.new Parser.new(options).parse!(raw_value).to_h
end

Public Instance Methods

age_left() click to toggle source
# File lib/parsers/http/cookie.rb, line 17
def age_left
  return @max_age if @max_age.is_a? Fixnum
  return (@max_age - Time.now).to_i
end
to_h() click to toggle source
# File lib/parsers/http/cookie.rb, line 33
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
to_s() click to toggle source
# File lib/parsers/http/cookie.rb, line 22
def to_s
  cookie = ["#{@name}=#{@value}"]
  cookie << "Expires=#{@expires}" if defined?(@expires) and !defined?(@max_age)
  cookie << "Max-Age=#{@max_age == Time.at(0) ? 0 : self.age_left }" if defined? @max_age
  cookie << "Domain=#{@domain}"   if defined? @domain
  cookie << "Path=#{@path}"       if defined? @path
  cookie << "Secure"              if defined? @secure
  cookie << "HttpOnly"            if defined? @http_only
  return cookie.join('; ')
end