add history:

This commit is contained in:
Kanezoh 2021-05-30 17:09:27 +09:00
parent 12bcaea03f
commit fbd99a854f
3 changed files with 14 additions and 6 deletions

View File

@ -4,7 +4,7 @@ agent = Mechanize.new
agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
params = {"hoge" => "hoge"}
page = agent.get("http://example.com/", params: params)
form = page.forms[0]
#form = page.forms[0]
#query = {"foo" => "foo_value", "bar" => "bar_value"}
#page = agent.post("http://example.com/", query: query)
#puts page.code

View File

@ -6,7 +6,6 @@ require "./mechanize/errors/*"
class Mechanize
VERSION = "0.1.0"
def initialize()
@agent = MechanizeCr::HTTP::Agent.new
@agent.context = self
@ -15,7 +14,7 @@ class Mechanize
def get(uri : String | URI, headers = HTTP::Headers.new, params : Hash(String, String | Array(String)) = Hash(String,String).new)
method = :get
page = @agent.fetch uri, method, headers, params
#add_to_history(page)
add_to_history(page)
#yield page if block_given?
page
end
@ -49,7 +48,7 @@ class Mechanize
page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data })#, cur_page
headers.delete("Content-Type")
headers.delete("Content-Length")
#add_to_history(page)
add_to_history(page)
page
end
@ -65,4 +64,12 @@ class Mechanize
code = response.not_nil!.status_code
MechanizeCr::Page.new(uri, response, body, code)
end
def history
@agent.history
end
def add_to_history(page)
history.push(page)
end
end

View File

@ -5,8 +5,9 @@ module MechanizeCr
module HTTP
class Agent
property :request_headers, :context
def initialize(@context : Mechanize | Nil = nil)
property history : Array(MechanizeCr::Page)
def initialize(@context : Mechanize | Nil = nil, history = Array(MechanizeCr::Page).new)
@history = history
@request_headers = ::HTTP::Headers.new
@context = context
end