Class: CookieHash

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cookie_hash.rb

Overview

class used for handling cookies from http requests

Constant Summary collapse

CLIENT_COOKIES =

constant that is used for filtering the cookie data (@hash) from unwanted values

%w[path expires domain path secure HTTPOnly HttpOnly].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ void

Initializes the instance with empty hash by default

Parameters:

  • hash (Hash) (defaults to: {})

    The Hash that will contain the data about the cookie


16
17
18
# File 'lib/cookie_hash.rb', line 16

def initialize(hash = {})
  @hash = hash
end

Instance Attribute Details

#hashHash

Returns The Hash that will contain the data about the cookie.

Returns:

  • (Hash)

    The Hash that will contain the data about the cookie


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cookie_hash.rb', line 6

class CookieHash
  extend Forwardable

  # constant that is used for filtering the cookie data (@hash) from unwanted values
  CLIENT_COOKIES = %w[path expires domain path secure HTTPOnly HttpOnly].freeze

  # Initializes the instance with empty hash by default
  #
  # @param [Hash] hash The Hash that will contain the data about the cookie
  # @return [void]
  def initialize(hash = {})
    @hash = hash
  end

  # parses the value received, if is is a hash, will merge it with the @hash instance, otherwise, will parse
  # the string by splitting it and assigning the keys and the values to the hash
  #
  # @param [Hash, String] value The value that will be parsed and merged into the hash
  # @return [void]
  def add_cookies(value)
    case value
      when Hash
        @hash.merge!(value)
      when String
        value.split('; ').each do |cookie|
          array = cookie.split('=')
          @hash[array[0].to_sym] = array[1]
        end
      else
        raise 'add_cookies only takes a Hash or a String'
    end
  end

  # returns the expire time of the cookie that has been already parsed
  #
  # @return [Time] returns the expire time of the cookie that has been parsed
  def expire_time
    DateTime.parse(@hash[:expires]).in_time_zone
  end

  # returns the cookie value as a String, filtering unwanted values
  #
  # @return [String] returns cookie value, filtering the unwanted values
  def to_cookie_string
    @hash.delete_if { |key, _value| CLIENT_COOKIES.include?(key.to_s) }.map { |key, value| "#{key}=#{value}" }.join('; ')
  end
end

Instance Method Details

#add_cookies(value) ⇒ void

This method returns an undefined value.

parses the value received, if is is a hash, will merge it with the @hash instance, otherwise, will parse the string by splitting it and assigning the keys and the values to the hash

Parameters:

  • value (Hash, String)

    The value that will be parsed and merged into the hash


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cookie_hash.rb', line 25

def add_cookies(value)
  case value
    when Hash
      @hash.merge!(value)
    when String
      value.split('; ').each do |cookie|
        array = cookie.split('=')
        @hash[array[0].to_sym] = array[1]
      end
    else
      raise 'add_cookies only takes a Hash or a String'
  end
end

#expire_timeTime

returns the expire time of the cookie that has been already parsed

Returns:

  • (Time)

    returns the expire time of the cookie that has been parsed


42
43
44
# File 'lib/cookie_hash.rb', line 42

def expire_time
  DateTime.parse(@hash[:expires]).in_time_zone
end

returns the cookie value as a String, filtering unwanted values

Returns:

  • (String)

    returns cookie value, filtering the unwanted values


49
50
51
# File 'lib/cookie_hash.rb', line 49

def to_cookie_string
  @hash.delete_if { |key, _value| CLIENT_COOKIES.include?(key.to_s) }.map { |key, value| "#{key}=#{value}" }.join('; ')
end