Sha256: 74992ddf7555ba0abd8508fecf7bb88d42c4edf3c188d5ad3d767523afc79ad0

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module WhatWeb
  class Target
    using WhatWeb::Helper

    attr_accessor :response
    attr_reader :user_agent
    attr_reader :url, :body, :headers, :raw_headers, :raw_response, :status, :uri

    def initialize(url, opts = {})
      @url = url.to_s
      @user_agent = opts[:user_agent] || "WhatWeb/#{VERSION}"
      @response = opts[:response] || open_url
      build
    end

    def open_url
      HTTP.headers(user_agent: user_agent).get url
    end

    def build
      @body = response.body.to_s
      @headers = response.headers.to_a.map { |k, v| [k.downcase, v] }.to_h
      @headers["set-cookie"] = set_cookie if response.headers["Set-Cookie"]
      @raw_headers = response.headers.to_a.map { |h| h.join(":") }.join("\n")
      @raw_response = body + raw_headers
      @status = response.status
      @uri = response.uri
    end

    def set_cookie
      cookie = response.headers["Set-Cookie"]
      cookie.is_a?(String) ? cookie : cookie.join("\n")
    end

    def md5sum
      @md5sum ||= response.md5sum
    end

    def tag_pattern
      @tag_pattern ||= response.tag_pattern
    end

    def text
      @text ||= response.text
    end

    def self.meta_refresh_regex
      /<meta[\s]+http\-equiv[\s]*=[\s]*['"]?refresh['"]?[^>]+content[\s]*=[^>]*[0-9]+;[\s]*url=['"]?([^"'>]+)['"]?[^>]*>/i
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_whatweb-0.4.1 lib/whatweb/target.rb
simple_whatweb-0.4.0 lib/whatweb/target.rb
simple_whatweb-0.3.0 lib/whatweb/target.rb