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-11 03:21:12 +02:00
|
|
|
getter fields : Array(MechanizeCr::FormContent::Field)
|
2021-05-18 15:00:04 +02:00
|
|
|
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
|
2021-06-11 03:21:12 +02:00
|
|
|
getter enctype : String
|
|
|
|
getter method : String
|
|
|
|
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-11 02:18:29 +02:00
|
|
|
@fields = Array(MechanizeCr::FormContent::Field).new
|
|
|
|
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
|
|
|
|
@action = node.fetch("action", "")
|
|
|
|
@method = node.fetch("method", "GET").upcase
|
2021-05-05 13:52:06 +02:00
|
|
|
#@name = node['name']
|
|
|
|
#@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)
|
|
|
|
raise MechanizeCr::ElementNotFoundError.new(:field, criteria) if f.nil?
|
|
|
|
f.first
|
2021-05-18 15:00:04 +02:00
|
|
|
end
|
|
|
|
|
2021-06-11 03:21:12 +02:00
|
|
|
private def parse
|
2021-05-18 15:10:03 +02:00
|
|
|
@fields = Array(MechanizeCr::FormContent::Field).new
|
2021-05-18 15:00:04 +02:00
|
|
|
@checkboxes = Array(MechanizeCr::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)
|
|
|
|
@fields << MechanizeCr::FormContent::Field.new(html_node)
|
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)
|
|
|
|
successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new
|
|
|
|
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
|
|
|
|
query.push(value)
|
|
|
|
end
|
|
|
|
query
|
2021-05-27 00:40:25 +02:00
|
|
|
end
|
2021-05-05 13:52:06 +02:00
|
|
|
end
|