From 6aed9b6f88d398d4e909a6e4e052f4e10ebb7286 Mon Sep 17 00:00:00 2001 From: Chandan Jhunjhunwal Date: Tue, 30 May 2023 23:19:10 +0530 Subject: [PATCH] fixed the private method access --- .gitignore | 1 + lib/final_redirect_url.rb | 47 ++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 35445de..c63d150 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /spec/reports/ /tmp/ /vendor/bundle +/.idea # rspec failure tracking .rspec_status diff --git a/lib/final_redirect_url.rb b/lib/final_redirect_url.rb index 4aff8d9..de42570 100644 --- a/lib/final_redirect_url.rb +++ b/lib/final_redirect_url.rb @@ -19,33 +19,34 @@ def self.final_redirect_url(url, options={}) final_url end - private - def self.is_valid_url?(url) - url.to_s.match? URI::regexp(['http', 'https']) - end + class << self + def self.is_valid_url?(url) + url.to_s.match? URI::regexp(['http', 'https']) + end - def self.get_final_redirect_url(url, limit = 10) - return url if limit <= 0 - uri = URI.parse(url) - response = ::Net::HTTP.get_response(uri) - if response.class == Net::HTTPOK - return uri - else - redirect_location = response['location'] - location_uri = URI.parse(redirect_location) - if location_uri.host.nil? - redirect_location = uri.scheme + '://' + uri.host + redirect_location + def self.get_final_redirect_url(url, limit = 10) + return url if limit <= 0 + uri = URI.parse(url) + response = ::Net::HTTP.get_response(uri) + if response.class == Net::HTTPOK + return uri + else + redirect_location = response['location'] + location_uri = URI.parse(redirect_location) + if location_uri.host.nil? + redirect_location = uri.scheme + '://' + uri.host + redirect_location + end + warn "redirected to #{redirect_location}" + get_final_redirect_url(redirect_location, limit - 1) end - warn "redirected to #{redirect_location}" - get_final_redirect_url(redirect_location, limit - 1) end - end - def self.url_string_from_uri(uri) - url_str = "#{uri.scheme}://#{uri.host}#{uri.request_uri}" - if uri.fragment - url_str = url_str + "##{uri.fragment}" + def self.url_string_from_uri(uri) + url_str = "#{uri.scheme}://#{uri.host}#{uri.request_uri}" + if uri.fragment + url_str = url_str + "##{uri.fragment}" + end + url_str end - url_str end end