Sha256: 10f931c9a6d31a793d503b3ff733f3397ce81827ad478428b665b027ba17ac20

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

require 'time'
require 'base64'

require 'concord'
require 'adamantium'
require 'abstract_type'
require 'lupo'

# Models an HTTP Cookie
class Cookie

  # An empty frozen array
  EMPTY_ARRAY = [].freeze

  # An empty frozen hash
  EMPTY_HASH = {}.freeze

  # An empty frozen string
  EMPTY_STRING = ''.freeze

  # Separates the cookie name from its value
  NAME_VALUE_SEPARATOR = '='.freeze

  # Separates cookies
  COOKIE_SEPARATOR = '; '.freeze

  # Separates ruby class names in a FQN
  DOUBLE_COLON = '::'.freeze

  # Helper for cookie deletion
  class Empty < self
    def initialize(name)
      super(name, nil)
    end
  end

  # Namespace for cookie encoders
  module Encoder
    Base64 = ->(string) { ::Base64.urlsafe_encode64(string) }
  end

  # Namespace for cookie decoders
  module Decoder
    Base64 = ->(string) { ::Base64.urlsafe_decode64(string) }
  end

  # Cookie error base class
  Error = Class.new(StandardError)

  include Concord::Public.new(:name, :value)
  include Adamantium::Flat

  def self.coerce(string)
    new(*string.split(NAME_VALUE_SEPARATOR, 2))
  end

  def encode(encoder = Encoder::Base64)
    new(encoder.call(value))
  end

  def decode(decoder = Decoder::Base64)
    new(decoder.call(value))
  end

  def encrypt(box)
    new(box.encrypt(value))
  end

  def decrypt(box)
    new(box.decrypt(value))
  end

  def to_s
    "#{name}=#{value}"
  end
  memoize :to_s

  private

  def new(new_value)
    self.class.new(name, new_value)
  end

end # class Cookie

require 'cookie/header'
require 'cookie/header/attribute'
require 'cookie/registry'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
majoun-0.0.4 lib/cookie.rb
majoun-0.0.3 lib/cookie.rb
majoun-0.0.2 lib/cookie.rb