From 085c94b93cf2977d0d23ef3f3f81dcfb54a4bc93 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Wed, 5 May 2021 20:52:06 +0900 Subject: [PATCH] add form and form_field class --- main.cr | 3 ++- src/mechanize.cr | 11 ++++++++++- src/mechanize/form.cr | 20 ++++++++++++++++++++ src/mechanize/form/field.cr | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/mechanize/form.cr create mode 100644 src/mechanize/form/field.cr diff --git a/main.cr b/main.cr index 4e53c72..6ae6618 100644 --- a/main.cr +++ b/main.cr @@ -3,7 +3,8 @@ require "./src/mechanize.cr" agent = Mechanize.new agent.request_headers = HTTP::Headers{"Foo" => "Bar"} params = {"hoge" => "hoge"} -page = agent.get("http://example.com/", params: params) +#page = agent.get("http://example.com/", params: params) +page = agent.post("http//example.com/") #puts page.code #puts page.body #puts page.css("h1").first.inner_text diff --git a/src/mechanize.cr b/src/mechanize.cr index 325d920..46aec5b 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -1,5 +1,5 @@ require "./mechanize/http/agent" -require "./mechanize/page" +require "./mechanize/form" class Mechanize VERSION = "0.1.0" @@ -17,6 +17,15 @@ class Mechanize page 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["method"] = "POST" + node["enctype"] = "application/x-www-form-urlencoded" + + form = MechanizeCr::Form.new(node) + form.fields << MechanizeCr::FormContent::Field.new({"name" => "foo"}, "bar") + end + def request_headers @agent.request_headers end diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr new file mode 100644 index 0000000..235cc81 --- /dev/null +++ b/src/mechanize/form.cr @@ -0,0 +1,20 @@ +require "./form/field" +class MechanizeCr::Form + getter fields : Array(MechanizeCr::FormContent::Field) + + def initialize(node : Hash(String, String)) + @enctype = node["enctype"] || "application/x-www-form-urlencoded" + @node = node + @fields = Array(MechanizeCr::FormContent::Field).new + #@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 + #parse + end +end diff --git a/src/mechanize/form/field.cr b/src/mechanize/form/field.cr new file mode 100644 index 0000000..74141aa --- /dev/null +++ b/src/mechanize/form/field.cr @@ -0,0 +1,14 @@ +class MechanizeCr::FormContent::Field + def initialize(node : Hash(String, String), value : String = node["value"]) + @node = node + #@name = Mechanize::Util.html_unescape(node['name']) + #@raw_value = value + #@value = if value.is_a? String + # Mechanize::Util.html_unescape(value) + # else + # value + # end +# + #@type = node['type'] + end +end