make page class
parent
41a072cb9c
commit
803e242035
|
@ -1,10 +1,12 @@
|
||||||
require "./mechanize/http/agent"
|
require "./mechanize/http/agent"
|
||||||
|
require "./mechanize/page"
|
||||||
|
|
||||||
class Mechanize
|
class Mechanize
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
|
|
||||||
def initialize()
|
def initialize()
|
||||||
@agent = MechanizeCr::HTTP::Agent.new
|
@agent = MechanizeCr::HTTP::Agent.new
|
||||||
|
@agent.context = self
|
||||||
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)
|
||||||
|
@ -22,4 +24,9 @@ class Mechanize
|
||||||
def request_headers=(request_headers)
|
def request_headers=(request_headers)
|
||||||
@agent.request_headers = request_headers
|
@agent.request_headers = request_headers
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse(uri, response, body)
|
||||||
|
code = response.not_nil!.status_code
|
||||||
|
MechanizeCr::Page.new(uri, response, body, code)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
require "http/client"
|
||||||
|
class MechanizeCr::File
|
||||||
|
#property :body, :filename
|
||||||
|
property :body, :code
|
||||||
|
def initialize(uri : String | Nil, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil)
|
||||||
|
@uri = uri
|
||||||
|
@body = body
|
||||||
|
@code = code
|
||||||
|
|
||||||
|
#@full_path = false unless defined? @full_path
|
||||||
|
|
||||||
|
#fill_header response
|
||||||
|
#extract_filename
|
||||||
|
|
||||||
|
#yield self if block_given?
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,17 +4,20 @@ require "http/client"
|
||||||
module MechanizeCr
|
module MechanizeCr
|
||||||
module HTTP
|
module HTTP
|
||||||
class Agent
|
class Agent
|
||||||
property :request_headers
|
property :request_headers, :context
|
||||||
|
|
||||||
def initialize()
|
def initialize(@context : Mechanize | Nil = nil)
|
||||||
@request_headers = ::HTTP::Headers.new
|
@request_headers = ::HTTP::Headers.new
|
||||||
|
@context = context
|
||||||
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)
|
||||||
add_request_headers(headers)
|
add_request_headers(headers)
|
||||||
params = hash_to_params(params)
|
params = hash_to_params(params)
|
||||||
response = http_request uri, method, params
|
response = http_request uri, method, params
|
||||||
puts response.not_nil!.body
|
body = response.not_nil!.body
|
||||||
|
page = response_parse(response, body, uri)
|
||||||
|
puts page.code
|
||||||
end
|
end
|
||||||
|
|
||||||
def http_request(uri, method, params)
|
def http_request(uri, method, params)
|
||||||
|
@ -45,6 +48,10 @@ module MechanizeCr
|
||||||
path += "?#{params}" unless params.empty?
|
path += "?#{params}" unless params.empty?
|
||||||
path
|
path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def response_parse (response, body, uri)
|
||||||
|
@context.not_nil!.parse uri, response, body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
require "./file"
|
||||||
|
class MechanizeCr::Page < MechanizeCr::File
|
||||||
|
def initialize(uri, response, body, code)
|
||||||
|
super(uri, response, body, code)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue