refactor code

master
Kanezoh 2021-08-06 08:04:47 +09:00
parent 3d4dfc4568
commit e3b712a36c
1 changed files with 16 additions and 11 deletions

View File

@ -4,9 +4,10 @@ require "http/client"
module MechanizeCr module MechanizeCr
module HTTP module HTTP
class Agent class Agent
property :request_headers, :context, :cookies property :request_headers, :context
property history : Array(Page) property history : Array(Page)
property user_agent : String property user_agent : String
property cookies : Hash(String, ::HTTP::Cookies)
def initialize(@context : Mechanize | Nil = nil) def initialize(@context : Mechanize | Nil = nil)
@history = Array(Page).new @history = Array(Page).new
@ -19,13 +20,13 @@ 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 = resolve(uri) uri = resolve(uri)
set_request_headers(uri, headers) set_request_headers(uri, headers)
request_user_agent set_user_agent
uri, params = resolve_parameters(uri, method, 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
add_response_cookies(response, uri, page) save_response_cookies(response, uri, page)
page page
end end
@ -59,7 +60,13 @@ module MechanizeCr
request_headers request_headers
else else
valid_cookies = cookies[host] valid_cookies = cookies[host]
valid_cookies.not_nil!.add_request_headers(request_headers) valid_cookies.try &.add_request_headers(request_headers)
end
end
private def set_user_agent
unless user_agent == ""
request_headers["User-Agent"] = user_agent
end end
end end
@ -81,7 +88,7 @@ module MechanizeCr
@context.not_nil!.parse(uri, response, body) @context.not_nil!.parse(uri, response, body)
end end
private def add_response_cookies(response, uri, page) private def save_response_cookies(response, uri, page)
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]
@ -110,10 +117,14 @@ module MechanizeCr
end end
end end
# reset request cookie before setting headers.
private def reset_request_header_cookies private def reset_request_header_cookies
request_headers.delete("Cookie") request_headers.delete("Cookie")
end end
# save cookies as Hash(String, HTTP::Cookies)
# String means url's domain.
# ex) Hash("example.com" => HTTP::Cookies)
private def save_cookies(uri, header_cookies) private def save_cookies(uri, header_cookies)
host = uri.host.to_s host = uri.host.to_s
if cookies.fetch(host, ::HTTP::Cookies.new).empty? if cookies.fetch(host, ::HTTP::Cookies.new).empty?
@ -123,12 +134,6 @@ module MechanizeCr
cookies[host] << cookie cookies[host] << cookie
end end
end end
private def request_user_agent
unless user_agent == ""
request_headers["User-Agent"] = user_agent
end
end
end end
end end
end end