From fbd99a854f0b9e77de9461dc406d66c31523f364 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 30 May 2021 17:09:27 +0900 Subject: [PATCH] add history: --- main.cr | 2 +- src/mechanize.cr | 13 ++++++++++--- src/mechanize/http/agent.cr | 5 +++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/main.cr b/main.cr index f675518..f854339 100644 --- a/main.cr +++ b/main.cr @@ -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 diff --git a/src/mechanize.cr b/src/mechanize.cr index 4b75d3b..e52d4bb 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -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 diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index ca90fc6..738d9f4 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -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