Class: Harmony::Page

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

Defined Under Namespace

Modules: Window

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>`



64
65
66
# File 'lib/harmony/page.rb', line 64

def initialize(document=nil)
  @window = Window.from_document(document) if document
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



52
53
54
55
56
# File 'lib/harmony/page.rb', line 52

def self.fetch(uri)
  page = new
  page.instance_variable_set(:@window, Window.from_uri(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



110
111
112
# File 'lib/harmony/page.rb', line 110

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



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

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:



74
75
76
77
78
79
# File 'lib/harmony/page.rb', line 74

def load(*paths)
  paths.flatten.each do |path|
    window.load(path.to_s)
  end
  self
end

#to_htmlString

Page as html document

Returns:

  • (String)

    html



118
119
120
# File 'lib/harmony/page.rb', line 118

def to_html
  document.innerHTML
end

#windowObject

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

Returns:

  • (Object)

    window DOM object



100
101
102
# File 'lib/harmony/page.rb', line 100

def window
  @window ||= Window.blank
end