Class: Harmony::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/harmony/page.rb

Defined Under Namespace

Classes: Window

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document = nil) ⇒ Page

Create new page containing given document.

Parameters:

  • document (String) (defaults to: nil)

    HTML document. Defaults to an “about:blank” window, with the basic structure: ‘<html><head><title></title></head><body></body></html>`



44
45
46
# File 'lib/harmony/page.rb', line 44

def initialize(document=nil)
  @window = document ? Window.from_document(document) : Window.blank
end

Instance Attribute Details

#windowObject (readonly)

DOM document’s ‘window` object. Equivalent to the return value of `page.execute_js(’window’)‘

Returns:

  • (Object)

    window DOM object



16
17
18
# File 'lib/harmony/page.rb', line 16

def window
  @window
end

Class Method Details

.fetch(uri) ⇒ Page

Create page from remote document.

Examples:


Page.fetch('http://montrealrb.org')
Page.fetch('http://localhost:3000')
Page.fetch('file:///home/mynyml/www/foo/index.html')

Parameters:

  • uri (String)

    uri to fetch document from

Returns:

  • (Page)

    new page object preloaded with fetched document



32
33
34
35
36
# File 'lib/harmony/page.rb', line 32

def self.fetch(uri)
  page = new
  page.window.open(uri)
  page
end

Instance Method Details

#documentObject

Convenience method, equivalent to the return value of ‘page.execute_js(’window.document’)‘

Returns:

  • (Object)

    document DOM object



82
83
84
# File 'lib/harmony/page.rb', line 82

def document
  window.document
end

#execute_js(code) ⇒ Object Also known as: x

Evaluate Javascript code within this page’s context.

Parameters:

  • code (String)

    javascript code to execute

Returns:

  • (Object)

    last javascript statement’s value, cast to a ruby object



71
72
73
# File 'lib/harmony/page.rb', line 71

def execute_js(code)
  window.evaluate(code)
end

#load(*paths) ⇒ Page

Load one or more javascript files in page’s context

Parameters:

  • paths (#to_s, #to_s, ...)

    paths to js file

Returns:



54
55
56
57
58
59
60
61
# File 'lib/harmony/page.rb', line 54

def load(*paths)
  paths.flatten.each do |path|
    path.to_s.map {|f|
      window.evaluate(File.read(f).gsub(/\A#!.*$/, ''), f, 1)
    }.last
  end
  self
end

#to_htmlString

Page as html document

Returns:

  • (String)

    html



90
91
92
# File 'lib/harmony/page.rb', line 90

def to_html
  document.innerHTML
end