Skip to content

fix Browser::Cookies #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions opal/browser/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ module Browser
# Allows manipulation of browser cookies.
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
#
# Usage:
#
# cookies = Browser::Cookies.new(`document`)
# cookies["my-cookie"] = "monster"
# cookies.delete("my-cookie")
#
class Cookies
# Default cookie options.
DEFAULT = {
expires: Time.now + 1.day,
expires: Time.now + 60 * 60 * 24,
secure: false
}

Expand Down Expand Up @@ -53,7 +60,9 @@ def [](name)
# @option options [String] :domain the domain the cookie is valid on
# @option options [Boolean] :secure whether the cookie is secure or not
def []=(name, value, options = {})
`#@document.cookie = #{encode name, value.is_a?(String) ? value : JSON.dump(value), @options.merge(options)}`
string = value.is_a?(String) ? value : JSON.dump(value)
encoded_value = encode(name, string, @options.merge(options))
`#@document.cookie = #{encoded_value}`
end

# Delete a cookie.
Expand Down Expand Up @@ -113,7 +122,7 @@ def encode(key, value, options = {})
io << key.encode_uri_component << ?= << value.encode_uri_component << '; '

io << 'max-age=' << options[:max_age] << '; ' if options[:max_age]
io << 'expires=' << options[:expires].to_utc << '; ' if options[:expires]
io << 'expires=' << options[:expires].utc << '; ' if options[:expires]
io << 'path=' << options[:path] << '; ' if options[:path]
io << 'domain=' << options[:domain] << '; ' if options[:domain]
io << 'secure' if options[:secure]
Expand Down