Class: Reaxar::Page

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

Overview

Represents a web page and provides methods for interacting with its content.

Examples:

Open a page and print its title

page = Reaxar::Page.open('https://example.com')
puts page.title

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, client = nil) {|self| ... } ⇒ Page

Initializes a new Page instance.

Parameters:

  • url (String)

    The URL of the page.

  • client (Client, nil) (defaults to: nil)

    Optional HTTP client.

Yields:

  • (self)

    Optional block to yield the page instance.



51
52
53
54
55
56
57
58
59
# File 'lib/reaxar/page.rb', line 51

def initialize(url, client = nil)
  @url = url
  @client = client || Client.new(self.class.logger)
  @client.use Reaxar::Middleware::Redirect
  @response = @client.get(url)
  @document = Nokogiri::HTML(@response.read)

  yield self if block_given?
end

Class Attribute Details

.loggerLogger?

Returns The logger instance used by the client.

Returns:

  • (Logger, nil)

    The logger instance used by the client.



27
28
29
# File 'lib/reaxar/page.rb', line 27

def logger
  @logger
end

Instance Attribute Details

#clientClient (readonly)

Returns The HTTP client used to fetch the page.

Returns:

  • (Client)

    The HTTP client used to fetch the page.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reaxar/page.rb', line 21

class Page
  attr_reader :url, :client, :response, :document

  class << self
    # @!attribute [rw] logger
    #   @return [Logger, nil] The logger instance used by the client.
    attr_accessor :logger

    # Configures the logger for the Page class.
    # @param logger [Logger] The logger to use.
    # @return [void]
    def configure(logger:)
      self.logger = logger
    end
  end

  # Opens a page asynchronously.
  # @param url [String] The URL to open.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [page] Optional block to yield the page instance.
  # @yieldparam page [Page] The page instance.
  # @return [Async::Task] The async task wrapping the page.
  def self.open(url, client = nil, &block)
    Async { new(url, client, &block) }
  end

  # Initializes a new Page instance.
  # @param url [String] The URL of the page.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [self] Optional block to yield the page instance.
  def initialize(url, client = nil)
    @url = url
    @client = client || Client.new(self.class.logger)
    @client.use Reaxar::Middleware::Redirect
    @response = @client.get(url)
    @document = Nokogiri::HTML(@response.read)

    yield self if block_given?
  end

  # Returns the title of the page.
  # @return [String, nil] The page title or nil if not found.
  def title
    document.title
  end

  # Returns all links (<a> elements) on the page.
  # @return [Array<Reaxar::Element::A>] The array of link elements.
  def links
    @links ||= document.css('a[href]').map do |link|
      Reaxar::Element::A.new(link, self)
    end
  end

  # Returns the HTML content of the page.
  # @return [String] The HTML content.
  def html
    document.to_html
  end

  # Finds a form on the page.
  # @param selector [String] CSS selector for the form (default: 'form').
  # @return [Object, nil] The form element or nil if not found.
  def form(selector = 'form')
    # Реализация работы с формами (можно расширить)
  end

  # Submits a form on the page.
  # @param selector [String] CSS selector for the form.
  # @param data [Hash] Data to submit with the form.
  # @return [Object] The result of the form submission.
  def submit_form(selector, data = {})
    # Реализация отправки формы
  end
end

#documentNokogiri::HTML::Document (readonly)

Returns The parsed HTML document.

Returns:

  • (Nokogiri::HTML::Document)

    The parsed HTML document.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reaxar/page.rb', line 21

