delegate css method to page's parser

This commit is contained in:
Kanezoh 2021-05-04 13:42:58 +09:00
parent d4d4cc97b3
commit 715fbfabb7
2 changed files with 7 additions and 7 deletions

View File

@ -4,6 +4,6 @@ agent = Mechanize.new
agent.request_headers = HTTP::Headers{"Foo" => "Bar"} agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
params = {"hoge" => "hoge"} params = {"hoge" => "hoge"}
page = agent.get("http://example.com/", params: params) page = agent.get("http://example.com/", params: params)
#puts page.code puts page.code
#puts page.body puts page.body
puts page.parser.not_nil!.css("h1").first.inner_text puts page.css("h1").first.inner_text

View File

@ -2,13 +2,13 @@ require "myhtml"
require "./file" require "./file"
class MechanizeCr::Page < MechanizeCr::File class MechanizeCr::Page < MechanizeCr::File
delegate :css, to: parser
def initialize(uri, response, body, code) def initialize(uri, response, body, code)
super(uri, response, body, code) super(uri, response, body, code)
end end
def parser def parser : Myhtml::Parser
return @parser if @parser @parser ||= Myhtml::Parser.new(@body)
return unless @body
@parser = Myhtml::Parser.new(@body)
end end
end end