refactor form
parent
74325fc5a1
commit
9edb9bc5f0
|
@ -2,11 +2,11 @@ require "./form/field"
|
||||||
require "./form/check_box"
|
require "./form/check_box"
|
||||||
|
|
||||||
class MechanizeCr::Form
|
class MechanizeCr::Form
|
||||||
getter fields : Array(MechanizeCr::FormContent::Field)
|
getter fields : Array(MechanizeCr::FormContent::Field)
|
||||||
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
|
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
|
||||||
getter enctype : String
|
getter enctype : String
|
||||||
getter method : String
|
getter method : String
|
||||||
property action : String
|
property action : String
|
||||||
|
|
||||||
def initialize(node : Node | Myhtml::Node)
|
def initialize(node : Node | Myhtml::Node)
|
||||||
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
|
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
|
||||||
|
@ -30,40 +30,6 @@ class MechanizeCr::Form
|
||||||
build_query_string(query_params)
|
build_query_string(query_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_query
|
|
||||||
query = [] of Array(String)
|
|
||||||
successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new
|
|
||||||
fields.each do |elm|
|
|
||||||
successful_controls << elm
|
|
||||||
end
|
|
||||||
checkboxes.each do |elm|
|
|
||||||
if elm.checked
|
|
||||||
successful_controls << elm
|
|
||||||
end
|
|
||||||
end
|
|
||||||
successful_controls.each do |ctrl|
|
|
||||||
value = ctrl.query_value
|
|
||||||
query.push(value)
|
|
||||||
end
|
|
||||||
query
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse
|
|
||||||
@fields = Array(MechanizeCr::FormContent::Field).new
|
|
||||||
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
|
|
||||||
@node.css("input").not_nil!.each do |html_node|
|
|
||||||
html_node = html_node.as(Myhtml::Node)
|
|
||||||
@fields << MechanizeCr::FormContent::Field.new(html_node)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def fields_with(criteria)
|
def fields_with(criteria)
|
||||||
value = Hash(String,String).new
|
value = Hash(String,String).new
|
||||||
if String === criteria
|
if String === criteria
|
||||||
|
@ -85,4 +51,38 @@ class MechanizeCr::Form
|
||||||
raise MechanizeCr::ElementNotFoundError.new(:field, criteria) if f.nil?
|
raise MechanizeCr::ElementNotFoundError.new(:field, criteria) if f.nil?
|
||||||
f.first
|
f.first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def parse
|
||||||
|
@fields = Array(MechanizeCr::FormContent::Field).new
|
||||||
|
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
|
||||||
|
@node.css("input").not_nil!.each do |html_node|
|
||||||
|
html_node = html_node.as(Myhtml::Node)
|
||||||
|
@fields << MechanizeCr::FormContent::Field.new(html_node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private 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
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
checkboxes.each do |elm|
|
||||||
|
if elm.checked
|
||||||
|
successful_controls << elm
|
||||||
|
end
|
||||||
|
end
|
||||||
|
successful_controls.each do |ctrl|
|
||||||
|
value = ctrl.query_value
|
||||||
|
query.push(value)
|
||||||
|
end
|
||||||
|
query
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue