MechanizeCr -> Mechanize

This commit is contained in:
Anton Maminov 2021-11-15 22:09:38 +02:00
parent 114a7fbcbe
commit a20b63a515
24 changed files with 153 additions and 143 deletions

View File

@ -27,7 +27,7 @@ describe "Form Fields Select List" do
form.selectboxes.size.should eq 1 form.selectboxes.size.should eq 1
end end
selectbox = form.selectboxes[0].as(MechanizeCr::FormContent::SelectList) selectbox = form.selectboxes[0].as(Mechanize::FormContent::SelectList)
it "returns selectbox options size" do it "returns selectbox options size" do
selectbox.options.size.should eq 3 selectbox.options.size.should eq 3

View File

@ -35,7 +35,7 @@ class Mechanize
} }
def initialize def initialize
@agent = MechanizeCr::HTTP::Agent.new @agent = Mechanize::HTTP::Agent.new
@agent.context = self @agent.context = self
@agent.user_agent = USER_AGENT["Mechanize"] @agent.user_agent = USER_AGENT["Mechanize"]
end end
@ -51,8 +51,8 @@ class Mechanize
# headers: HTTP::Headers{"Foo" => "Bar"}) # headers: HTTP::Headers{"Foo" => "Bar"})
# ``` # ```
def get(uri : String | URI, def get(uri : String | URI,
headers = HTTP::Headers.new, headers = ::HTTP::Headers.new,
params : Hash(String, String | Array(String)) = Hash(String, String).new) : MechanizeCr::Page params : Hash(String, String | Array(String)) = Hash(String, String).new) : Mechanize::Page
method = :get method = :get
page = @agent.fetch uri, method, headers, params page = @agent.fetch uri, method, headers, params
add_to_history(page) add_to_history(page)
@ -71,17 +71,17 @@ class Mechanize
# headers: HTTP::Headers{"Foo" => "Bar"}) # headers: HTTP::Headers{"Foo" => "Bar"})
# ``` # ```
def post(uri : String | URI, def post(uri : String | URI,
headers = HTTP::Headers.new, headers = ::HTTP::Headers.new,
query : Hash(String, String | Array(String)) = Hash(String, String).new) : MechanizeCr::Page query : Hash(String, String | Array(String)) = Hash(String, String).new) : Mechanize::Page
node = Node.new node = Node.new
node["method"] = "POST" node["method"] = "POST"
node["enctype"] = "application/x-www-form-urlencoded" node["enctype"] = "application/x-www-form-urlencoded"
form = MechanizeCr::Form.new(node) form = Mechanize::Form.new(node)
query.each do |k, v| query.each do |k, v|
node = Node.new node = Node.new
node["name"] = k node["name"] = k
form.fields << MechanizeCr::FormContent::Field.new(node, v) form.fields << Mechanize::FormContent::Field.new(node, v)
end end
post_form(uri, form, headers) post_form(uri, form, headers)
end end
@ -125,18 +125,18 @@ class Mechanize
# get the page mechanize last visited. # get the page mechanize last visited.
# #
# ``` # ```
# agent.current_page => #<MechanizeCr::Page> # agent.current_page => #<Mechanize::Page>
# ``` # ```
def current_page : MechanizeCr::Page def current_page : Mechanize::Page
@agent.current_page @agent.current_page
end end
# get the latest page recorded in history, and the page is deleted from history. # get the latest page recorded in history, and the page is deleted from history.
# #
# ``` # ```
# agent.back => #<MechanizeCr::Page> # agent.back => #<Mechanize::Page>
# ``` # ```
def back : MechanizeCr::Page def back : Mechanize::Page
@agent.history.pop @agent.history.pop
end end
@ -151,7 +151,7 @@ class Mechanize
# form.field_with("foo").value = "bar" # form.field_with("foo").value = "bar"
# agent.submit(form) # agent.submit(form)
# ``` # ```
def submit(form, button = nil) : MechanizeCr::Page? def submit(form, button = nil) : Mechanize::Page?
form.add_button_to_query(button) if button form.add_button_to_query(button) if button
case form.method.upcase case form.method.upcase
when "POST" when "POST"
@ -162,19 +162,19 @@ class Mechanize
# parse response. it is used internally. # parse response. it is used internally.
def parse(uri, response, body) def parse(uri, response, body)
code = response.not_nil!.status_code code = response.not_nil!.status_code
MechanizeCr::Page.new(uri, response, body, code, self) Mechanize::Page.new(uri, response, body, code, self)
end end
# get the history (`MechanizeCr::History`). # get the history (`Mechanize::History`).
# the requests mechanize send is recorded in this history. # the requests mechanize send is recorded in this history.
# ``` # ```
# agent.history => #<MechanizeCr::History> # agent.history => #<Mechanize::History>
# ``` # ```
def history : MechanizeCr::History def history : Mechanize::History
@agent.history @agent.history
end end
# add page to history (`MechanizeCr::History`). # add page to history (`Mechanize::History`).
# #
# if you send request, mechanize calls this method and records page, # if you send request, mechanize calls this method and records page,
# so you don't need to call this on your own. # so you don't need to call this on your own.
@ -207,7 +207,7 @@ class Mechanize
# link = page.links.first # link = page.links.first
# page2 = agent.click(link) # page2 = agent.click(link)
# ``` # ```
def click(link : MechanizeCr::PageContent::Link) : MechanizeCr::Page def click(link : Mechanize::PageContent::Link) : Mechanize::Page
href = link.href href = link.href
get href get href
end end
@ -219,13 +219,13 @@ class Mechanize
# ``` # ```
def download(uri : URI | String, def download(uri : URI | String,
filename : String, filename : String,
headers = HTTP::Headers.new, headers = ::HTTP::Headers.new,
params : Hash(String, String | Array(String)) = Hash(String, String).new) params : Hash(String, String | Array(String)) = Hash(String, String).new)
transact do transact do
page = get(uri, headers, params) page = get(uri, headers, params)
case page case page
when MechanizeCr::File when Mechanize::File
File.write(filename, page.body) ::File.write(filename, page.body)
end end
end end
end end
@ -233,7 +233,7 @@ class Mechanize
# Runs given block, then resets the page history as it was before. # Runs given block, then resets the page history as it was before.
private def transact private def transact
# save the previous history status. # save the previous history status.
history_backup = MechanizeCr::History.new(@agent.history.max_size, @agent.history.array.dup) history_backup = Mechanize::History.new(@agent.history.max_size, @agent.history.array.dup)
begin begin
yield self yield self
ensure ensure
@ -243,7 +243,7 @@ class Mechanize
end end
# send POST request from form. # send POST request from form.
private def post_form(uri, form, headers) : MechanizeCr::Page private def post_form(uri, form, headers) : Mechanize::Page
cur_page = form.page || (current_page unless history.empty?) cur_page = form.page || (current_page unless history.empty?)
request_data = form.request_data request_data = form.request_data

