mechanize.cr/src/mechanize/form.cr

124 lines
3.4 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-06-16 17:11:04 +02:00
require "./form/radio_button"
2021-05-18 15:00:04 +02:00
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-16 17:11:04 +02:00
getter radiobuttons : Array(FormContent::RadioButton)
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-16 17:11:04 +02:00
@radiobuttons = Array(FormContent::RadioButton).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-17 01:30:32 +02:00
# generate fields_with and field_with methods.
# These methods are used for finding nodes that matches conditions.
2021-06-17 01:33:23 +02:00
# ex.) field_with("email") finds <input name="email">
2021-06-17 01:30:32 +02:00
2021-06-17 03:50:16 +02:00
{% for singular, index in ["field", "radiobutton"] %}
2021-06-17 01:30:32 +02:00
{% plural = "#{singular.id}s" %}
def {{plural.id}}_with(criteria)
value = Hash(String,String).new
if String === criteria
value = {"name" => criteria}
else
# TODO
# when args whose type isn't String is given
end
f = {{plural.id}}.select do |elm|
value.all? do |k,v|
v === elm.name
end
2021-05-18 15:00:04 +02:00
end
2021-06-17 01:30:32 +02:00
f.empty? ? nil : f
2021-05-18 15:00:04 +02:00
end
2021-06-11 03:21:12 +02:00
2021-06-17 03:50:16 +02:00
def {{plural.id}}_with(criteria, &block)
value = Hash(String,String).new
if String === criteria
value = {"name" => criteria}
else
# TODO
# when args whose type isn't String is given
end
f = {{plural.id}}.select do |elm|
value.all? do |k,v|
v === elm.name
end
end
yield f
f
end
2021-06-17 01:30:32 +02:00
def {{singular.id}}_with(criteria)
f = {{plural.id}}_with(criteria)
raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.nil?
f.first
end
{% end %}
2021-05-18 15:00:04 +02:00
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