diff --git a/src/mechanize.cr b/src/mechanize.cr index bca31f3..bbec91a 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -1,3 +1,6 @@ +require "uri" +require "http/client" +require "lexbor" require "./mechanize/http/agent" require "./mechanize/form" require "./mechanize/node" @@ -73,13 +76,13 @@ class Mechanize def post(uri : String | URI, headers = ::HTTP::Headers.new, query : Hash(String, String | Array(String)) = Hash(String, String).new) : Mechanize::Page - node = Node.new + node = Mechanize::Node.new node["method"] = "POST" node["enctype"] = "application/x-www-form-urlencoded" form = Mechanize::Form.new(node) query.each do |k, v| - node = Node.new + node = Mechanize::Node.new node["name"] = k form.fields << Mechanize::FormContent::Field.new(node, v) end diff --git a/src/mechanize/file.cr b/src/mechanize/file.cr index 9ec0b40..be8436c 100644 --- a/src/mechanize/file.cr +++ b/src/mechanize/file.cr @@ -1,5 +1,3 @@ -require "http/client" - class Mechanize abstract class File # property :filename diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index fc5e254..0a7fd18 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -13,7 +13,7 @@ class Mechanize class Form include ElementMatcher - getter node : Node | Lexbor::Node + getter node : Mechanize::Node | Lexbor::Node # returns hoge array of `Mechanize::FormContent::Field` in the form. getter fields : Array(FormContent::Field) # returns an array of input tags whose type is checkbox in the form. @@ -35,7 +35,7 @@ class Mechanize # returns the page which includes the form. getter page : Page? - def initialize(node : Node | Lexbor::Node, page : Page? = nil) + def initialize(node : Mechanize::Node | Lexbor::Node, page : Page? = nil) @enctype = node.fetch("enctype", "application/x-www-form-urlencoded") @node = node @fields = Array(FormContent::Field).new diff --git a/src/mechanize/form/button.cr b/src/mechanize/form/button.cr index dab59f5..2cdc247 100644 --- a/src/mechanize/form/button.cr +++ b/src/mechanize/form/button.cr @@ -1,9 +1,9 @@ # This class represents button related html element. # <button>, and <input> whose type is button, reset, image, submit. class Mechanize::FormContent::Button < Mechanize::FormContent::Field - getter form_node : Node | Lexbor::Node + getter form_node : Mechanize::Node | Lexbor::Node - def initialize(node : Node | Lexbor::Node, form_node : Node | Lexbor::Node, value = nil) + def initialize(node : Mechanize::Node | Lexbor::Node, form_node : Mechanize::Node | Lexbor::Node, value = nil) @form_node = form_node super(node, value) end diff --git a/src/mechanize/form/field.cr b/src/mechanize/form/field.cr index 62166d9..4c12bb4 100644 --- a/src/mechanize/form/field.cr +++ b/src/mechanize/form/field.cr @@ -9,9 +9,9 @@ class Mechanize::FormContent::Field # returns field's 'value' attribute. # value property is changeable, but this property stores raw value. getter raw_value : String? - getter node : Node | Lexbor::Node + getter node : Mechanize::Node | Lexbor::Node - def initialize(node : Node | Lexbor::Node, value = nil) + def initialize(node : Mechanize::Node | Lexbor::Node, value = nil) @node = node @name = node.fetch("name", "") @value = value || node.fetch("value", nil) diff --git a/src/mechanize/form/radio_button.cr b/src/mechanize/form/radio_button.cr index efb0b48..085bc70 100644 --- a/src/mechanize/form/radio_button.cr +++ b/src/mechanize/form/radio_button.cr @@ -2,7 +2,7 @@ class Mechanize::FormContent::RadioButton < Mechanize::FormContent::Field property :checked, :form - def initialize(node : Node | Lexbor::Node, form : Form) + def initialize(node : Mechanize::Node | Lexbor::Node, form : Form) @checked = !!node.fetch("checked", nil) @form = form super(node) diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 7a94879..c7fe6f9 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -1,5 +1,3 @@ -require "uri" -require "http/client" require "../cookie" require "../history" diff --git a/src/mechanize/node.cr b/src/mechanize/node.cr index 4915952..5367aaa 100644 --- a/src/mechanize/node.cr +++ b/src/mechanize/node.cr @@ -1,21 +1,12 @@ -require "lexbor" +class Mechanize + # This is a fake node used when sending post request. + class Node < Hash(String, String) + def css(str) + [] of Hash(String, String) + end -# This is a fake node used when sending post request. -class Node < Hash(String, String) - def css(str) - [] of Hash(String, String) - end - - def inner_text - "" + def inner_text + "" + end end end - -# This is a real Node got from html. -struct Lexbor::Node - delegate :[], to: attributes - delegate :[]=, to: attributes - delegate :[]?, to: attributes - delegate :fetch, to: attributes - delegate :has_key?, to: attributes -end