Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal-browser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 50bd6b6bf053
Choose a base ref
...
head repository: opal/opal-browser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 64d067f6353d
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 28, 2014

  1. Copy the full SHA
    875862f View commit details
  2. Copy the full SHA
    26a67cb View commit details
  3. README: add badges

    meh committed Jan 28, 2014
    Copy the full SHA
    64d067f View commit details
Showing with 93 additions and 28 deletions.
  1. +5 −0 README.md
  2. +80 −24 opal/browser/cookies.rb
  3. +0 −4 opal/browser/dom/document.rb
  4. +8 −0 opal/browser/location.rb
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Browser support for Opal
========================

[![Build Status](https://secure.travis-ci.org/opal/opal-browser.png?branch=master)](http://travis-ci.org/opal/opal-browser)
[![Gem Version](https://badge.fury.io/rb/opal-browser.png)](http://badge.fury.io/rb/opal-browser)
[![Code Climate](https://codeclimate.com/github/opal/opal-browser.png)](https://codeclimate.com/github/opal/opal-browser)

This library aims to be a full-blown wrapper for all the browser API including
HTML5.

104 changes: 80 additions & 24 deletions opal/browser/cookies.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
require 'stringio'

module Browser

# Allows manipulation of browser cookies.
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
class Cookies
# Default cookie options.
DEFAULT = {
expires: Time.now + 1.day,
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 = {
expires: Time.now + 1.day,
path: '',
domain: '',
secure: ''
}
end

def options (value = nil)
value ? @options.merge!(value) : @options
@options = DEFAULT.dup
end

def [] (name)
matches = `#@document.cookie`.scan(/#{Regexp.escape(key.encode_uri_component)}=([^;]*)/)
# 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(name.encode_uri_component)}=([^;]*)/)

return if matches.empty?

@@ -30,49 +41,94 @@ def [] (name)
result.length == 1 ? result.first : result
end

def []= (name, value, options = {})
# Set a cookie.
#
# Options
# -------
# + **max_age** - the max age of the cookie in seconds
# + **expires** - the expire date as {Time}
# + **path** - the path where the cookie is valid on
# + **domain** - the domain where the cookie is valid on
# + **secure** - whether the cookie is secure or not
#
# @param name [String] the name of the cookie
# @param value [Object] the data to set
# @param options [Hash] the options for the cookie
def []=(name, value, options = {})
`#@document.cookie = #{encode name, value.is_a?(String) ? value : JSON.dump(value), @options.merge(options)}`
end

def delete (name)
# 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<String>] 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

def each
# 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 = {})
result = "#{key.encode_uri_component}=#{value.encode_uri_component}; "
def encode(key, value, options = {})
io = StringIO.new

io << key.encode_uri_component << ?= << value.encode_uri_component << '; '

result += "max-age=#{options[:max_age]}; " if options[:max_age]
result += "expires=#{options[:expires].to_utc}; " if options[:expires]
result += "path=#{options[:path]}; " if options[:path]
result += "domain=#{options[:domain]}; " if options[:domain]
result += 'secure' if options[:secure]
io << 'max-age=' << options[:max_age] << '; ' if options[:max_age]
io << 'expires=' << options[:expires].to_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

result
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

4 changes: 0 additions & 4 deletions opal/browser/dom/document.rb
Original file line number Diff line number Diff line change
@@ -55,10 +55,6 @@ def inspect
"#<DOM::Document: #{children.inspect}>"
end

def location
Location.new(`#@native.location`) if `#@native.location`
end

def title
`#@native.title`
end
8 changes: 8 additions & 0 deletions opal/browser/location.rb
Original file line number Diff line number Diff line change
@@ -76,4 +76,12 @@ def location
end
end

class DOM::Document
# @!attribute [r] location
# @return [Location] the location for the document
def location
Location.new(`#@native.location`) if `#@native.location`
end
end

end