add comment

This commit is contained in:
Kanezoh 2021-10-14 20:42:33 +09:00
parent 999a13c2d8
commit f175c42097
3 changed files with 46 additions and 17 deletions

View File

@ -159,6 +159,7 @@ class Mechanize
end end
end end
# parse response. it is used internally.
def parse(uri, response, body) def parse(uri, response, body)
code = response.not_nil!.status_code code = response.not_nil!.status_code
MechanizeCr::Page.new(uri, response, body, code, self) MechanizeCr::Page.new(uri, response, body, code, self)
@ -169,7 +170,7 @@ class Mechanize
# ``` # ```
# agent.history => #<MechanizeCr::History> # agent.history => #<MechanizeCr::History>
# ``` # ```
def history def history : MechanizeCr::History
@agent.history @agent.history
end end
@ -186,29 +187,38 @@ class Mechanize
# ``` # ```
# agent.max_history # => 100 # agent.max_history # => 100
# ``` # ```
def max_history def max_history : Int32
history.max_size history.max_size
end end
# Set maximum number of pages allowed in the history. # set maximum number of pages allowed in the history.
# The default setting is 100 pages. # the default value is 100.
# ``` # ```
# agent.max_history = 150 # agent.max_history = 150
# ``` # ```
def max_history=(length) def max_history=(length : Int32)
history.max_size = length history.max_size = length
end end
# click link, and return page. # click link, and transit page.
def click(link) #
# ```
# page = agent.get("http://example.com")
# link = page.links.first
# page2 = agent.click(link)
# ```
def click(link : MechanizeCr::PageContent::Link) : MechanizeCr::Page
href = link.href href = link.href
get href get href
end end
# download page body from given uri. # download page body from given uri.
# TODO: except this request from history. # ```
def download(uri, # # make download.html whose content is http://example.com's html.
filename, # agent.download("http://example.com", "download.html")
# ```
def download(uri : URI | String,
filename : String,
headers = HTTP::Headers.new, headers = HTTP::Headers.new,
params : Hash(String, String | Array(String)) = Hash(String, String).new) params : Hash(String, String | Array(String)) = Hash(String, String).new)
transact do transact do

View File

@ -2,6 +2,10 @@ require "./file"
require "./utils/element_matcher" require "./utils/element_matcher"
require "./page/link" require "./page/link"
# This class represents the page of response.
# If you send request, it returns the instance of Page.
# You can get status code, title, and page body, and search html node using css selector.
class MechanizeCr::Page < MechanizeCr::File class MechanizeCr::Page < MechanizeCr::File
include MechanizeCr::ElementMatcher include MechanizeCr::ElementMatcher
delegate :css, to: parser delegate :css, to: parser
@ -13,11 +17,14 @@ class MechanizeCr::Page < MechanizeCr::File
super(uri, response, body, code) super(uri, response, body, code)
end end
# parser to parse response body.
# TODO: now it's Lexbor::Parser. I want to also support other parsers like JSON.
def parser : Lexbor::Parser def parser : Lexbor::Parser
@parser ||= Lexbor::Parser.new(@body) @parser ||= Lexbor::Parser.new(@body)
end end
def title # return page title.
def title : String
title_node = css("title") title_node = css("title")
if title_node.empty? if title_node.empty?
"" ""
@ -26,7 +33,8 @@ class MechanizeCr::Page < MechanizeCr::File
end end
end end
def forms # return all forms(`MechanizeCr::Form`) in the page.
def forms : Array(MechanizeCr::Form)
forms = css("form").map do |html_form| forms = css("form").map do |html_form|
form = Form.new(html_form, self) form = Form.new(html_form, self)
form.action ||= @uri.to_s form.action ||= @uri.to_s
@ -34,7 +42,8 @@ class MechanizeCr::Page < MechanizeCr::File
end.to_a end.to_a
end end
def links # return all links(`MechanizeCr::PageContent::Link) in the page.
def links : Array(MechanizeCr::PageContent::Link)
links = %w{a area}.map do |tag| links = %w{a area}.map do |tag|
css(tag).map do |node| css(tag).map do |node|
PageContent::Link.new(node, @mech, self) PageContent::Link.new(node, @mech, self)
@ -42,8 +51,5 @@ class MechanizeCr::Page < MechanizeCr::File
end.flatten end.flatten
end end
# generate form_with, forms_with methods
# ex) form_with({:name => "login_form"})
# it detects form(s) which match conditions.
elements_with "form" elements_with "form"
end end

View File

@ -1,7 +1,20 @@
module MechanizeCr::ElementMatcher module MechanizeCr::ElementMatcher
macro elements_with(singular, plural = "") macro elements_with(singular, plural = "")
{% plural = "#{singular.id}s" if plural.empty? %} {% plural = "#{singular.id}s" if plural.empty? %}
def {{plural.id}}_with(criteria) # search {{ singular.id }} which matches condition.
#
# Examples
# ```
# # if you specify String like "foo", it searches form which name is "foo".
# # like {<form name="foo"></form>}
# page.form_with("foo")
#
# # you can specify tag's attribute and its' value by NamedTuple or Hash(String, String).
# ex) <form class="foo"></form>
# page.form_with("class" => "foo")
# page.form_with(class: "foo")
# ```
def {{plural.id}}_with(criteria : String | NamedTuple | Hash(String,String))
{{plural.id}}_with(criteria){} {{plural.id}}_with(criteria){}
end end