From ae0650d2bea1a68fc186ca7df731c1b946798711 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 22 Aug 2021 09:47:10 +0900 Subject: [PATCH 1/4] file download --- spec/agent_spec.cr | 7 +++++++ src/mechanize.cr | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index e6b9dbe..a729169 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -56,4 +56,11 @@ describe "Mechanize Agent test" do agent2.history.size.should eq 1 agent2.history.pop.uri.to_s.should eq "http://example.com/form" end + + it "can download page" do + agent = Mechanize.new + agent.download("http://example.com", "mechanizecr_example.html") + File.exists?("mechanizecr_example.html").should eq true + File.delete("mechanizecr_example.html") + end end diff --git a/src/mechanize.cr b/src/mechanize.cr index 5cd6dbc..83413bb 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -17,7 +17,9 @@ class Mechanize @agent.user_agent = AGENT["Mechanize"] end - 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 page = @agent.fetch uri, method, headers, params add_to_history(page) @@ -25,7 +27,9 @@ class Mechanize page end - def post(uri : String | URI, headers = HTTP::Headers.new, query : Hash(String, String | Array(String)) = Hash(String, String).new) + def post(uri : String | URI, + headers = HTTP::Headers.new, + query : Hash(String, String | Array(String)) = Hash(String, String).new) node = Node.new node["method"] = "POST" node["enctype"] = "application/x-www-form-urlencoded" @@ -119,4 +123,19 @@ class Mechanize href = link.href get href end + + # download page body from given uri. + # TODO: except this request from history. + def download(uri, + filename, + headers = HTTP::Headers.new, + params : Hash(String, String | Array(String)) = Hash(String, String).new) + page = get(uri, headers, params) + + case page + when MechanizeCr::File + File.write(filename, page.body) + end + page + end end From b98bb4eb2dd943986a80eab2da608495c419a376 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 22 Aug 2021 09:47:57 +0900 Subject: [PATCH 2/4] format code --- src/mechanize.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mechanize.cr b/src/mechanize.cr index 83413bb..5533293 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -126,7 +126,7 @@ class Mechanize # download page body from given uri. # TODO: except this request from history. - def download(uri, + def download(uri, filename, headers = HTTP::Headers.new, params : Hash(String, String | Array(String)) = Hash(String, String).new) From d344fadc44919b469175077a3f9fdccd56edc120 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 24 Aug 2021 07:50:12 +0900 Subject: [PATCH 3/4] add transact method --- spec/agent_spec.cr | 10 ++++++++++ src/mechanize.cr | 12 ++++++++++++ src/mechanize/history.cr | 6 +++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index a729169..ed7e8f8 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -63,4 +63,14 @@ describe "Mechanize Agent test" do File.exists?("mechanizecr_example.html").should eq true File.delete("mechanizecr_example.html") end + + it "doesn't add page to history within transact block" do + agent = Mechanize.new + agent.get("http://example.com/") + agent.history.size.should eq 1 + agent.transact do + agent.get("http://example.com/") + end + agent.history.size.should eq 1 + end end diff --git a/src/mechanize.cr b/src/mechanize.cr index 5533293..3bd9b7a 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -138,4 +138,16 @@ class Mechanize end page end + + # Runs given block, then resets the page history as it was before. + def transact + # save the previous history status. + history_backup = MechanizeCr::History.new(@agent.history.max_size, @agent.history.array.dup) + begin + yield self + ensure + # restore the previous history. + @agent.history = history_backup + end + end end diff --git a/src/mechanize/history.cr b/src/mechanize/history.cr index 183a90b..6e1600b 100644 --- a/src/mechanize/history.cr +++ b/src/mechanize/history.cr @@ -4,11 +4,11 @@ class MechanizeCr::History property max_size : Int32 property array : Array(MechanizeCr::Page) - forward_missing_to @array + delegate :size, :empty?, :last, to: array - def initialize(max_size = 100) + def initialize(max_size = 100, array = Array(MechanizeCr::Page).new) @max_size = max_size - @array = Array(MechanizeCr::Page).new + @array = array end def push(page, uri = nil) From cfa824c9091d4e33d3c7b3578a68849837105195 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 24 Aug 2021 08:09:43 +0900 Subject: [PATCH 4/4] download file within transact --- spec/agent_spec.cr | 16 +++++++++------- src/mechanize.cr | 12 ++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index ed7e8f8..86b7300 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -57,13 +57,6 @@ describe "Mechanize Agent test" do agent2.history.pop.uri.to_s.should eq "http://example.com/form" end - it "can download page" do - agent = Mechanize.new - agent.download("http://example.com", "mechanizecr_example.html") - File.exists?("mechanizecr_example.html").should eq true - File.delete("mechanizecr_example.html") - end - it "doesn't add page to history within transact block" do agent = Mechanize.new agent.get("http://example.com/") @@ -73,4 +66,13 @@ describe "Mechanize Agent test" do end agent.history.size.should eq 1 end + + it "can download page" do + agent = Mechanize.new + agent.history.size.should eq 0 + agent.download("http://example.com", "mechanizecr_example.html") + agent.history.size.should eq 0 + File.exists?("mechanizecr_example.html").should eq true + File.delete("mechanizecr_example.html") + end end diff --git a/src/mechanize.cr b/src/mechanize.cr index 3bd9b7a..6fcf332 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -130,13 +130,13 @@ class Mechanize filename, headers = HTTP::Headers.new, params : Hash(String, String | Array(String)) = Hash(String, String).new) - page = get(uri, headers, params) - - case page - when MechanizeCr::File - File.write(filename, page.body) + transact do + page = get(uri, headers, params) + case page + when MechanizeCr::File + File.write(filename, page.body) + end end - page end # Runs given block, then resets the page history as it was before.