commit
a247432bc4
|
@ -8,9 +8,10 @@ require "./form/button"
|
|||
require "./form/select_list"
|
||||
require "./utils/element_matcher"
|
||||
|
||||
# This class represents the form tag of html.
|
||||
class Mechanize::Form
|
||||
include Mechanize::ElementMatcher
|
||||
class Mechanize
|
||||
# This class represents the form tag of html.
|
||||
class Form
|
||||
include ElementMatcher
|
||||
|
||||
getter node : Node | Lexbor::Node
|
||||
# returns hoge array of `Mechanize::FormContent::Field` in the form.
|
||||
|
@ -32,20 +33,20 @@ class Mechanize::Form
|
|||
# return form's 'action' attribute.
|
||||
property action : String
|
||||
# returns the page which includes the form.
|
||||
getter page : Mechanize::Page?
|
||||
getter page : Page?
|
||||
|
||||
def initialize(node : Node | Lexbor::Node, page : Mechanize::Page? = nil)
|
||||
def initialize(node : Node | Lexbor::Node, page : Page? = nil)
|
||||
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
|
||||
@node = node
|
||||
@fields = Array(Mechanize::FormContent::Field).new
|
||||
@checkboxes = Array(Mechanize::FormContent::CheckBox).new
|
||||
@radiobuttons = Array(Mechanize::FormContent::RadioButton).new
|
||||
@selectboxes = Array(Mechanize::FormContent::MultiSelectList).new
|
||||
@buttons = Array(Mechanize::FormContent::Button).new
|
||||
@fields = Array(FormContent::Field).new
|
||||
@checkboxes = Array(FormContent::CheckBox).new
|
||||
@radiobuttons = Array(FormContent::RadioButton).new
|
||||
@selectboxes = Array(FormContent::MultiSelectList).new
|
||||
@buttons = Array(FormContent::Button).new
|
||||
@action = node.fetch("action", "")
|
||||
@method = node.fetch("method", "GET").upcase
|
||||
@name = node.fetch("name", "")
|
||||
@clicked_buttons = Array(Mechanize::FormContent::Button).new
|
||||
@clicked_buttons = Array(FormContent::Button).new
|
||||
@page = page
|
||||
# @mech = mech
|
||||
|
||||
|
@ -68,7 +69,7 @@ class Mechanize::Form
|
|||
elements_with "checkbox", "checkboxes"
|
||||
elements_with "button"
|
||||
|
||||
# Returns all fields of <input type="textarea">
|
||||
# Returns all fields of <input type="textarea">
|
||||
def textareas
|
||||
fields.select { |f| f.class == FormContent::Textarea }.map &.as(FormContent::Textarea)
|
||||
end
|
||||
|
@ -79,25 +80,25 @@ class Mechanize::Form
|
|||
type = (html_node["type"]? || "text").downcase
|
||||
case type
|
||||
when "checkbox"
|
||||
checkboxes << Mechanize::FormContent::CheckBox.new(html_node, self)
|
||||
checkboxes << FormContent::CheckBox.new(html_node, self)
|
||||
when "radio"
|
||||
radiobuttons << Mechanize::FormContent::RadioButton.new(html_node, self)
|
||||
radiobuttons << FormContent::RadioButton.new(html_node, self)
|
||||
when "button"
|
||||
buttons << Mechanize::FormContent::Button.new(html_node, @node)
|
||||
buttons << FormContent::Button.new(html_node, @node)
|
||||
when "submit"
|
||||
buttons << Mechanize::FormContent::SubmitButton.new(html_node, @node)
|
||||
buttons << FormContent::SubmitButton.new(html_node, @node)
|
||||
when "reset"
|
||||
buttons << Mechanize::FormContent::ResetButton.new(html_node, @node)
|
||||
buttons << FormContent::ResetButton.new(html_node, @node)
|
||||
when "image"
|
||||
buttons << Mechanize::FormContent::ImageButton.new(html_node, @node)
|
||||
buttons << FormContent::ImageButton.new(html_node, @node)
|
||||
when "text"
|
||||
fields << Mechanize::FormContent::Text.new(html_node)
|
||||
fields << FormContent::Text.new(html_node)
|
||||
when "hidden"
|
||||
fields << Mechanize::FormContent::Hidden.new(html_node)
|
||||
fields << FormContent::Hidden.new(html_node)
|
||||
when "textarea"
|
||||
fields << Mechanize::FormContent::Textarea.new(html_node)
|
||||
fields << FormContent::Textarea.new(html_node)
|
||||
else
|
||||
fields << Mechanize::FormContent::Field.new(html_node)
|
||||
fields << FormContent::Field.new(html_node)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -160,7 +161,7 @@ class Mechanize::Form
|
|||
# raise Mechanize::Error,
|
||||
# "radiobuttons #{values} are checked in the #{name} group, " \
|
||||
# "only one is allowed"
|
||||
raise Mechanize::Error.new
|
||||
raise Error.new
|
||||
else
|
||||
successful_controls << checked.first unless checked.empty?
|
||||
end
|
||||
|
@ -201,4 +202,5 @@ class Mechanize::Form
|
|||
|
||||
@clicked_buttons << button
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# This class represents button related html element.
|
||||
# <button>, and <input> whose type is button, reset, image, submit.
|
||||
# <button>, and <input> whose type is button, reset, image, submit.
|
||||
class Mechanize::FormContent::Button < Mechanize::FormContent::Field
|
||||
getter form_node : Node | Lexbor::Node
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# This class represents <input type="checkbox">
|
||||
# This class represents <input type="checkbox">
|
||||
class Mechanize::FormContent::CheckBox < Mechanize::FormContent::RadioButton
|
||||
# set checkbox checked
|
||||
def check
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# This class represents <input> elements in the form.
|
||||
# This class represents <input> elements in the form.
|
||||
class Mechanize::FormContent::Field
|
||||
# returns field's 'value' attribute
|
||||
property value : String?
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# This class represents <input type="hidden">
|
||||
# This class represents <input type="hidden">
|
||||
class Mechanize::FormContent::Hidden < Mechanize::FormContent::Field
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# This class represents <input type="image">
|
||||
# This class represents <input type="image">
|
||||
class Mechanize::FormContent::ImageButton < Mechanize::FormContent::Button
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require "./option"
|
||||
|
||||
# This class represents <select multiple>
|
||||
# This class represents <select multiple>
|
||||
class Mechanize::FormContent::MultiSelectList
|
||||
getter node : Lexbor::Node
|
||||
getter name : String
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
# This class represents <option> of <select>
|
||||
class Mechanize::FormContent::Option
|
||||
getter select_list : FormContent::MultiSelectList
|
||||
getter node : Lexbor::Node
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# This class represents <input type="radio">
|
||||
# This class represents <input type="radio">
|
||||
class Mechanize::FormContent::RadioButton < Mechanize::FormContent::Field
|
||||
property :checked, :form
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# This class represents <input type="reset">
|
||||
# This class represents <input type="reset">
|
||||
class Mechanize::FormContent::ResetButton < Mechanize::FormContent::Button
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require "./multi_select_list"
|
||||
|
||||
# This class represents <select> which is not multiple
|
||||
# This class represents <select> which is not multiple
|
||||
class Mechanize::FormContent::SelectList < Mechanize::FormContent::MultiSelectList
|
||||
def initialize(node)
|
||||
super node
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# This class represents <input type="submit">
|
||||
# This class represents <input type="submit">
|
||||
class Mechanize::FormContent::SubmitButton < Mechanize::FormContent::Button
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# This class represents <input type="text">
|
||||
# This class represents <input type="text">
|
||||
class Mechanize::FormContent::Text < Mechanize::FormContent::Field
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# This class represents <input type="textarea">
|
||||
# This class represents <input type="textarea">
|
||||
class Mechanize::FormContent::Textarea < Mechanize::FormContent::Field
|
||||
end
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
require "./page"
|
||||
|
||||
# This class represents the history of http response you sent.
|
||||
# If you send a request, mechanize saves the history.
|
||||
class Mechanize
|
||||
# This class represents the history of http response you sent.
|
||||
# If you send a request, mechanize saves the history.
|
||||
class History
|
||||
# max page size history can save. default is 100.
|
||||
# as same as `agent.max_history`.
|
||||
property max_size : Int32
|
||||
property array : Array(Mechanize::Page)
|
||||
property array : Array(Page)
|
||||
|
||||
delegate :size, :empty?, :last, to: array
|
||||
|
||||
def initialize(max_size = 100, array = Array(Mechanize::Page).new)
|
||||
def initialize(max_size = 100, array = Array(Page).new)
|
||||
@max_size = max_size
|
||||
@array = array
|
||||
end
|
||||
|
|
|
@ -5,15 +5,16 @@ require "../history"
|
|||
|
||||
class Mechanize
|
||||
module HTTP
|
||||
# This class handles web interation mechanize made.
|
||||
class Agent
|
||||
property request_headers : ::HTTP::Headers
|
||||
property context : Mechanize?
|
||||
property history : Mechanize::History
|
||||
property history : History
|
||||
property user_agent : String
|
||||
property request_cookies : ::HTTP::Cookies
|
||||
|
||||
def initialize(@context : Mechanize? = nil)
|
||||
@history = Mechanize::History.new
|
||||
@history = History.new
|
||||
@request_headers = ::HTTP::Headers.new
|
||||
@context = context
|
||||
@request_cookies = ::HTTP::Cookies.new
|
||||
|
@ -72,7 +73,7 @@ class Mechanize
|
|||
# ```
|
||||
# agent.current_page
|
||||
# ```
|
||||
def current_page : Mechanize::Page
|
||||
def current_page : Page
|
||||
@history.last
|
||||
end
|
||||
|
||||
|
@ -80,7 +81,7 @@ class Mechanize
|
|||
# ```
|
||||
# agent.back
|
||||
# ```
|
||||
def back : Mechanize::Page
|
||||
def back : Page
|
||||
@history.pop
|
||||
end
|
||||
|
||||
|
@ -115,7 +116,7 @@ class Mechanize
|
|||
end
|
||||
|
||||
# Sets a Referer header.
|
||||
def set_request_referer(referer : Mechanize::Page?)
|
||||
def set_request_referer(referer : Page?)
|
||||
return unless referer
|
||||
|
||||
request_headers["Referer"] = referer.uri.to_s
|
||||
|
|
|
@ -2,12 +2,12 @@ require "./file"
|
|||
require "./utils/element_matcher"
|
||||
require "./page/link"
|
||||
|
||||
# This class represents the result of http response.
|
||||
# If you send a request, it returns the instance of `Mechanize::Page`.
|
||||
# You can get status code, title, and page body, and search html node using css selector from page instance.
|
||||
class Mechanize
|
||||
# This class represents the result of http response.
|
||||
# If you send a request, it returns the instance of `Mechanize::Page`.
|
||||
# You can get status code, title, and page body, and search html node using css selector from page instance.
|
||||
class Page < Mechanize::File
|
||||
include Mechanize::ElementMatcher
|
||||
include ElementMatcher
|
||||
|
||||
# look at lexbor document.(https://github.com/kostya/lexbor#readme)
|
||||
delegate :css, to: parser
|
||||
|
@ -42,7 +42,7 @@ class Mechanize
|
|||
# ```
|
||||
# page.forms # => Array(Mechanize::Form)
|
||||
# ```
|
||||
def forms : Array(Mechanize::Form)
|
||||
def forms : Array(Form)
|
||||
forms = css("form").map do |html_form|
|
||||
form = Form.new(html_form, self)
|
||||
form.action ||= @uri.to_s
|
||||
|
@ -54,7 +54,7 @@ class Mechanize
|
|||
# ```
|
||||
# page.links # => Array(Mechanize::PageContent::Link)
|
||||
# ```
|
||||
def links : Array(Mechanize::PageContent::Link)
|
||||
def links : Array(PageContent::Link)
|
||||
links = %w{a area}.map do |tag|
|
||||
css(tag).map do |node|
|
||||
PageContent::Link.new(node, @mech, self)
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
# This class represents link element like <a> and <area>.
|
||||
# The instance of this class is clickable.
|
||||
class Mechanize::PageContent::Link
|
||||
class Mechanize
|
||||
class PageContent::Link
|
||||
getter node : Lexbor::Node
|
||||
getter page : Mechanize::Page
|
||||
getter page : Page
|
||||
getter mech : Mechanize
|
||||
getter href : String
|
||||
getter text : String
|
||||
|
@ -19,4 +20,5 @@ class Mechanize::PageContent::Link
|
|||
def click
|
||||
@mech.click self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -65,7 +65,7 @@ class Mechanize
|
|||
def {{singular.id}}_with(criteria)
|
||||
f = {{plural.id}}_with(criteria)
|
||||
# TODO: Write correct error message.
|
||||
raise Mechanize::ElementNotFoundError.new(:{{singular.id}}, "") if f.empty?
|
||||
raise ElementNotFoundError.new(:{{singular.id}}, "") if f.empty?
|
||||
f.first
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue