format code
This commit is contained in:
parent
858a999ed2
commit
9478bffdf1
14
main.cr
14
main.cr
@ -4,10 +4,10 @@ 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
|
||||||
#puts page.body
|
# puts page.body
|
||||||
#puts page.css("h1").first.inner_text
|
# puts page.css("h1").first.inner_text
|
||||||
#puts page.title
|
# puts page.title
|
||||||
|
@ -6,12 +6,11 @@ WebMock.stub(:get, "example.com/cookies2").to_return(headers: {"Set-Cookie" => "
|
|||||||
WebMock.stub(:get, "example.com/cookies3").to_return(headers: {"Set-Cookie" => "id=456"})
|
WebMock.stub(:get, "example.com/cookies3").to_return(headers: {"Set-Cookie" => "id=456"})
|
||||||
WebMock.stub(:get, "example.com/secure_cookies").to_return(headers: {"Set-Cookie" => "id=123; Secure"})
|
WebMock.stub(:get, "example.com/secure_cookies").to_return(headers: {"Set-Cookie" => "id=123; Secure"})
|
||||||
WebMock.stub(:get, "example.com/paths").to_return(headers: {"Set-Cookie" => "id=123; Path=/paths"})
|
WebMock.stub(:get, "example.com/paths").to_return(headers: {"Set-Cookie" => "id=123; Path=/paths"})
|
||||||
WebMock.stub(:get, "example.com/paths/hoge").to_return()
|
WebMock.stub(:get, "example.com/paths/hoge").to_return
|
||||||
WebMock.stub(:get, "https://example.com/").to_return()
|
WebMock.stub(:get, "https://example.com/").to_return
|
||||||
WebMock.stub(:get, "example.com/hoge/paths").to_return()
|
WebMock.stub(:get, "example.com/hoge/paths").to_return
|
||||||
WebMock.stub(:get, "www.example.com").to_return()
|
WebMock.stub(:get, "www.example.com").to_return
|
||||||
WebMock.stub(:get, "example.com/meta_cookie").to_return(body:
|
WebMock.stub(:get, "example.com/meta_cookie").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/button").to_return(body:
|
WebMock.stub(:get, "example.com/form/button").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/check_box").to_return(body:
|
WebMock.stub(:get, "example.com/form/check_box").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/fields").to_return(body:
|
WebMock.stub(:get, "example.com/form/fields").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/multi_select_list").to_return(body:
|
WebMock.stub(:get, "example.com/form/multi_select_list").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/multi_select_list").to_return(body:
|
WebMock.stub(:get, "example.com/form/multi_select_list").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper.cr"
|
require "../spec_helper.cr"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/radio_button").to_return(body:
|
WebMock.stub(:get, "example.com/form/radio_button").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
@ -26,7 +25,7 @@ describe "Form Fields RadioButton" do
|
|||||||
radiobuttons.size.should eq 3
|
radiobuttons.size.should eq 3
|
||||||
|
|
||||||
it "returns radiobutton check status" do
|
it "returns radiobutton check status" do
|
||||||
radiobuttons.map(&.checked?).should eq [false,false,false]
|
radiobuttons.map(&.checked?).should eq [false, false, false]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can change check status" do
|
it "can change check status" do
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/select_list").to_return(body:
|
WebMock.stub(:get, "example.com/form/select_list").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form/textarea").to_return(body:
|
WebMock.stub(:get, "example.com/form/textarea").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/check_form").to_return(body:
|
WebMock.stub(:get, "example.com/check_form").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
WebMock.stub(:get, "http://example.com/?foo=bar&foo1=bar2")
|
WebMock.stub(:get, "http://example.com/?foo=bar&foo1=bar2")
|
||||||
WebMock.stub(:post, "http://example.com/post").
|
WebMock.stub(:post, "http://example.com/post")
|
||||||
with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
|
.with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
|
||||||
to_return(body: "success")
|
.to_return(body: "success")
|
||||||
WebMock.stub(:get, "example.com/%E3%81%82%E3%81%82%E3%81%82")
|
WebMock.stub(:get, "example.com/%E3%81%82%E3%81%82%E3%81%82")
|
||||||
|
|
||||||
describe "Mechanize HTTP test" do
|
describe "Mechanize HTTP test" do
|
||||||
@ -49,7 +49,7 @@ describe "Mechanize HTTP test" do
|
|||||||
|
|
||||||
it "simple POST" do
|
it "simple POST" do
|
||||||
agent = Mechanize.new
|
agent = Mechanize.new
|
||||||
query = { "email" => "foobar" }
|
query = {"email" => "foobar"}
|
||||||
page = agent.post("http://example.com/post", query: query)
|
page = agent.post("http://example.com/post", query: query)
|
||||||
page.body.should eq "success"
|
page.body.should eq "success"
|
||||||
page.code.should eq 200
|
page.code.should eq 200
|
||||||
|
@ -35,14 +35,14 @@ describe "Mechanize Page test" do
|
|||||||
it "can detect form by using form_with method, argument type: Hash" do
|
it "can detect form by using form_with method, argument type: Hash" do
|
||||||
agent = Mechanize.new
|
agent = Mechanize.new
|
||||||
page = agent.get("http://example.com/form")
|
page = agent.get("http://example.com/form")
|
||||||
form = page.form_with({"name" => "sample_form" })
|
form = page.form_with({"name" => "sample_form"})
|
||||||
form.name.should eq "sample_form"
|
form.name.should eq "sample_form"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can detect form by using form_with method, argument type: NamedTuple" do
|
it "can detect form by using form_with method, argument type: NamedTuple" do
|
||||||
agent = Mechanize.new
|
agent = Mechanize.new
|
||||||
page = agent.get("http://example.com/form")
|
page = agent.get("http://example.com/form")
|
||||||
form = page.form_with({name: "sample_form" })
|
form = page.form_with({name: "sample_form"})
|
||||||
form.name.should eq "sample_form"
|
form.name.should eq "sample_form"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,14 +2,12 @@ require "spec"
|
|||||||
require "webmock"
|
require "webmock"
|
||||||
require "../src/mechanize"
|
require "../src/mechanize"
|
||||||
|
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com")
|
WebMock.stub(:get, "example.com")
|
||||||
WebMock.stub(:get, "fail_example.com").to_return(status: 500)
|
WebMock.stub(:get, "fail_example.com").to_return(status: 500)
|
||||||
WebMock.stub(:get, "body_example.com").to_return(body: "hello")
|
WebMock.stub(:get, "body_example.com").to_return(body: "hello")
|
||||||
WebMock.stub(:get, "another_domain.com/")
|
WebMock.stub(:get, "another_domain.com/")
|
||||||
|
|
||||||
WebMock.stub(:get, "example.com/form").to_return(body:
|
WebMock.stub(:get, "example.com/form").to_return(body: <<-BODY
|
||||||
<<-BODY
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>page_title</title>
|
<title>page_title</title>
|
||||||
@ -24,10 +22,10 @@ WebMock.stub(:get, "example.com/form").to_return(body:
|
|||||||
</html>
|
</html>
|
||||||
BODY
|
BODY
|
||||||
)
|
)
|
||||||
WebMock.stub(:post, "example.com/post_path").
|
WebMock.stub(:post, "example.com/post_path")
|
||||||
with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
|
.with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
|
||||||
to_return(body: "success")
|
.to_return(body: "success")
|
||||||
|
|
||||||
WebMock.stub(:post, "example.com/post_path").
|
WebMock.stub(:post, "example.com/post_path")
|
||||||
with(body: "name=foo&email=bar&commit=submit", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
|
.with(body: "name=foo&email=bar&commit=submit", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
|
||||||
to_return(body: "success with button")
|
.to_return(body: "success with button")
|
||||||
|
@ -10,27 +10,28 @@ class Mechanize
|
|||||||
AGENT = {
|
AGENT = {
|
||||||
"Mechanize" => "Mechanize/#{VERSION} Crystal/#{Crystal::VERSION} (https://github.com/Kanezoh/mechanize.cr)",
|
"Mechanize" => "Mechanize/#{VERSION} Crystal/#{Crystal::VERSION} (https://github.com/Kanezoh/mechanize.cr)",
|
||||||
}
|
}
|
||||||
def initialize()
|
|
||||||
|
def initialize
|
||||||
@agent = MechanizeCr::HTTP::Agent.new
|
@agent = MechanizeCr::HTTP::Agent.new
|
||||||
@agent.context = self
|
@agent.context = self
|
||||||
@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)
|
||||||
#yield page if block_given?
|
# yield page if block_given?
|
||||||
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"
|
||||||
|
|
||||||
form = MechanizeCr::Form.new(node)
|
form = MechanizeCr::Form.new(node)
|
||||||
query.each do |k,v|
|
query.each do |k, v|
|
||||||
node = Node.new
|
node = Node.new
|
||||||
node["name"] = k
|
node["name"] = k
|
||||||
form.fields << MechanizeCr::FormContent::Field.new(node, v)
|
form.fields << MechanizeCr::FormContent::Field.new(node, v)
|
||||||
@ -50,7 +51,7 @@ class Mechanize
|
|||||||
headers.merge!(content_headers)
|
headers.merge!(content_headers)
|
||||||
|
|
||||||
# fetch the page
|
# fetch the page
|
||||||
page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data }, referer: cur_page)
|
page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data}, referer: 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)
|
||||||
@ -81,7 +82,7 @@ class Mechanize
|
|||||||
@agent.history.pop
|
@agent.history.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
def submit(form, button=nil)
|
def submit(form, button = nil)
|
||||||
form.add_button_to_query(button) if button
|
form.add_button_to_query(button) if button
|
||||||
case form.method.upcase
|
case form.method.upcase
|
||||||
when "POST"
|
when "POST"
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
# this property is used to send cookies to same origin resource.
|
# this property is used to send cookies to same origin resource.
|
||||||
class ::HTTP::Cookie
|
class ::HTTP::Cookie
|
||||||
property origin : String?
|
property origin : String?
|
||||||
|
|
||||||
def initialize(name : String, value : String, @path : String? = nil,
|
def initialize(name : String, value : String, @path : String? = nil,
|
||||||
@expires : Time? = nil, @domain : String? = nil,
|
@expires : Time? = nil, @domain : String? = nil,
|
||||||
@secure : Bool = false, @http_only : Bool = false,
|
@secure : Bool = false, @http_only : Bool = false,
|
||||||
@ -30,7 +31,7 @@ class ::HTTP::Cookie
|
|||||||
end
|
end
|
||||||
|
|
||||||
if domain
|
if domain
|
||||||
host.try &.=~(/.*#{domain.try &.gsub(".", "\.")}$/)
|
host.try &.=~(/.*#{domain.try &.gsub(".", ".")}$/)
|
||||||
else
|
else
|
||||||
origin == host
|
origin == host
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
require "./base_error"
|
require "./base_error"
|
||||||
|
|
||||||
class MechanizeCr::ElementNotFoundError < MechanizeCr::Error
|
class MechanizeCr::ElementNotFoundError < MechanizeCr::Error
|
||||||
getter element : Symbol
|
getter element : Symbol
|
||||||
getter conditions : String
|
getter conditions : String
|
||||||
@ -9,5 +10,4 @@ class MechanizeCr::ElementNotFoundError < MechanizeCr::Error
|
|||||||
|
|
||||||
super "Element #{element} with conditions #{conditions} was not found"
|
super "Element #{element} with conditions #{conditions} was not found"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
require "http/client"
|
require "http/client"
|
||||||
|
|
||||||
class MechanizeCr::File
|
class MechanizeCr::File
|
||||||
#property :body, :filename
|
# property :body, :filename
|
||||||
property :body, :code, uri, :response
|
property :body, :code, uri, :response
|
||||||
def initialize(uri : URI, response : ::HTTP::Client::Response, body : String , code : Int32)
|
|
||||||
|
def initialize(uri : URI, response : ::HTTP::Client::Response, body : String, code : Int32)
|
||||||
@uri = uri
|
@uri = uri
|
||||||
@body = body
|
@body = body
|
||||||
@code = code
|
@code = code
|
||||||
|
@ -36,10 +36,10 @@ class MechanizeCr::Form
|
|||||||
@name = node.fetch("name", "")
|
@name = node.fetch("name", "")
|
||||||
@clicked_buttons = Array(FormContent::Button).new
|
@clicked_buttons = Array(FormContent::Button).new
|
||||||
@page = page
|
@page = page
|
||||||
#@mech = mech
|
# @mech = mech
|
||||||
|
|
||||||
#@encoding = node['accept-charset'] || (page && page.encoding) || nil
|
# @encoding = node['accept-charset'] || (page && page.encoding) || nil
|
||||||
#@ignore_encoding_error = false
|
# @ignore_encoding_error = false
|
||||||
parse
|
parse
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ class MechanizeCr::Form
|
|||||||
buttons << FormContent::Button.new(html_node, @node)
|
buttons << FormContent::Button.new(html_node, @node)
|
||||||
when "submit"
|
when "submit"
|
||||||
buttons << FormContent::SubmitButton.new(html_node, @node)
|
buttons << FormContent::SubmitButton.new(html_node, @node)
|
||||||
when"reset"
|
when "reset"
|
||||||
buttons << FormContent::ResetButton.new(html_node, @node)
|
buttons << FormContent::ResetButton.new(html_node, @node)
|
||||||
when "image"
|
when "image"
|
||||||
buttons << FormContent::ImageButton.new(html_node, @node)
|
buttons << FormContent::ImageButton.new(html_node, @node)
|
||||||
@ -118,7 +118,7 @@ class MechanizeCr::Form
|
|||||||
|
|
||||||
private def build_query_string(params : Array(Array(String)))
|
private def build_query_string(params : Array(Array(String)))
|
||||||
params.reduce("") do |acc, arr|
|
params.reduce("") do |acc, arr|
|
||||||
hash = { arr[0] => arr[1] }
|
hash = {arr[0] => arr[1]}
|
||||||
acc + URI::Params.encode(hash) + '&'
|
acc + URI::Params.encode(hash) + '&'
|
||||||
end.rchop
|
end.rchop
|
||||||
end
|
end
|
||||||
@ -144,9 +144,9 @@ class MechanizeCr::Form
|
|||||||
radio_groups.each_value do |g|
|
radio_groups.each_value do |g|
|
||||||
checked = g.select(&.checked)
|
checked = g.select(&.checked)
|
||||||
if checked.uniq.size > 1
|
if checked.uniq.size > 1
|
||||||
#values = checked.map(&.value).join(', ').inspect
|
# values = checked.map(&.value).join(', ').inspect
|
||||||
#name = checked.first.name.inspect
|
# name = checked.first.name.inspect
|
||||||
#raise Mechanize::Error,
|
# raise Mechanize::Error,
|
||||||
# "radiobuttons #{values} are checked in the #{name} group, " \
|
# "radiobuttons #{values} are checked in the #{name} group, " \
|
||||||
# "only one is allowed"
|
# "only one is allowed"
|
||||||
raise MechanizeCr::Error.new
|
raise MechanizeCr::Error.new
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
class MechanizeCr::FormContent::Button < MechanizeCr::FormContent::Field
|
class MechanizeCr::FormContent::Button < MechanizeCr::FormContent::Field
|
||||||
getter form_node : Node | Lexbor::Node
|
getter form_node : Node | Lexbor::Node
|
||||||
def initialize(node : Node | Lexbor::Node, form_node : Node | Lexbor::Node, value=nil)
|
|
||||||
|
def initialize(node : Node | Lexbor::Node, form_node : Node | Lexbor::Node, value = nil)
|
||||||
@form_node = form_node
|
@form_node = form_node
|
||||||
super(node, value)
|
super(node, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require "./reset_button"
|
require "./reset_button"
|
||||||
require "./submit_button"
|
require "./submit_button"
|
||||||
require "./image_button"
|
require "./image_button"
|
||||||
|
@ -2,6 +2,7 @@ class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::RadioButton
|
|||||||
def check
|
def check
|
||||||
@checked = true
|
@checked = true
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_value
|
def query_value
|
||||||
[@name, @value || "on"]
|
[@name, @value || "on"]
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ class MechanizeCr::FormContent::Field
|
|||||||
getter raw_value : String?
|
getter raw_value : String?
|
||||||
getter node : Node | Lexbor::Node
|
getter node : Node | Lexbor::Node
|
||||||
|
|
||||||
def initialize(node : Node | Lexbor::Node, value=nil)
|
def initialize(node : Node | Lexbor::Node, value = nil)
|
||||||
@node = node
|
@node = node
|
||||||
@name = node.fetch("name", "")
|
@name = node.fetch("name", "")
|
||||||
@value = value || node.fetch("value", nil)
|
@value = value || node.fetch("value", nil)
|
||||||
@ -30,7 +30,7 @@ class MechanizeCr::FormContent::Field
|
|||||||
def inspect # :nodoc:
|
def inspect # :nodoc:
|
||||||
"[%s:0x%x type: %s name: %s value: %s]" % [
|
"[%s:0x%x type: %s name: %s value: %s]" % [
|
||||||
self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase,
|
self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase,
|
||||||
object_id, type, name, value
|
object_id, type, name, value,
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -55,7 +55,7 @@ class MechanizeCr::FormContent::MultiSelectList
|
|||||||
def inspect # :nodoc:
|
def inspect # :nodoc:
|
||||||
"[%s:0x%x type: %s name: %s values: [%s]]" % [
|
"[%s:0x%x type: %s name: %s values: [%s]]" % [
|
||||||
self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase,
|
self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase,
|
||||||
object_id, type, name, values.join(',')
|
object_id, type, name, values.join(','),
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -24,33 +24,32 @@ class MechanizeCr::FormContent::RadioButton < MechanizeCr::FormContent::Field
|
|||||||
checked
|
checked
|
||||||
end
|
end
|
||||||
|
|
||||||
#def hash # :nodoc:
|
# def hash # :nodoc:
|
||||||
# @form.hash ^ @name.hash ^ @value.hash
|
# @form.hash ^ @name.hash ^ @value.hash
|
||||||
#end
|
# end
|
||||||
#
|
#
|
||||||
#def label
|
# def label
|
||||||
# (id = self['id']) && @form.page.labels_hash[id] || nil
|
# (id = self['id']) && @form.page.labels_hash[id] || nil
|
||||||
#end
|
# end
|
||||||
#
|
#
|
||||||
#def text
|
# def text
|
||||||
# label.text rescue nil
|
# label.text rescue nil
|
||||||
#end
|
# end
|
||||||
#
|
#
|
||||||
#def [](key)
|
# def [](key)
|
||||||
# @node[key]
|
# @node[key]
|
||||||
#end
|
# end
|
||||||
|
|
||||||
# alias checked? checked
|
# alias checked? checked
|
||||||
|
|
||||||
#def == other # :nodoc:
|
# def == other # :nodoc:
|
||||||
# self.class === other and
|
# self.class === other and
|
||||||
# other.form == @form and
|
# other.form == @form and
|
||||||
# other.name == @name and
|
# other.name == @name and
|
||||||
# other.value == @value
|
# other.value == @value
|
||||||
#end
|
# end
|
||||||
#
|
#
|
||||||
#alias eql? == # :nodoc:
|
# alias eql? == # :nodoc:
|
||||||
|
|
||||||
|
|
||||||
private def uncheck_peers
|
private def uncheck_peers
|
||||||
form.radiobuttons_with(name).try &.each do |b|
|
form.radiobuttons_with(name).try &.each do |b|
|
||||||
|
@ -2,6 +2,7 @@ require "./page"
|
|||||||
|
|
||||||
class MechanizeCr::History < Array(MechanizeCr::Page)
|
class MechanizeCr::History < Array(MechanizeCr::Page)
|
||||||
property max_size : Int32
|
property max_size : Int32
|
||||||
|
|
||||||
def initialize(max_size = 100)
|
def initialize(max_size = 100)
|
||||||
@max_size = max_size
|
@max_size = max_size
|
||||||
super
|
super
|
||||||
|
@ -19,7 +19,7 @@ module MechanizeCr
|
|||||||
@user_agent = ""
|
@user_agent = ""
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new,
|
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String, String).new,
|
||||||
referer = (current_page unless history.empty?))
|
referer = (current_page unless history.empty?))
|
||||||
uri = resolve_url(uri, referer)
|
uri = resolve_url(uri, referer)
|
||||||
set_request_headers(uri, headers)
|
set_request_headers(uri, headers)
|
||||||
@ -52,7 +52,7 @@ module MechanizeCr
|
|||||||
|
|
||||||
def http_request(uri, method, params)
|
def http_request(uri, method, params)
|
||||||
case uri.scheme.not_nil!.downcase
|
case uri.scheme.not_nil!.downcase
|
||||||
when "http", "https" then
|
when "http", "https"
|
||||||
case method
|
case method
|
||||||
when :get
|
when :get
|
||||||
::HTTP::Client.get(uri, headers: request_headers)
|
::HTTP::Client.get(uri, headers: request_headers)
|
||||||
@ -82,7 +82,7 @@ module MechanizeCr
|
|||||||
|
|
||||||
private def set_request_headers(uri, headers)
|
private def set_request_headers(uri, headers)
|
||||||
reset_request_header_cookies
|
reset_request_header_cookies
|
||||||
headers.each do |k,v|
|
headers.each do |k, v|
|
||||||
request_headers[k] = v
|
request_headers[k] = v
|
||||||
end
|
end
|
||||||
valid_cookies(uri).add_request_headers(request_headers)
|
valid_cookies(uri).add_request_headers(request_headers)
|
||||||
@ -118,7 +118,7 @@ module MechanizeCr
|
|||||||
if page.body =~ /Set-Cookie/
|
if page.body =~ /Set-Cookie/
|
||||||
page.css("head meta[http-equiv=\"Set-Cookie\"]").each do |meta|
|
page.css("head meta[http-equiv=\"Set-Cookie\"]").each do |meta|
|
||||||
cookie = meta["content"].split(";")[0]
|
cookie = meta["content"].split(";")[0]
|
||||||
key,value = cookie.split("=")
|
key, value = cookie.split("=")
|
||||||
cookie = ::HTTP::Cookie.new(name: key, value: value)
|
cookie = ::HTTP::Cookie.new(name: key, value: value)
|
||||||
save_cookies(uri, [cookie])
|
save_cookies(uri, [cookie])
|
||||||
end
|
end
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
require "lexbor"
|
require "lexbor"
|
||||||
|
|
||||||
# This is a fake node used when sending post request.
|
# This is a fake node used when sending post request.
|
||||||
class Node < Hash(String,String)
|
class Node < Hash(String, String)
|
||||||
def css(str)
|
def css(str)
|
||||||
[] of Hash(String,String)
|
[] of Hash(String, String)
|
||||||
end
|
end
|
||||||
|
|
||||||
def inner_text
|
def inner_text
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
module MechanzeCr::ElementMatcher
|
module MechanzeCr::ElementMatcher
|
||||||
macro elements_with(singular, plural="")
|
macro elements_with(singular, plural = "")
|
||||||
{% plural = "#{singular.id}s" if plural.empty? %}
|
{% plural = "#{singular.id}s" if plural.empty? %}
|
||||||
def {{plural.id}}_with(criteria)
|
def {{plural.id}}_with(criteria)
|
||||||
{{plural.id}}_with(criteria){}
|
{{plural.id}}_with(criteria){}
|
||||||
|
Loading…
Reference in New Issue
Block a user