add comment to other classes

This commit is contained in:
Kanezoh 2021-11-18 11:45:30 +09:00
parent 794a2085de
commit 2c26835252
5 changed files with 32 additions and 7 deletions

View File

@ -1,5 +1,6 @@
# TODO: want to add methods with safe way like Ruby's refinement. # TODO: want to property's without open HTTP::Cookie class.
# This class represents http cookie.
# open HTTP::Cookie class to add origin property. # open HTTP::Cookie class to add origin property.
# origin property represents the origin of the resource. # origin property represents the origin of the resource.
# if cookie's domain attribute isn't designated, # if cookie's domain attribute isn't designated,

View File

@ -1,5 +1,6 @@
require "./base_error" require "./base_error"
# This error means matched elements are not found by *_with method.
class Mechanize::ElementNotFoundError < Mechanize::Error class Mechanize::ElementNotFoundError < Mechanize::Error
getter element : Symbol getter element : Symbol
getter conditions : String getter conditions : String

View File

@ -6,12 +6,13 @@ require "../history"
class Mechanize class Mechanize
module HTTP module HTTP
class Agent class Agent
property :request_headers, :context property request_headers : ::HTTP::Headers
property context : Mechanize?
property history : Mechanize::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)
@history = Mechanize::History.new @history = Mechanize::History.new
@request_headers = ::HTTP::Headers.new @request_headers = ::HTTP::Headers.new
@context = context @context = context
@ -19,6 +20,9 @@ class Mechanize
@user_agent = "" @user_agent = ""
end end
# send http request and return page.
# This method is called from Mechanize#get, #post and other methods.
# There's no need to call this method directly.
def fetch(uri, method = :get, headers = ::HTTP::Headers.new, params = Hash(String, String).new, def fetch(uri, method = :get, headers = ::HTTP::Headers.new, params = Hash(String, String).new,
referer = (current_page unless history.empty?)) referer = (current_page unless history.empty?))
uri = resolve_url(uri, referer) uri = resolve_url(uri, referer)
@ -51,7 +55,8 @@ class Mechanize
fetch(uri) fetch(uri)
end end
def http_request(uri, method, params) # send http request
private def http_request(uri, method, params) : ::HTTP::Client::Response?
case uri.scheme.not_nil!.downcase case uri.scheme.not_nil!.downcase
when "http", "https" when "http", "https"
case method case method
@ -63,20 +68,34 @@ class Mechanize
end end
end end
def current_page # returns the page now mechanize visiting.
# ```
# agent.current_page
# ```
def current_page : Mechanize::Page
@history.last @history.last
end end
def back # returns the page mechanize previous visited.
# ```
# agent.back
# ```
def back : Mechanize::Page
@history.pop @history.pop
end end
# Get maximum number of items allowed in the history. The default setting is 100 pages. # Get maximum number of items allowed in the history. The default setting is 100 pages.
def max_history # ```
# agent.max_history # => 100
# ```
def max_history : Int32
@history.max_size @history.max_size
end end
# Set maximum number of items allowed in the history. # Set maximum number of items allowed in the history.
# ```
# agent.max_history = 1000
# ```
def max_history=(length) def max_history=(length)
@history.max_size = length @history.max_size = length
end end

View File

@ -1,3 +1,5 @@
# This class represents link element like <a> and <area>.
# The instance of this class is clickable.
class Mechanize::PageContent::Link class Mechanize::PageContent::Link
getter node : Lexbor::Node getter node : Lexbor::Node
getter page : Mechanize::Page getter page : Mechanize::Page

View File

@ -1,4 +1,6 @@
class Mechanize class Mechanize
# This module is for macros making *_with methods.
# These methods are useful for searching elements by its' attribute.
module ElementMatcher 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? %}