View File

@ -1,3 +1,5 @@
# Base Error class # Base Error class
class MechanizeCr::Error < RuntimeError class Mechanize
class Error < RuntimeError
end
end end

View File

@ -1,6 +1,6 @@
require "./base_error" require "./base_error"
class MechanizeCr::ElementNotFoundError < MechanizeCr::Error class Mechanize::ElementNotFoundError < Mechanize::Error
getter element : Symbol getter element : Symbol
getter conditions : String getter conditions : String

View File

@ -1,6 +1,7 @@
require "http/client" require "http/client"
class MechanizeCr::File class Mechanize
class File
# property :body, :filename # property :body, :filename
property :body, :code, uri, :response property :body, :code, uri, :response
@ -10,3 +11,4 @@ class MechanizeCr::File
@code = code @code = code
end end
end end
end

View File

@ -8,33 +8,33 @@ require "./form/button"
require "./form/select_list" require "./form/select_list"
require "./utils/element_matcher" require "./utils/element_matcher"
class MechanizeCr::Form class Mechanize::Form
include MechanizeCr::ElementMatcher include Mechanize::ElementMatcher
getter node : Node | Lexbor::Node getter node : Node | Lexbor::Node
getter fields : Array(FormContent::Field) getter fields : Array(Mechanize::FormContent::Field)
getter checkboxes : Array(FormContent::CheckBox) getter checkboxes : Array(Mechanize::FormContent::CheckBox)
getter radiobuttons : Array(FormContent::RadioButton) getter radiobuttons : Array(Mechanize::FormContent::RadioButton)
getter selectboxes : Array(FormContent::MultiSelectList) getter selectboxes : Array(Mechanize::FormContent::MultiSelectList)
getter buttons : Array(FormContent::Button) getter buttons : Array(Mechanize::FormContent::Button)
getter enctype : String getter enctype : String
getter method : String getter method : String
getter name : String getter name : String
getter page : Page? getter page : Mechanize::Page?
property action : String property action : String
def initialize(node : Node | Lexbor::Node, page : Page? = nil) def initialize(node : Node | Lexbor::Node, page : Mechanize::Page? = nil)
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded") @enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
@node = node @node = node
@fields = Array(FormContent::Field).new @fields = Array(Mechanize::FormContent::Field).new
@checkboxes = Array(FormContent::CheckBox).new @checkboxes = Array(Mechanize::FormContent::CheckBox).new
@radiobuttons = Array(FormContent::RadioButton).new @radiobuttons = Array(Mechanize::FormContent::RadioButton).new
@selectboxes = Array(FormContent::MultiSelectList).new @selectboxes = Array(Mechanize::FormContent::MultiSelectList).new
@buttons = Array(FormContent::Button).new @buttons = Array(Mechanize::FormContent::Button).new
@action = node.fetch("action", "") @action = node.fetch("action", "")
@method = node.fetch("method", "GET").upcase @method = node.fetch("method", "GET").upcase
@name = node.fetch("name", "") @name = node.fetch("name", "")
@clicked_buttons = Array(FormContent::Button).new @clicked_buttons = Array(Mechanize::FormContent::Button).new
@page = page @page = page
# @mech = mech # @mech = mech
@ -68,25 +68,25 @@ class MechanizeCr::Form
type = (html_node["type"]? || "text").downcase type = (html_node["type"]? || "text").downcase
case type case type
when "checkbox" when "checkbox"
checkboxes << FormContent::CheckBox.new(html_node, self) checkboxes << Mechanize::FormContent::CheckBox.new(html_node, self)
when "radio" when "radio"
radiobuttons << FormContent::RadioButton.new(html_node, self) radiobuttons << Mechanize::FormContent::RadioButton.new(html_node, self)
when "button" when "button"
buttons << FormContent::Button.new(html_node, @node) buttons << Mechanize::FormContent::Button.new(html_node, @node)
when "submit" when "submit"
buttons << FormContent::SubmitButton.new(html_node, @node) buttons << Mechanize::FormContent::SubmitButton.new(html_node, @node)
when "reset" when "reset"
buttons << FormContent::ResetButton.new(html_node, @node) buttons << Mechanize::FormContent::ResetButton.new(html_node, @node)
when "image" when "image"
buttons << FormContent::ImageButton.new(html_node, @node) buttons << Mechanize::FormContent::ImageButton.new(html_node, @node)
when "text" when "text"
fields << FormContent::Text.new(html_node) fields << Mechanize::FormContent::Text.new(html_node)
when "hidden" when "hidden"
fields << FormContent::Hidden.new(html_node) fields << Mechanize::FormContent::Hidden.new(html_node)
when "textarea" when "textarea"
fields << FormContent::Textarea.new(html_node) fields << Mechanize::FormContent::Textarea.new(html_node)
else else
fields << FormContent::Field.new(html_node) fields << Mechanize::FormContent::Field.new(html_node)
end end
end end
@ -149,7 +149,7 @@ class MechanizeCr::Form
# raise Mechanize::Error, # raise Mechanize::Error,
# "radiobuttons #{values} are checked in the #{name} group, " \ # "radiobuttons #{values} are checked in the #{name} group, " \
# "only one is allowed" # "only one is allowed"
raise MechanizeCr::Error.new raise Mechanize::Error.new
else else
successful_controls << checked.first unless checked.empty? successful_controls << checked.first unless checked.empty?
end end

