Sha256: 57e821a8df2ee5b51da5cc9cd8209ed91e0d62e0a41cc6c1b84109df3c7c29fe

Contents?: true

Size: 885 Bytes

Versions: 15

Compression:

Stored size: 885 Bytes

Contents

# frozen_string_literal: true

module Onebox
  class StatusCheck
    def initialize(url, options = Onebox.options)
      @url = url
      @options = options
      @status = -1
    end

    def ok?
      status > 199 && status < 300
    end

    def status
      check if @status == -1
      @status
    end

    def human_status
      case status
      when 0
        :connection_error
      when 200..299
        :success
      when 400..499
        :client_error
      when 500..599
        :server_error
      else
        :unknown_error
      end
    end

    private

    def check
      res = URI.open(@url, read_timeout: (@options.timeout || Onebox.options.timeout))
      @status = res.status.first.to_i
    rescue OpenURI::HTTPError => e
      @status = e.io.status.first.to_i
    rescue Timeout::Error, Errno::ECONNREFUSED, Net::HTTPError
      @status = 0
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
onebox-2.2.19 lib/onebox/status_check.rb
onebox-2.2.18 lib/onebox/status_check.rb
onebox-2.2.17 lib/onebox/status_check.rb
onebox-2.2.16 lib/onebox/status_check.rb
onebox-2.2.15 lib/onebox/status_check.rb
onebox-2.2.14 lib/onebox/status_check.rb
onebox-2.2.13 lib/onebox/status_check.rb
onebox-2.2.12 lib/onebox/status_check.rb
onebox-2.2.11 lib/onebox/status_check.rb
onebox-2.2.10 lib/onebox/status_check.rb
onebox-2.2.9 lib/onebox/status_check.rb
onebox-2.2.8 lib/onebox/status_check.rb
onebox-2.2.6 lib/onebox/status_check.rb
onebox-2.2.5 lib/onebox/status_check.rb
onebox-2.2.4 lib/onebox/status_check.rb