Class: Browser::Cookies

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/browser/cookies.rb

Overview

Allows manipulation of browser cookies.

Usage:

cookies = Browser::Cookies.new(document) cookies["my-cookie"] = "monster" cookies.delete("my-cookie")

Constant Summary collapse

DEFAULT =

Default cookie options.

{
  expires: Time.now + 60 * 60 * 24,
  secure:  false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Cookies

Create a new Browser::Cookies wrapper.

Parameters:

  • document (native)

    the native document object


29
30
31
32
# File 'opal/browser/cookies.rb', line 29

def initialize(document)
  @document = document
  @options  = DEFAULT.dup
end

Instance Attribute Details

#keysArray<String> (readonly)

Returns all the cookie names.

Returns:

  • (Array<String>)

    all the cookie names


77
78
79
80
81
# File 'opal/browser/cookies.rb', line 77

def keys
  Array(`#@document.cookie.split(/; /)`).map {|cookie|
    cookie.split(/\s*=\s*/).first
  }
end

#optionsObject (readonly)

Returns the value of attribute options


24
25
26
# File 'opal/browser/cookies.rb', line 24

def options
  @options
end

#valuesArray (readonly)

Returns all the cookie values.

Returns:

  • (Array)

    all the cookie values


85
86
87
88
89
# File 'opal/browser/cookies.rb', line 85

def values
  keys.map {|key|
    self[key]
  }
end

Instance Method Details

#[](name) ⇒ Object

Access the cookie with the given name.

Parameters:

  • name (String)

    the name of the cookie

Returns:


39
40
41
42
43
44
45
46
47
48
49
# File 'opal/browser/cookies.rb', line 39

def [](name)
  matches = `#@document.cookie`.scan(/#{Regexp.escape(FormData.encode(name))}=([^;]*)/)

  return if matches.empty?

  result = matches.flatten.map {|value|
    JSON.parse(FormData.decode(value))
  }

  result.length == 1 ? result.first : result
end

#[]=(name, value, options = {}) ⇒ Object

Set a cookie.

Parameters:

  • name (String)

    the name of the cookie

  • value (Object)

    the data to set

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

    the options for the cookie

Options Hash (options):

  • :max_age (Integer)

    the max age of the cookie in seconds

  • :expires (Time)

    the expire date

  • :path (String)

    the path the cookie is valid on

  • :domain (String)

    the domain the cookie is valid on

  • :secure (Boolean)

    whether the cookie is secure or not


62
63
64
65
66
# File 'opal/browser/cookies.rb', line 62

def []=(name, value, options = {})
  string = value.is_a?(String) ? value : JSON.dump(value)
  encoded_value = encode(name, string, @options.merge(options))
  `#@document.cookie = #{encoded_value}`
end

#clearself

Delete all the cookies

Returns:

  • (self)

110
111
112
113
114
115
116
# File 'opal/browser/cookies.rb', line 110

def clear
  keys.each {|key|
    delete key
  }

  self
end

#delete(name) ⇒ Object

Delete a cookie.

Parameters:

  • name (String)

    the name of the cookie


71
72
73
# File 'opal/browser/cookies.rb', line 71

def delete(name)
  `#@document.cookie = #{encode name, '', expires: Time.now}`
end

#each {|key, value| ... } ⇒ self

Enumerate the cookies.

Yield Parameters:

  • key (String)

    the name of the cookie

  • value (String)

    the value of the cookie

Returns:

  • (self)

97
98
99
100
101
102
103
104
105
# File 'opal/browser/cookies.rb', line 97

def each(&block)
  return enum_for :each unless block

  keys.each {|key|
    yield key, self[key]
  }

  self
end