View File

@ -1,4 +1,4 @@
class MechanizeCr::FormContent::Button < MechanizeCr::FormContent::Field class Mechanize::FormContent::Button < Mechanize::FormContent::Field
getter form_node : Node | Lexbor::Node getter form_node : Node | Lexbor::Node
def initialize(node : Node | Lexbor::Node, form_node : Node | Lexbor::Node, value = nil) def initialize(node : Node | Lexbor::Node, form_node : Node | Lexbor::Node, value = nil)

View File

@ -1,4 +1,4 @@
class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::RadioButton class Mechanize::FormContent::CheckBox < Mechanize::FormContent::RadioButton
def check def check
@checked = true @checked = true
end end

View File

@ -1,4 +1,4 @@
class MechanizeCr::FormContent::Field class Mechanize::FormContent::Field
property value : String? property value : String?
getter name : String getter name : String
getter type : String getter type : String
@ -29,7 +29,7 @@ class MechanizeCr::FormContent::Field
def inspect # :nodoc: def inspect # :nodoc:
"[%s:0x%x type: %s name: %s value: %s]" % [ "[%s:0x%x type: %s name: %s value: %s]" % [
self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase, self.class.name.sub(/Mechanize::FormContent::/, "").downcase,
object_id, type, name, value, object_id, type, name, value,
] ]
end end

