mechanize.cr/src/mechanize/form.cr

97 lines
2.6 KiB
Crystal
Raw Normal View History

2021-05-05 13:52:06 +02:00
require "./form/field"
2021-05-18 15:00:04 +02:00
require "./form/check_box"
2021-05-05 13:52:06 +02:00
class MechanizeCr::Form
2021-06-16 16:35:14 +02:00
getter fields : Array(FormContent::Field)
getter checkboxes : Array(FormContent::CheckBox)
2021-06-11 03:21:12 +02:00
getter enctype : String
getter method : String
2021-06-11 03:52:34 +02:00
getter name : String
2021-06-11 03:21:12 +02:00
property action : String
2021-05-05 13:52:06 +02:00
2021-05-26 06:49:37 +02:00
def initialize(node : Node | Myhtml::Node)
2021-06-11 02:18:29 +02:00
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
2021-05-05 13:52:06 +02:00
@node = node
2021-06-16 16:35:14 +02:00
@fields = Array(FormContent::Field).new
@checkboxes = Array(FormContent::CheckBox).new
2021-06-11 02:18:29 +02:00
@action = node.fetch("action", "")
@method = node.fetch("method", "GET").upcase
2021-06-11 03:52:34 +02:00
@name = node.fetch("name", "")
2021-05-05 13:52:06 +02:00
#@clicked_buttons = []
#@page = page
#@mech = mech
2021-06-11 02:18:29 +02:00
2021-05-05 13:52:06 +02:00
#@encoding = node['accept-charset'] || (page && page.encoding) || nil
#@ignore_encoding_error = false
2021-05-18 15:00:04 +02:00
parse
end
def request_data
query_params = build_query
2021-05-20 11:00:39 +02:00
build_query_string(query_params)
2021-05-18 15:00:04 +02:00
end
2021-06-11 03:21:12 +02:00
def fields_with(criteria)
value = Hash(String,String).new
if String === criteria
value = {"name" => criteria}
else
# TODO
# when args whose type isn't String is given
2021-05-18 15:00:04 +02:00
end
2021-06-11 03:21:12 +02:00
f = fields.select do |field|
value.all? do |k,v|
v === field.name
2021-05-18 15:00:04 +02:00
end
end
2021-06-11 03:21:12 +02:00
f.empty? ? nil : f
end
def field_with(criteria)
f = fields_with(criteria)
2021-06-16 16:35:14 +02:00
raise ElementNotFoundError.new(:field, criteria) if f.nil?
2021-06-11 03:21:12 +02:00
f.first
2021-05-18 15:00:04 +02:00
end
2021-06-11 03:21:12 +02:00
private def parse
2021-06-16 16:35:14 +02:00
@fields = Array(FormContent::Field).new
@checkboxes = Array(FormContent::CheckBox).new
2021-05-27 00:40:25 +02:00
@node.css("input").not_nil!.each do |html_node|
html_node = html_node.as(Myhtml::Node)
2021-06-16 07:58:49 +02:00
type = (html_node["type"] || "text").downcase
case type
when "checkbox"
2021-06-16 16:35:14 +02:00
@checkboxes << FormContent::CheckBox.new(html_node, self)
2021-06-16 07:58:49 +02:00
else
2021-06-16 16:35:14 +02:00
@fields << FormContent::Field.new(html_node)
2021-06-16 07:58:49 +02:00
end
2021-05-18 15:00:04 +02:00
end
2021-05-05 13:52:06 +02:00
end
2021-05-20 11:00:39 +02:00
2021-06-11 03:21:12 +02:00
private def build_query_string(params : Array(Array(String)))
2021-05-20 11:00:39 +02:00
params.reduce("") do |acc, arr|
hash = { arr[0] => arr[1] }
acc + URI::Params.encode(hash) + '&'
end.rchop
end
2021-05-27 00:40:25 +02:00
2021-06-11 03:21:12 +02:00
private def build_query
query = [] of Array(String)
2021-06-16 16:35:14 +02:00
successful_controls = Array(FormContent::Field | FormContent::CheckBox).new
2021-06-11 03:21:12 +02:00
fields.each do |elm|
successful_controls << elm
2021-05-27 00:40:25 +02:00
end
2021-06-11 03:21:12 +02:00
checkboxes.each do |elm|
if elm.checked
successful_controls << elm
2021-05-27 00:40:25 +02:00
end
end
2021-06-11 03:21:12 +02:00
successful_controls.each do |ctrl|
value = ctrl.query_value
2021-06-12 16:10:56 +02:00
next if value[0] == ""
2021-06-11 03:21:12 +02:00
query.push(value)
end
query
2021-05-27 00:40:25 +02:00
end
2021-05-05 13:52:06 +02:00
end