make page class
This commit is contained in:
parent
41a072cb9c
commit
803e242035
@ -1,10 +1,12 @@
|
||||
require "./mechanize/http/agent"
|
||||
require "./mechanize/page"
|
||||
|
||||
class Mechanize
|
||||
VERSION = "0.1.0"
|
||||
|
||||
def initialize()
|
||||
@agent = MechanizeCr::HTTP::Agent.new
|
||||
@agent.context = self
|
||||
end
|
||||
|
||||
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)
|
||||
@agent.request_headers = request_headers
|
||||
end
|
||||
|
||||
def parse(uri, response, body)
|
||||
code = response.not_nil!.status_code
|
||||
MechanizeCr::Page.new(uri, response, body, code)
|
||||
end
|
||||
end
|
||||
|
17
src/mechanize/file.cr
Normal file
17
src/mechanize/file.cr
Normal file
@ -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 HTTP
|
||||
class Agent
|
||||
property :request_headers
|
||||
property :request_headers, :context
|
||||
|
||||
def initialize()
|
||||
def initialize(@context : Mechanize | Nil = nil)
|
||||
@request_headers = ::HTTP::Headers.new
|
||||
@context = context
|
||||
end
|
||||
|
||||
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new)
|
||||
add_request_headers(headers)
|
||||
params = hash_to_params(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
|
||||
|
||||
def http_request(uri, method, params)
|
||||
@ -45,6 +48,10 @@ module MechanizeCr
|
||||
path += "?#{params}" unless params.empty?
|
||||
path
|
||||
end
|
||||
|
||||
private def response_parse (response, body, uri)
|
||||
@context.not_nil!.parse uri, response, body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
6
src/mechanize/page.cr
Normal file
6
src/mechanize/page.cr
Normal file
@ -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
Block a user