implementing page#forms and fields
parent
2813e989a9
commit
cbcf025c33
4
main.cr
4
main.cr
|
@ -3,8 +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("https://kowabana.jp/users/sign_in/", params: params)
|
page = agent.get("http://example.com/", params: params)
|
||||||
page.forms
|
form = page.forms[0]
|
||||||
#query = {"foo" => "foo_value", "bar" => "bar_value"}
|
#query = {"foo" => "foo_value", "bar" => "bar_value"}
|
||||||
#page = agent.post("http://example.com/", query: query)
|
#page = agent.post("http://example.com/", query: query)
|
||||||
#puts page.code
|
#puts page.code
|
||||||
|
|
|
@ -51,7 +51,9 @@ class MechanizeCr::Form
|
||||||
def parse
|
def parse
|
||||||
@fields = Array(MechanizeCr::FormContent::Field).new
|
@fields = Array(MechanizeCr::FormContent::Field).new
|
||||||
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
|
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
|
||||||
@node.css("input").not_nil!.each do |node|
|
@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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,4 +63,24 @@ class MechanizeCr::Form
|
||||||
acc + URI::Params.encode(hash) + '&'
|
acc + URI::Params.encode(hash) + '&'
|
||||||
end.rchop
|
end.rchop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fields_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
|
||||||
|
fields.select do |field|
|
||||||
|
value.all? do |k,v|
|
||||||
|
v === field.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def field_with(criteria)
|
||||||
|
f = fields_with(criteria)
|
||||||
|
f.empty? ? Array(MechanizeCr::FormContent::Field).new : f.first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,4 +13,5 @@ struct Myhtml::Node
|
||||||
delegate :[], to: attributes
|
delegate :[], to: attributes
|
||||||
delegate :[]=, to: attributes
|
delegate :[]=, to: attributes
|
||||||
delegate :[]?, to: attributes
|
delegate :[]?, to: attributes
|
||||||
|
delegate :fetch, to: attributes
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,16 +16,10 @@ class MechanizeCr::Page < MechanizeCr::File
|
||||||
end
|
end
|
||||||
|
|
||||||
def forms
|
def forms
|
||||||
#@forms ||= css("form").map do |html_form|
|
forms = css("form").map do |html_form|
|
||||||
# form = Mechanize::Form.new(html_form, @mech, self)
|
|
||||||
# form.attributes["action"]# ||= @uri.to_s
|
|
||||||
# form
|
|
||||||
#end
|
|
||||||
|
|
||||||
forms = css("form").each do |html_form|
|
|
||||||
form = MechanizeCr::Form.new(html_form)
|
form = MechanizeCr::Form.new(html_form)
|
||||||
puts form.action# ||= @uri.to_s
|
form.action ||= @uri.to_s
|
||||||
form
|
form
|
||||||
end
|
end.to_a
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue