Class: Reaxar::Element::A

Inherits:
Object
  • Object
show all
Defined in:
lib/reaxar/element/a.rb

Overview

Represents an HTML anchor (‘<a>`) element on a page.

Examples:

Accessing link attributes and clicking

link = Reaxar::Element::A.new(nokogiri_element, page)
puts link.href
link.click { |new_page| puts new_page.title }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, page) ⇒ A

Initializes a new anchor element wrapper.

Parameters:

  • element (Nokogiri::XML::Element)

    The Nokogiri element.

  • page (Reaxar::Page)

    The parent page.



22
23
24
25
# File 'lib/reaxar/element/a.rb', line 22

def initialize(element, page)
  @element = element
  @page = page
end

Instance Attribute Details

#elementNokogiri::XML::Element (readonly)

Returns The underlying Nokogiri element.

Returns:

  • (Nokogiri::XML::Element)

    The underlying Nokogiri element.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/reaxar/element/a.rb', line 16

class A
  attr_reader :element, :page

  # Initializes a new anchor element wrapper.
  # @param element [Nokogiri::XML::Element] The Nokogiri element.
  # @param page [Reaxar::Page] The parent page.
  def initialize(element, page)
    @element = element
    @page = page
  end

  # Returns the absolute URL for the link.
  # @return [String] The resolved href attribute.
  def href
    URI.join(page.url, element[:href]).to_s
  end

  # Opens the link in a new page context.
  # @yield [page] Optional block with the new page instance.
  # @return [Async::Task] The async task wrapping the new page.
  def click(&block)
    Page.open(href, page.client, &block)
  end
end

#pageReaxar::Page (readonly)

Returns The page this element belongs to.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/reaxar/element/a.rb', line 16

class A
  attr_reader :element, :page

  # Initializes a new anchor element wrapper.
  # @param element [Nokogiri::XML::Element] The Nokogiri element.
  # @param page [Reaxar::Page] The parent page.
  def initialize(element, page)
    @element = element
    @page = page
  end

  # Returns the absolute URL for the link.
  # @return [String] The resolved href attribute.
  def href
    URI.join(page.url, element[:href]).to_s
  end

  # Opens the link in a new page context.
  # @yield [page] Optional block with the new page instance.
  # @return [Async::Task] The async task wrapping the new page.
  def click(&block)
    Page.open(href, page.client, &block)
  end
end

Instance Method Details

#click {|page| ... } ⇒ Async::Task

Opens the link in a new page context.

Yields:

  • (page)

    Optional block with the new page instance.

Returns:

  • (Async::Task)

    The async task wrapping the new page.



36
37
38
# File 'lib/reaxar/element/a.rb', line 36

def click(&block)
  Page.open(href, page.client, &block)
end

#hrefString

Returns the absolute URL for the link.

Returns:

  • (String)

    The resolved href attribute.



29
30
31
# File 'lib/reaxar/element/a.rb', line 29

def href
  URI.join(page.url, element[:href]).to_s
end