Sha256: 9f2987136c4c45593f428559fa5068f76cca3c6a98a269c7ef81247c5bbeb4da

Contents?: true

Size: 881 Bytes

Versions: 62

Compression:

Stored size: 881 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 = 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

62 entries across 62 versions & 1 rubygems

Version Path
onebox-1.9.28.2 lib/onebox/status_check.rb
onebox-1.9.28.1 lib/onebox/status_check.rb
onebox-1.9.28 lib/onebox/status_check.rb
onebox-1.9.27.2 lib/onebox/status_check.rb
onebox-1.9.27.1 lib/onebox/status_check.rb
onebox-1.9.26 lib/onebox/status_check.rb
onebox-1.9.25 lib/onebox/status_check.rb
onebox-1.9.24 lib/onebox/status_check.rb
onebox-1.9.23 lib/onebox/status_check.rb
onebox-1.9.22 lib/onebox/status_check.rb
onebox-1.9.21 lib/onebox/status_check.rb
onebox-1.9.20 lib/onebox/status_check.rb
onebox-1.9.19 lib/onebox/status_check.rb
onebox-1.9.18 lib/onebox/status_check.rb
onebox-1.9.17 lib/onebox/status_check.rb
onebox-1.9.16 lib/onebox/status_check.rb
onebox-1.9.15 lib/onebox/status_check.rb
onebox-1.9.14 lib/onebox/status_check.rb
onebox-1.9.13 lib/onebox/status_check.rb
onebox-1.9.12 lib/onebox/status_check.rb