From 5429d896833bc12cebd0a455526213f8ed4089d4 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 18 May 2021 22:00:04 +0900 Subject: [PATCH] add checkbox --- src/mechanize.cr | 3 ++- src/mechanize/form.cr | 33 +++++++++++++++++++++++++++++++-- src/mechanize/form/check_box.cr | 27 +++++++++++++++++++++++++++ src/mechanize/node.cr | 13 +++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/mechanize/form/check_box.cr create mode 100644 src/mechanize/node.cr diff --git a/src/mechanize.cr b/src/mechanize.cr index b84f256..315826d 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -1,5 +1,6 @@ require "./mechanize/http/agent" require "./mechanize/form" +require "./mechanize/node" class Mechanize VERSION = "0.1.0" @@ -18,7 +19,7 @@ class Mechanize end def post(uri : String | URI, headers = HTTP::Headers.new, query : Hash(String, String | Array(String)) = Hash(String,String).new) - node = Hash(String, String).new + node = Node.new(fake: true) node["method"] = "POST" node["enctype"] = "application/x-www-form-urlencoded" diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 235cc81..37cccd1 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -1,11 +1,15 @@ require "./form/field" +require "./form/check_box" + class MechanizeCr::Form getter fields : Array(MechanizeCr::FormContent::Field) + getter checkboxes : Array(MechanizeCr::FormContent::CheckBox) - def initialize(node : Hash(String, String)) + def initialize(node : Node) @enctype = node["enctype"] || "application/x-www-form-urlencoded" @node = node @fields = Array(MechanizeCr::FormContent::Field).new + @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new #@action = Mechanize::Util.html_unescape(node['action']) #@method = (node['method'] || 'GET').upcase #@name = node['name'] @@ -15,6 +19,31 @@ class MechanizeCr::Form # #@encoding = node['accept-charset'] || (page && page.encoding) || nil #@ignore_encoding_error = false - #parse + parse + request_data + end + + def request_data + query_params = build_query + end + + def build_query() + query = [] of String + successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new + fields.each do |elm| + successful_controls.push(elm) + end + checkboxes.each do |elm| + if elm.checked + successful_controls << elm + end + end + end + + def parse + @fields = Array(MechanizeCr::FormContent::Field).new + @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new + @node.search("input").not_nil!.each do |node| + end end end diff --git a/src/mechanize/form/check_box.cr b/src/mechanize/form/check_box.cr new file mode 100644 index 0000000..0412978 --- /dev/null +++ b/src/mechanize/form/check_box.cr @@ -0,0 +1,27 @@ +class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::Field + property :checked + property :form + + def initialize(node : Node, value : String = node["value"]) + @checked = !!node["checked"] + @form = form + super(node) + end + + def check + #uncheck_peers + @checked = true + end + + def uncheck + @checked = false + end + + def checked? + checked + end + + def click + checked ? uncheck : check + end +end diff --git a/src/mechanize/node.cr b/src/mechanize/node.cr new file mode 100644 index 0000000..168d1dd --- /dev/null +++ b/src/mechanize/node.cr @@ -0,0 +1,13 @@ +class Node < Hash(String,String) + property fake : Bool + def initialize(fake = false) + @fake = fake + super() + end + + def search(str) + if fake + [] of Hash(String,String) + end + end +end