class Page
  attr_reader :url, :client, :response, :document

  class << self
    # @!attribute [rw] logger
    #   @return [Logger, nil] The logger instance used by the client.
    attr_accessor :logger

    # Configures the logger for the Page class.
    # @param logger [Logger] The logger to use.
    # @return [void]
    def configure(logger:)
      self.logger = logger
    end
  end

  # Opens a page asynchronously.
  # @param url [String] The URL to open.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [page] Optional block to yield the page instance.
  # @yieldparam page [Page] The page instance.
  # @return [Async::Task] The async task wrapping the page.
  def self.open(url, client = nil, &block)
    Async { new(url, client, &block) }
  end

  # Initializes a new Page instance.
  # @param url [String] The URL of the page.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [self] Optional block to yield the page instance.
  def initialize(url, client = nil)
    @url = url
    @client = client || Client.new(self.class.logger)
    @client.use Reaxar::Middleware::Redirect
    @response = @client.get(url)
    @document = Nokogiri::HTML(@response.read)

    yield self if block_given?
  end

  # Returns the title of the page.
  # @return [String, nil] The page title or nil if not found.
  def title
    document.title
  end

  # Returns all links (<a> elements) on the page.
  # @return [Array<Reaxar::Element::A>] The array of link elements.
  def links
    @links ||= document.css('a[href]').map do |link|
      Reaxar::Element::A.new(link, self)
    end
  end

  # Returns the HTML content of the page.
  # @return [String] The HTML content.
  def html
    document.to_html
  end

  # Finds a form on the page.
  # @param selector [String] CSS selector for the form (default: 'form').
  # @return [Object, nil] The form element or nil if not found.
  def form(selector = 'form')
    # Реализация работы с формами (можно расширить)
  end

  # Submits a form on the page.
  # @param selector [String] CSS selector for the form.
  # @param data [Hash] Data to submit with the form.
  # @return [Object] The result of the form submission.
  def submit_form(selector, data = {})
    # Реализация отправки формы
  end
end

#responseObject (readonly)

Returns The HTTP response object.

Returns:

  • (Object)

    The HTTP response object.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reaxar/page.rb', line 21

class Page
  attr_reader :url, :client, :response, :document

  class << self
    # @!attribute [rw] logger
    #   @return [Logger, nil] The logger instance used by the client.
    attr_accessor :logger

    # Configures the logger for the Page class.
    # @param logger [Logger] The logger to use.
    # @return [void]
    def configure(logger:)
      self.logger = logger
    end
  end

  # Opens a page asynchronously.
  # @param url [String] The URL to open.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [page] Optional block to yield the page instance.
  # @yieldparam page [Page] The page instance.
  # @return [Async::Task] The async task wrapping the page.
  def self.open(url, client = nil, &block)
    Async { new(url, client, &block) }
  end

  # Initializes a new Page instance.
  # @param url [String] The URL of the page.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [self] Optional block to yield the page instance.
  def initialize(url, client = nil)
    @url = url
    @client = client || Client.new(self.class.logger)
    @client.use Reaxar::Middleware::Redirect
    @response = @client.get(url)
    @document = Nokogiri::HTML(@response.read)

    yield self if block_given?
  end

  # Returns the title of the page.
  # @return [String, nil] The page title or nil if not found.
  def title
    document.title
  end

  # Returns all links (<a> elements) on the page.
  # @return [Array<Reaxar::Element::A>] The array of link elements.
  def links
    @links ||= document.css('a[href]').map do |link|
      Reaxar::Element::A.new(link, self)
    end
  end

  # Returns the HTML content of the page.
  # @return [String] The HTML content.
  def html
    document.to_html
  end

  # Finds a form on the page.
  # @param selector [String] CSS selector for the form (default: 'form').
  # @return [Object, nil] The form element or nil if not found.
  def form(selector = 'form')
    # Реализация работы с формами (можно расширить)
  end

  # Submits a form on the page.
  # @param selector [String] CSS selector for the form.
  # @param data [Hash] Data to submit with the form.
  # @return [Object] The result of the form submission.
  def submit_form(selector, data = {})
    # Реализация отправки формы
  end
end

#urlString (readonly)

Returns The URL of the page.

Returns:

  • (String)

    The URL of the page.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reaxar/page.rb', line 21

