require 'stringio' 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 + 60 * 60 * 24, secure: false } include Enumerable attr_reader :options # Create a new {Cookies} wrapper. # # @param document [native] the native document object def initialize(document) @document = document @options = DEFAULT.dup end # Access the cookie with the given name. # # @param name [String] the name of the cookie # # @return [Object] 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 # Set a cookie. # # @param name [String] the name of the cookie # @param value [Object] the data to set # @param options [Hash] the options for the cookie # # @option options [Integer] :max_age the max age of the cookie in seconds # @option options [Time] :expires the expire date # @option options [String] :path the path the cookie is valid on # @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 = {}) 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. # # @param name [String] the name of the cookie def delete(name) `#@document.cookie = #{encode name, '', expires: Time.now}` end # @!attribute [r] keys # @return [Array] all the cookie names def keys Array(`#@document.cookie.split(/; /)`).map {|cookie| cookie.split(/\s*=\s*/).first } end # @!attribute [r] values # @return [Array] all the cookie values def values keys.map {|key| self[key] } end # Enumerate the cookies. # # @yieldparam key [String] the name of the cookie # @yieldparam value [String] the value of the cookie # # @return [self] def each(&block) return enum_for :each unless block keys.each {|key| yield key, self[key] } self end # Delete all the cookies # # @return [self] def clear keys.each {|key| delete key } self end protected def encode(key, value, options = {}) io = StringIO.new io << FormData.encode(key) << ?= << FormData.encode(value) << '; ' io << 'max-age=' << options[:max_age] << '; ' if options[:max_age] 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] io.string end end class DOM::Document < DOM::Element # @!attribute [r] cookies # @return [Cookies] the cookies for the document def cookies Cookies.new(@native) if defined?(`#@native.cookie`) end end end