add form and form_field class

master
Kanezoh 2021-05-05 20:52:06 +09:00
parent 470fdacee4
commit 085c94b93c
4 changed files with 46 additions and 2 deletions

View File

@ -3,7 +3,8 @@ require "./src/mechanize.cr"
agent = Mechanize.new agent = Mechanize.new
agent.request_headers = HTTP::Headers{"Foo" => "Bar"} agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
params = {"hoge" => "hoge"} 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.code
#puts page.body #puts page.body
#puts page.css("h1").first.inner_text #puts page.css("h1").first.inner_text

View File

@ -1,5 +1,5 @@
require "./mechanize/http/agent" require "./mechanize/http/agent"
require "./mechanize/page" require "./mechanize/form"
class Mechanize class Mechanize
VERSION = "0.1.0" VERSION = "0.1.0"
@ -17,6 +17,15 @@ class Mechanize
page page
end 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 def request_headers
@agent.request_headers @agent.request_headers
end end

20
src/mechanize/form.cr Normal file
View File

@ -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

View File

@ -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