View File

@ -1,2 +1,2 @@
class MechanizeCr::FormContent::Hidden < MechanizeCr::FormContent::Field class Mechanize::FormContent::Hidden < Mechanize::FormContent::Field
end end

View File

@ -1,2 +1,2 @@
class MechanizeCr::FormContent::ImageButton < MechanizeCr::FormContent::Button class Mechanize::FormContent::ImageButton < Mechanize::FormContent::Button
end end

View File

@ -1,6 +1,6 @@
require "./option" require "./option"
class MechanizeCr::FormContent::MultiSelectList class Mechanize::FormContent::MultiSelectList
getter node : Lexbor::Node getter node : Lexbor::Node
getter name : String getter name : String
getter type : String getter type : String
@ -54,7 +54,7 @@ class MechanizeCr::FormContent::MultiSelectList
def inspect # :nodoc: def inspect # :nodoc:
"[%s:0x%x type: %s name: %s values: [%s]]" % [ "[%s:0x%x type: %s name: %s values: [%s]]" % [
self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase, self.class.name.sub(/Mechanize::FormContent::/, "").downcase,
object_id, type, name, values.join(','), object_id, type, name, values.join(','),
] ]
end end

View File

@ -1,4 +1,4 @@
class MechanizeCr::FormContent::Option class Mechanize::FormContent::Option
getter select_list : FormContent::MultiSelectList getter select_list : FormContent::MultiSelectList
getter node : Lexbor::Node getter node : Lexbor::Node
getter text : String getter text : String

View File

@ -1,4 +1,4 @@
class MechanizeCr::FormContent::RadioButton < MechanizeCr::FormContent::Field class Mechanize::FormContent::RadioButton < Mechanize::FormContent::Field
property :checked, :form property :checked, :form
def initialize(node : Node | Lexbor::Node, form : Form) def initialize(node : Node | Lexbor::Node, form : Form)

View File

@ -1,2 +1,2 @@
class MechanizeCr::FormContent::ResetButton < MechanizeCr::FormContent::Button class Mechanize::FormContent::ResetButton < Mechanize::FormContent::Button
end end

View File

@ -1,6 +1,6 @@
require "./multi_select_list" require "./multi_select_list"
class MechanizeCr::FormContent::SelectList < MechanizeCr::FormContent::MultiSelectList class Mechanize::FormContent::SelectList < Mechanize::FormContent::MultiSelectList
def initialize(node) def initialize(node)
super node super node
# only one selected option is allowed # only one selected option is allowed

View File

@ -1,2 +1,2 @@
class MechanizeCr::FormContent::SubmitButton < MechanizeCr::FormContent::Button class Mechanize::FormContent::SubmitButton < Mechanize::FormContent::Button
end end

View File

@ -1,2 +1,2 @@
class MechanizeCr::FormContent::Text < MechanizeCr::FormContent::Field class Mechanize::FormContent::Text < Mechanize::FormContent::Field
end end

View File

@ -1,2 +1,2 @@
class MechanizeCr::FormContent::Textarea < MechanizeCr::FormContent::Field class Mechanize::FormContent::Textarea < Mechanize::FormContent::Field
end end

View File

