class DeepHealthCheck::HTTPDependencyHealthCheck

Public Class Methods

new() click to toggle source
# File lib/deep_health_check/http_dependency_health_check.rb, line 8
def initialize
  @type = 'http'
  @dependencies = process_dependencies fetch_dependencies_from_env
end

Private Instance Methods

faraday() click to toggle source
# File lib/deep_health_check/http_dependency_health_check.rb, line 40
def faraday
  Faraday.new do |builder|
    builder.response :json, content_type: /\bjson$/
    builder.response(:encoding) if defined?(Faraday::Encoding)
    builder.adapter Faraday.default_adapter
  end
end
health_status_code() click to toggle source
# File lib/deep_health_check/http_dependency_health_check.rb, line 26
def health_status_code
  failed = @dependencies.any? do |_name, response|
    !response[:status] || response[:status] >= 300
  end
  failed ? 503 : 200
end
http_status(url) click to toggle source
# File lib/deep_health_check/http_dependency_health_check.rb, line 33
def http_status(url)
  response = faraday.get url
  { status: response.status, details: response.body }
rescue RuntimeError, Faraday::Error => e
  { status: nil, details: e.inspect }
end
process_dependencies(dependencies_list) click to toggle source
# File lib/deep_health_check/http_dependency_health_check.rb, line 15
def process_dependencies(dependencies_list)
  threads = dependencies_list.map do |url|
    Thread.new do
      { url.to_s => http_status(url) }
    end
  end

  threads.each(&:join)
  threads.map(&:value).reduce(&:merge)
end