add history:

master
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"} agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
params = {"hoge" => "hoge"} params = {"hoge" => "hoge"}
page = agent.get("http://example.com/", params: params) page = agent.get("http://example.com/", params: params)
form = page.forms[0] #form = page.forms[0]
#query = {"foo" => "foo_value", "bar" => "bar_value"} #query = {"foo" => "foo_value", "bar" => "bar_value"}
#page = agent.post("http://example.com/", query: query) #page = agent.post("http://example.com/", query: query)
#puts page.code #puts page.code

View File

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

View File

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