From 803e24203563df8cfcfadd3a7d6643d008b3cf11 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Mon, 3 May 2021 12:56:08 +0900 Subject: [PATCH] make page class --- src/mechanize.cr | 7 +++++++ src/mechanize/file.cr | 17 +++++++++++++++++ src/mechanize/http/agent.cr | 13 ++++++++++--- src/mechanize/page.cr | 6 ++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/mechanize/file.cr create mode 100644 src/mechanize/page.cr diff --git a/src/mechanize.cr b/src/mechanize.cr index 0b6f565..325d920 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -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 diff --git a/src/mechanize/file.cr b/src/mechanize/file.cr new file mode 100644 index 0000000..423db3d --- /dev/null +++ b/src/mechanize/file.cr @@ -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 diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index a424162..2f7f0c1 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -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 diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr new file mode 100644 index 0000000..e9b56ef --- /dev/null +++ b/src/mechanize/page.cr @@ -0,0 +1,6 @@ +require "./file" +class MechanizeCr::Page < MechanizeCr::File + def initialize(uri, response, body, code) + super(uri, response, body, code) + end +end