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
|
|
|
|
getter fields : Array(MechanizeCr::FormContent::Field)
|
2021-05-18 15:00:04 +02:00
|
|
|
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
|
2021-05-20 11:00:39 +02:00
|
|
|
getter enctype : String
|
2021-05-05 13:52:06 +02:00
|
|
|
|
2021-05-18 15:00:04 +02:00
|
|
|
def initialize(node : Node)
|
2021-05-05 13:52:06 +02:00
|
|
|
@enctype = node["enctype"] || "application/x-www-form-urlencoded"
|
|
|
|
@node = node
|
|
|
|
@fields = Array(MechanizeCr::FormContent::Field).new
|
2021-05-18 15:00:04 +02:00
|
|
|
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
|
2021-05-05 13:52:06 +02:00
|
|
|
#@action = Mechanize::Util.html_unescape(node['action'])
|
|
|
|
#@method = (node['method'] || 'GET').upcase
|
|
|
|
#@name = node['name']
|
|
|
|
#@clicked_buttons = []
|
|
|
|
#@page = page
|
|
|
|
#@mech = mech
|
|
|
|
#
|
|
|
|
#@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-05-20 11:00:39 +02:00
|
|
|
def build_query
|
|
|
|
query = [] of Array(String)
|
2021-05-18 15:00:04 +02:00
|
|
|
successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new
|
|
|
|
fields.each do |elm|
|
2021-05-18 15:10:03 +02:00
|
|
|
successful_controls << elm
|
2021-05-18 15:00:04 +02:00
|
|
|
end
|
|
|
|
checkboxes.each do |elm|
|
|
|
|
if elm.checked
|
|
|
|
successful_controls << elm
|
|
|
|
end
|
|
|
|
end
|
2021-05-20 11:00:39 +02:00
|
|
|
successful_controls.each do |ctrl|
|
|
|
|
value = ctrl.query_value
|
|
|
|
query.push(value)
|
|
|
|
end
|
|
|
|
query
|
2021-05-18 15:00:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
@node.search("input").not_nil!.each do |node|
|
|
|
|
end
|
2021-05-05 13:52:06 +02:00
|
|
|
end
|
2021-05-20 11:00:39 +02:00
|
|
|
|
|
|
|
def build_query_string(params : Array(Array(String)))
|
|
|
|
params.reduce("") do |acc, arr|
|
|
|
|
hash = { arr[0] => arr[1] }
|
|
|
|
acc + URI::Params.encode(hash) + '&'
|
|
|
|
end.rchop
|
|
|
|
end
|
2021-05-05 13:52:06 +02:00
|
|
|
end
|