get parameters
This commit is contained in:
parent
890d4e5449
commit
1dd6e72025
6
main.cr
6
main.cr
@ -3,9 +3,9 @@ require "./src/mechanize.cr"
|
|||||||
agent = Mechanize.new
|
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://google.com/", params: params)
|
||||||
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
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
require "./mechanize/http/agent"
|
require "./mechanize/http/agent"
|
||||||
require "./mechanize/form"
|
require "./mechanize/form"
|
||||||
require "./mechanize/node"
|
require "./mechanize/node"
|
||||||
|
require "./mechanize/page"
|
||||||
|
|
||||||
class Mechanize
|
class Mechanize
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
@ -44,9 +45,9 @@ class Mechanize
|
|||||||
headers.merge!(content_headers)
|
headers.merge!(content_headers)
|
||||||
|
|
||||||
# fetch the page
|
# fetch the page
|
||||||
#page = @agent.fetch uri, :post, headers, [request_data]#, cur_page
|
page = @agent.fetch uri, :post, headers, [request_data]#, cur_page
|
||||||
#add_to_history(page)
|
#add_to_history(page)
|
||||||
#page
|
page
|
||||||
end
|
end
|
||||||
|
|
||||||
def request_headers
|
def request_headers
|
||||||
|
@ -14,8 +14,8 @@ module MechanizeCr
|
|||||||
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)
|
||||||
uri = URI.parse(uri)
|
uri = URI.parse(uri)
|
||||||
set_request_headers(headers)
|
set_request_headers(headers)
|
||||||
params = hash_to_params(params)
|
uri, params = resolve_parameters(uri, method, params)
|
||||||
response = http_request uri, method, params
|
response = http_request(uri, method, params)
|
||||||
body = response.not_nil!.body
|
body = response.not_nil!.body
|
||||||
page = response_parse(response, body, uri)
|
page = response_parse(response, body, uri)
|
||||||
# save cookies
|
# save cookies
|
||||||
@ -24,13 +24,13 @@ module MechanizeCr
|
|||||||
end
|
end
|
||||||
|
|
||||||
def http_request(uri, method, params)
|
def http_request(uri, method, params)
|
||||||
client = ::HTTP::Client.new(uri.host.not_nil!)
|
|
||||||
path = compose_path(uri, params)
|
|
||||||
case uri.scheme.not_nil!.downcase
|
case uri.scheme.not_nil!.downcase
|
||||||
when "http", "https" then
|
when "http", "https" then
|
||||||
case method
|
case method
|
||||||
when :get
|
when :get
|
||||||
client.get(path, headers: request_headers)
|
::HTTP::Client.get(uri, headers: request_headers)
|
||||||
|
when :post
|
||||||
|
#client.post(path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -41,14 +41,15 @@ module MechanizeCr
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def hash_to_params(params)
|
private def resolve_parameters(uri, method, params)
|
||||||
URI::Params.encode(params)
|
case method
|
||||||
end
|
when :get
|
||||||
|
query = URI::Params.encode(params)
|
||||||
private def compose_path(uri, params)
|
uri.query = query
|
||||||
path = uri.path
|
return uri, nil
|
||||||
path += "?#{params}" unless params.empty?
|
else
|
||||||
path
|
return uri, params
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def response_parse(response, body, uri)
|
private def response_parse(response, body, uri)
|
||||||
|
Loading…
Reference in New Issue
Block a user