Skip to content

Commit

Permalink
Fix Browser::Cookies
Browse files Browse the repository at this point in the history
replace 1.day method since its been moved out of opal to opal-activesupport
fixes setting a cookie.  use utc instead of to_utc
add usage example doc
  • Loading branch information
tongueroo committed Oct 15, 2016
1 parent 815348e commit fc44555
Showing 1 changed file with 12 additions and 3 deletions.
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

0 comments on commit fc44555

Please sign in to comment.