Sha256: 52e812784619a3d47d40a1850538c263693823634ee826b71b602cf3791aada8

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'sitehub/cookie/attribute'
require 'sitehub/cookie/flag'
require 'sitehub/constants'
class SiteHub
  class Cookie
    attr_reader :attributes_and_flags, :name_attribute
    include Constants

    FIRST = 0

    def initialize(cookie_string)
      pairs = cookie_string.split(SEMICOLON).map do |entry|
        if entry.include?(EQUALS_SIGN)
          Cookie::Attribute.new(*entry.split(EQUALS_SIGN))
        else
          Cookie::Flag.new(entry)
        end
      end

      name_attribute = pairs.delete_at(FIRST)
      @attributes_and_flags = pairs
      @name_attribute = Cookie::Attribute.new(name_attribute.name.to_s, name_attribute.value)
    end

    def name
      name_attribute.name
    end

    def value
      name_attribute.value
    end

    def find(name)
      attributes_and_flags.find { |entry| entry.name == name }
    end

    def ==(other)
      other.is_a?(self.class) &&
        sorted_attributes_and_flags(attributes_and_flags) == sorted_attributes_and_flags(other.attributes_and_flags)
    end

    def sorted_attributes_and_flags(attributes_and_flags)
      attributes_and_flags.sort { |entry1, entry2| entry1.name <=> entry2.name }
    end

    def to_s
      [name_attribute].concat(attributes_and_flags).join(SEMICOLON_WITH_SPACE)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sitehub-0.4.3 lib/sitehub/cookie.rb