class EnvatoOptimiser::ImageResponseStatusCodeCheck

Public Class Methods

new(image_urls) click to toggle source

Initialize an image response status code check.

@param image_urls [Array] Image URLs that you would like to check the HTTP

response status codes for.

@return [Nothing].

# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 9
def initialize(image_urls)
  @redirects    = []
  @not_found    = []
  @forbidden    = []
  @server_error = []

  fetch_image_responses(image_urls)
end

Public Instance Methods

to_h() click to toggle source

Format the image response status code results into a usable Hash.

@return [Hash]

# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 21
def to_h
  {
    :image_redirect_count     => redirect_count,
    :image_not_found_count    => not_found_count,
    :image_forbidden_count    => forbidden_count,
    :image_server_error_count => server_error_count,
  }
end

Private Instance Methods

fetch_image_responses(image_urls) click to toggle source
# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 48
def fetch_image_responses(image_urls)
  image_urls.each do |image|
    response = Net::HTTP.get_response(URI.parse(image))

    case response.code.to_i
    when 300..399
      @redirects << image
    when 404
      @not_found << image
    when 403
      @forbidden << image
    when 500..599
      @server_error << image
    end
  end
end
forbidden_count() click to toggle source
# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 40
def forbidden_count
  @forbidden.size
end
not_found_count() click to toggle source
# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 36
def not_found_count
  @not_found.size
end
redirect_count() click to toggle source
# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 32
def redirect_count
  @redirects.size
end
server_error_count() click to toggle source
# File lib/envato_optimiser/checks/image_response_status_code_check.rb, line 44
def server_error_count
  @server_error.size
end