Class: Browser::Cookies

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

Overview

Allows manipulation of browser cookies.

Usage:

cookies = $document.cookies cookies["my-cookie"] = "monster" cookies.delete("my-cookie")

By default, cookies are stored JSON-encoded. You can supply a raw: option whenever you need to access/write the cookies in a raw way, eg.

cookies["my-other-cookie", raw: true] = 123

You can also set this option while referencing $document.cookies, eg.

cookies = $document.cookies(raw: true) cookies["my-other-cookie"] = 123

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, options = {}) ⇒ Cookies

Create a new Browser::Cookies wrapper.

Parameters:

  • document (native)

    the native document object

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

    the default cookie options



38
39
40
41
# File 'opal/browser/cookies.rb', line 38

def initialize(document, options = {})
  @document = document
  @options  = DEFAULT.merge(options)
end

Instance Attribute Details

#keysArray<String> (readonly)

Returns all the cookie names.

Returns:

  • (Array<String>)

    all the cookie names



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

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

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'opal/browser/cookies.rb', line 32

def options
  @options
end

#valuesArray (readonly)

Returns all the cookie values.

Returns:

  • (Array)

    all the cookie values



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

def values(options = {})
  options = @options.merge(options)
  keys.map do |key|
    self[key, options]
  end
end

Instance Method Details

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

Access the cookie with the given name.

Parameters:

  • name (String)

    the name of the cookie

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

    the options for the cookie

Options Hash (options):

  • :raw (Boolean)

    get a raw cookie value, don't encode it with JSON

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'opal/browser/cookies.rb', line 51

def [](name, options = {})
  options = @options.merge(options)

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

  return if matches.empty?

  result = matches.flatten.map do |value|
    if options[:raw]
      FormData.decode(value)
    else
      JSON.parse(FormData.decode(value))
    end
  end

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

#[]=(name, options = {}, value) ⇒ 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):

  • :raw (Boolean)

    don't encode a value with JSON

  • :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



81
82
83
84
85
86
87
88
89
90
# File 'opal/browser/cookies.rb', line 81

def []=(name, options = {}, value)
  options = @options.merge(options)
  if options[:raw]
    string = value.to_s
  else
    string = JSON.dump(value)
  end
  encoded_value = encode(name, string, options)
  `#@document.cookie = #{encoded_value}`
end

#clear(_options = {}) ⇒ self

Delete all the cookies

Returns:

  • (self)


140
141
142
143
144
145
146
# File 'opal/browser/cookies.rb', line 140

def clear(_options = {})
  keys.each do |key|
    delete key
  end

  self
end

#delete(name, _options = {}) ⇒ Object

Delete a cookie.

Parameters:

  • name (String)

    the name of the cookie



95
96
97
# File 'opal/browser/cookies.rb', line 95

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

#each(options = {}) {|key, value| ... } ⇒ self

Enumerate the cookies.

Parameters:

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

    the options for the cookie

Options Hash (options):

  • :raw (Boolean)

    don't encode a value with JSON

Yield Parameters:

  • key (String)

    the name of the cookie

  • value (String)

    the value of the cookie

Returns:

  • (self)


126
127
128
129
130
131
132
133
134
135
# File 'opal/browser/cookies.rb', line 126

def each(options = {}, &block)
  return enum_for :each, options unless block
  options = @options.merge(options)

  keys.each do |key|
    yield key, self[key, options]
  end

  self
end