Sha256: 766957f81b3f15c9402d066f2f0a5e97a34349ff695648cfc6736904255e41af

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

load File.dirname(__FILE__) + '/web/client.rb'

module HttpCrawler
  module Client

    class << self

      # 接收格式
      # web_name = "biquge_duquanben"
      # 返回 HttpCrawler::Web::BiqugeDuquanben::Client 实例
      #
      def for(web_name, *args)
        "HttpCrawler::Web::#{web_name.camelize}::Client".constantize.new(*args)
      end

      #
      # 接收格式
      # module_name = "HttpCrawler::Web::BiqugeDuquanben"
      # 返回 HttpCrawler::Web::BiqugeDuquanben::Client 实例
      #
      def for_module(module_name, *args)
        "#{module_name}::Client".constantize.new(*args)
      end
    end

    attr_reader :http, :uri

    #
    #  init_uri 如果未初始化@uri,则会报错
    #  继承类需要重定义 init_uri
    #
    def initialize
      raise "Client uri为空" unless init_uri
      @http = HttpCrawler::HTTP.new(uri.host, uri.port)

      @http.use_ssl = (uri.scheme == "https")

      @http.open_timeout = 5
      @http.read_timeout = 5
      @http.proxy_key = "#{self.class}"
      init_http

      Rails.logger.debug "proxy_key => #{@http.proxy_key}"
    end

    # 初始化http参数
    def init_http

    end

    #  init_uri 如果未初始化@uri,则会报错
    #  继承类需要实现 @uri = URI("http://host")
    #
    def init_uri
      @uri = nil
    end

    def header
      @header ||= init_header
    end

    def init_header
      nil
    end

    def update_header(parameter = {})
      nil
    end

    def update_proxy(proxy = {})
      @http.update_proxy(proxy)
    end

    def auto_proxy=(value)
      Rails.logger.debug "自动更新代理"
      @http.auto_proxy = value
      @http.update_proxy if (value == true && @http.proxy? == false)
    end

    # 是否验证码界面
    def validation_page?(*arg)
      false
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
http_crawler-0.2.2.3 lib/http_crawler/client.rb