Class: Browser::Cookies

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

Overview

Allows manipulation of browser cookies.

Constant Summary collapse

DEFAULT =

Default cookie options.

{
  expires: Time.now + 1.day,
  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



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

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



68
69
70
71
72
# File 'opal/browser/cookies.rb', line 68

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

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'opal/browser/cookies.rb', line 17

def options
  @options
end

#valuesArray (readonly)

Returns all the cookie values.

Returns:

  • (Array)

    all the cookie values



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

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:



32
33
34
35
36
37
38
39
40
41
42
# File 'opal/browser/cookies.rb', line 32

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

  return if matches.empty?

  result = matches.map {|cookie|
    JSON.parse(cookie.match(/^.*?=(.*)$/)[1].decode_uri_component)
  }

  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



55
56
57
# File 'opal/browser/cookies.rb', line 55

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

#clearself

Delete all the cookies

Returns:

  • (self)


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

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

  self
end

#delete(name) ⇒ Object

Delete a cookie.

Parameters:

  • name (String)

    the name of the cookie



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

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)


88
89
90
91
92
93
94
95
96
# File 'opal/browser/cookies.rb', line 88

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

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

  self
end