@ -1,12 +1,13 @@
require "./page" require "./page"
class MechanizeCr::History class Mechanize
class History
property max_size : Int32 property max_size : Int32
property array : Array(MechanizeCr::Page) property array : Array(Mechanize::Page)
delegate :size, :empty?, :last, to: array delegate :size, :empty?, :last, to: array
def initialize(max_size = 100, array = Array(MechanizeCr::Page).new) def initialize(max_size = 100, array = Array(Mechanize::Page).new)
@max_size = max_size @max_size = max_size
@array = array @array = array
end end
@ -26,3 +27,4 @@ class MechanizeCr::History
page = @array.pop page = @array.pop
end end
end end
end

View File

@ -3,16 +3,16 @@ require "http/client"
require "../cookie" require "../cookie"
require "../history" require "../history"
module MechanizeCr class Mechanize
module HTTP module HTTP
class Agent class Agent
property :request_headers, :context property :request_headers, :context
property history : MechanizeCr::History property history : Mechanize::History
property user_agent : String property user_agent : String
property request_cookies : ::HTTP::Cookies property request_cookies : ::HTTP::Cookies
def initialize(@context : Mechanize | Nil = nil) def initialize(@context : Mechanize | Nil = nil)
@history = MechanizeCr::History.new @history = Mechanize::History.new
@request_headers = ::HTTP::Headers.new @request_headers = ::HTTP::Headers.new
@context = context @context = context
@request_cookies = ::HTTP::Cookies.new @request_cookies = ::HTTP::Cookies.new

View File

@ -6,8 +6,9 @@ require "./page/link"
# If you send request, it returns the instance of Page. # If you send request, it returns the instance of Page.
# You can get status code, title, and page body, and search html node using css selector. # You can get status code, title, and page body, and search html node using css selector.
class MechanizeCr::Page < MechanizeCr::File class Mechanize
include MechanizeCr::ElementMatcher class Page < Mechanize::File
include Mechanize::ElementMatcher
delegate :css, to: parser delegate :css, to: parser
property mech : Mechanize property mech : Mechanize
@ -33,23 +34,24 @@ class MechanizeCr::Page < MechanizeCr::File
end end
end end
# return all forms(`MechanizeCr::Form`) in the page. # return all forms(`Mechanize::Form`) in the page.
def forms : Array(MechanizeCr::Form) def forms : Array(Mechanize::Form)
forms = css("form").map do |html_form| forms = css("form").map do |html_form|
form = Form.new(html_form, self) form = Mechanize::Form.new(html_form, self)
form.action ||= @uri.to_s form.action ||= @uri.to_s
form form
end.to_a end.to_a
end end
# return all links(`MechanizeCr::PageContent::Link) in the page. # return all links(`Mechanize::PageContent::Link) in the page.
def links : Array(MechanizeCr::PageContent::Link) def links : Array(Mechanize::PageContent::Link)
links = %w{a area}.map do |tag| links = %w{a area}.map do |tag|
css(tag).map do |node| css(tag).map do |node|
PageContent::Link.new(node, @mech, self) Mechanize::PageContent::Link.new(node, @mech, self)
end end
end.flatten end.flatten
end end
elements_with "form" elements_with "form"
end end
end

View File

@ -1,6 +1,6 @@
class MechanizeCr::PageContent::Link class Mechanize::PageContent::Link
getter node : Lexbor::Node getter node : Lexbor::Node
getter page : Page getter page : Mechanize::Page
getter mech : Mechanize getter mech : Mechanize
getter href : String getter href : String
getter text : String getter text : String

View File

@ -1,4 +1,5 @@
module MechanizeCr::ElementMatcher class Mechanize
module ElementMatcher
macro elements_with(singular, plural = "") macro elements_with(singular, plural = "")
{% plural = "#{singular.id}s" if plural.empty? %} {% plural = "#{singular.id}s" if plural.empty? %}
# search {{ singular.id }} which matches condition. # search {{ singular.id }} which matches condition.
@ -46,8 +47,9 @@ module MechanizeCr::ElementMatcher
def {{singular.id}}_with(criteria) def {{singular.id}}_with(criteria)
f = {{plural.id}}_with(criteria) f = {{plural.id}}_with(criteria)
# TODO: Write correct error message. # TODO: Write correct error message.
raise ElementNotFoundError.new(:{{singular.id}}, "") if f.empty? raise Mechanize::ElementNotFoundError.new(:{{singular.id}}, "") if f.empty?
f.first f.first
end end
end end
end end
end