Merge branch 'master' of github.com:Kanezoh/mechanize.cr
This commit is contained in:
commit
248c3575dd
@ -56,4 +56,23 @@ describe "Mechanize Agent test" do
|
|||||||
agent2.history.size.should eq 1
|
agent2.history.size.should eq 1
|
||||||
agent2.history.pop.uri.to_s.should eq "http://example.com/form"
|
agent2.history.pop.uri.to_s.should eq "http://example.com/form"
|
||||||
end
|
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
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
@ -17,7 +17,9 @@ class Mechanize
|
|||||||
@agent.user_agent = AGENT["Mechanize"]
|
@agent.user_agent = AGENT["Mechanize"]
|
||||||
end
|
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
|
method = :get
|
||||||
page = @agent.fetch uri, method, headers, params
|
page = @agent.fetch uri, method, headers, params
|
||||||
add_to_history(page)
|
add_to_history(page)
|
||||||
@ -25,7 +27,9 @@ class Mechanize
|
|||||||
page
|
page
|
||||||
end
|
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 = Node.new
|
||||||
node["method"] = "POST"
|
node["method"] = "POST"
|
||||||
node["enctype"] = "application/x-www-form-urlencoded"
|
node["enctype"] = "application/x-www-form-urlencoded"
|
||||||
@ -119,4 +123,31 @@ class Mechanize
|
|||||||
href = link.href
|
href = link.href
|
||||||
get href
|
get href
|
||||||
end
|
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)
|
||||||
|
transact do
|
||||||
|
page = get(uri, headers, params)
|
||||||
|
case page
|
||||||
|
when MechanizeCr::File
|
||||||
|
File.write(filename, page.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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
|
end
|
||||||
|
@ -4,11 +4,11 @@ class MechanizeCr::History
|
|||||||
property max_size : Int32
|
property max_size : Int32
|
||||||
property array : Array(MechanizeCr::Page)
|
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
|
@max_size = max_size
|
||||||
@array = Array(MechanizeCr::Page).new
|
@array = array
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(page, uri = nil)
|
def push(page, uri = nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user