Sha256: ad533b5c0f56246bee5f1a877941de8e610770fc2e7de15b66bb1fe588d2f1a9

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# -*- encoding: utf-8 -*-

require 'open-uri'
require 'open_uri_redirections'
require 'timeout'

module MetaInspector

  # Makes the request to the server
  class Request
    include MetaInspector::Exceptionable

    def initialize(initial_url, options = {})
      options = defaults.merge(options)

      @url                = initial_url
      @allow_redirections = options[:allow_redirections]
      @timeout            = options[:timeout]
      @exception_log      = options[:exception_log]
    end

    extend Forwardable
    def_delegators :@url, :url

    def read
      response.read if response
    end

    def content_type
      response.content_type if response
    end

    private

    def response
      Timeout::timeout(@timeout) { @response ||= fetch }

      rescue TimeoutError, SocketError => e
        @exception_log << e
        nil
    end

    def fetch
      request = open(url, {:allow_redirections => @allow_redirections})

      @url.url = request.base_uri.to_s

      request
    end

    def defaults
      { allow_redirections: false, timeout: 20, exception_log: MetaInspector::ExceptionLog.new }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metainspector-1.17.1 lib/meta_inspector/request.rb
metainspector-1.17.0 lib/meta_inspector/request.rb