Class: UrlVerifier::Curler

Inherits:
Object
  • Object
show all
Defined in:
lib/url_verifier/curler.rb

Instance Method Summary collapse

Constructor Details

#initializeCurler

Returns a new instance of Curler.



10
11
12
13
# File 'lib/url_verifier/curler.rb', line 10

def initialize
  @web_formatter = CrmFormatter::Web.new
  @ran_again = false
end

Instance Method Details

#error_parser(curl_err) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/url_verifier/curler.rb', line 82

def error_parser(curl_err)
  if curl_err.include?("Couldn't connect to server")
    curl_err = "Error: Expired Url"
  elsif curl_err.include?("SSL connect error")
    curl_err = "Error: SSL"
  elsif curl_err.include?("Couldn't resolve host name")
    curl_err = "Error: Host"
  elsif curl_err.include?("Peer certificate")
    curl_err = "Error: Certificate"
  elsif curl_err.include?("Failure when receiving data")
    curl_err = "Error: Transfer"
  elsif curl_err.include?("TCP connection")
    curl_err = "Error: TCP"
  else
    curl_err = "Error: Undefined"
  end
  curl_err
end

#https_to_http(url) ⇒ Object



72
73
74
# File 'lib/url_verifier/curler.rb', line 72

def https_to_http(url)
  url = url.gsub('https://', 'http://')
end

#pre_curl_msg(url, timeout) ⇒ Object



76
77
78
79
80
# File 'lib/url_verifier/curler.rb', line 76

def pre_curl_msg(url, timeout)
  msg = "\n\n#{'='*40}\nVERIFYING: #{url}\nMax Wait Set: #{timeout} Seconds\n\n"
  puts msg
  msg
end

#run_again(curl_result, url, timeout) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/url_verifier/curler.rb', line 57

def run_again(curl_result, url, timeout)
  if curl_result[:curl_err].present?
    if @ran_again == false
      @ran_again = true
      url = https_to_http(url)
      curl_result = start_curl(url, timeout)
    else
      @ran_again = false
    end
  else
    @ran_again = false
  end
  curl_result
end

#start_curl(url, timeout) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/url_verifier/curler.rb', line 15

def start_curl(url, timeout)
  curl_result = { verified_url: nil, response_code: nil, curl_err: nil }
  if url.present?
    result = nil

    begin # Curl Exception Handling
      begin # Timeout Exception Handling
        pre_curl_msg(url, timeout)
        Timeout.timeout(timeout) do
          result = Curl::Easy.perform(url) do |curl|
            curl.follow_location = true
            curl.useragent = "curb"
            curl.connect_timeout = timeout
            curl.enable_cookies = true
            curl.head = true #testing - new
          end # result

          curl_result[:response_code] = result&.response_code.to_s
          web_hsh = @web_formatter.format_url(result&.last_effective_url)

          url_f = web_hsh[:url_f]
          curl_result[:verified_url] = url_f if url_f.present?
        end

      rescue Timeout::Error # Timeout Exception Handling
        curl_result[:curl_err] = "Error: Timeout"
      end

    # rescue LoadError => e  # Curl Exception Handling
    rescue StandardError => e
      curl_err = error_parser("Error: #{$!.message}")
      # CheckInt.new.check_int if curl_err.include?('TCP')
      curl_result[:curl_err] = curl_err
    end
  else ## If no url present?
    curl_result[:curl_err] = 'URL Nil'
  end

  curl_result = run_again(curl_result, url, timeout)
  curl_result
end