From 715fbfabb7af5d72204edb6bbcda8754ac20f8ae Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 4 May 2021 13:42:58 +0900 Subject: [PATCH] delegate css method to page's parser --- main.cr | 6 +++--- src/mechanize/page.cr | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/main.cr b/main.cr index 2197a14..cd0a922 100644 --- a/main.cr +++ b/main.cr @@ -4,6 +4,6 @@ agent = Mechanize.new agent.request_headers = HTTP::Headers{"Foo" => "Bar"} params = {"hoge" => "hoge"} page = agent.get("http://example.com/", params: params) -#puts page.code -#puts page.body -puts page.parser.not_nil!.css("h1").first.inner_text +puts page.code +puts page.body +puts page.css("h1").first.inner_text diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr index 72826c3..bb04f4a 100644 --- a/src/mechanize/page.cr +++ b/src/mechanize/page.cr @@ -2,13 +2,13 @@ require "myhtml" require "./file" class MechanizeCr::Page < MechanizeCr::File + delegate :css, to: parser + def initialize(uri, response, body, code) super(uri, response, body, code) end - def parser - return @parser if @parser - return unless @body - @parser = Myhtml::Parser.new(@body) + def parser : Myhtml::Parser + @parser ||= Myhtml::Parser.new(@body) end end