class Page
  attr_reader :url, :client, :response, :document

  class << self
    # @!attribute [rw] logger
    #   @return [Logger, nil] The logger instance used by the client.
    attr_accessor :logger

    # Configures the logger for the Page class.
    # @param logger [Logger] The logger to use.
    # @return [void]
    def configure(logger:)
      self.logger = logger
    end
  end

  # Opens a page asynchronously.
  # @param url [String] The URL to open.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [page] Optional block to yield the page instance.
  # @yieldparam page [Page] The page instance.
  # @return [Async::Task] The async task wrapping the page.
  def self.open(url, client = nil, &block)
    Async { new(url, client, &block) }
  end

  # Initializes a new Page instance.
  # @param url [String] The URL of the page.
  # @param client [Client, nil] Optional HTTP client.
  # @yield [self] Optional block to yield the page instance.
  def initialize(url, client = nil)
    @url = url
    @client = client || Client.new(self.class.logger)
    @client.use Reaxar::Middleware::Redirect
    @response = @client.get(url)
    @document = Nokogiri::HTML(@response.read)

    yield self if block_given?
  end

  # Returns the title of the page.
  # @return [String, nil] The page title or nil if not found.
  def title
    document.title
  end

  # Returns all links (<a> elements) on the page.
  # @return [Array<Reaxar::Element::A>] The array of link elements.
  def links
    @links ||= document.css('a[href]').map do |link|
      Reaxar::Element::A.new(link, self)
    end
  end

  # Returns the HTML content of the page.
  # @return [String] The HTML content.
  def html
    document.to_html
  end

  # Finds a form on the page.
  # @param selector [String] CSS selector for the form (default: 'form').
  # @return [Object, nil] The form element or nil if not found.
  def form(selector = 'form')
    # Реализация работы с формами (можно расширить)
  end

  # Submits a form on the page.
  # @param selector [String] CSS selector for the form.
  # @param data [Hash] Data to submit with the form.
  # @return [Object] The result of the form submission.
  def submit_form(selector, data = {})
    # Реализация отправки формы
  end
end

Class Method Details

.configure(logger:) ⇒ void

This method returns an undefined value.

Configures the logger for the Page class.

Parameters:

  • logger (Logger)

    The logger to use.



32
33
34
# File 'lib/reaxar/page.rb', line 32

def configure(logger:)
  self.logger = logger
end

.open(url, client = nil) {|page| ... } ⇒ Async::Task

Opens a page asynchronously.

Parameters:

  • url (String)

    The URL to open.

  • client (Client, nil) (defaults to: nil)

    Optional HTTP client.

Yields:

  • (page)

    Optional block to yield the page instance.

Yield Parameters:

  • page (Page)

    The page instance.

Returns:

  • (Async::Task)

    The async task wrapping the page.



43
44
45
# File 'lib/reaxar/page.rb', line 43

def self.open(url, client = nil, &block)
  Async { new(url, client, &block) }
end

Instance Method Details

#form(selector = 'form') ⇒ Object?

Finds a form on the page.

Parameters:

  • selector (String) (defaults to: 'form')

    CSS selector for the form (default: ‘form’).

Returns:

  • (Object, nil)

    The form element or nil if not found.



84
85
86
# File 'lib/reaxar/page.rb', line 84

def form(selector = 'form')
  # Реализация работы с формами (можно расширить)
end

#htmlString

Returns the HTML content of the page.

Returns:

  • (String)

    The HTML content.



77
78
79
# File 'lib/reaxar/page.rb', line 77

def html
  document.to_html
end

Returns all links (<a> elements) on the page.

Returns:



69
70
71
72
73
# File 'lib/reaxar/page.rb', line 69

def links
  @links ||= document.css('a[href]').map do |link|
    Reaxar::Element::A.new(link, self)
  end
end

#submit_form(selector, data = {}) ⇒ Object

Submits a form on the page.

Parameters:

  • selector (String)

    CSS selector for the form.

  • data (Hash) (defaults to: {})

    Data to submit with the form.

Returns:

  • (Object)

    The result of the form submission.



92
93
94
# File 'lib/reaxar/page.rb', line 92

def submit_form(selector, data = {})
  # Реализация отправки формы
end

#titleString?

Returns the title of the page.

Returns:

  • (String, nil)

    The page title or nil if not found.



63
64
65
# File 'lib/reaxar/page.rb', line 63

def title
  document.title
end