Class: UrlVerifier::RunCurler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ RunCurler

Returns a new instance of RunCurler.



6
7
8
9
10
11
# File 'lib/url_verifier/run_curler.rb', line 6

def initialize(args={})
  @timeout_limit = args.fetch(:timeout_limit, 60)
  @web_formatter = CrmFormatter::Web.new
  @curler = UrlVerifier::Curler.new
  @time_now = nil
end

Instance Attribute Details

#time_nowObject

Returns the value of attribute time_now.



4
5
6
# File 'lib/url_verifier/run_curler.rb', line 4

def time_now
  @time_now
end

Instance Method Details

#check_for_redirect(url_hash) ⇒ Object



54
55
56
57
58
59
# File 'lib/url_verifier/run_curler.rb', line 54

def check_for_redirect(url_hash)
  ver = url_hash[:verified_url]
  form = url_hash[:url_f]
  url_hash[:url_redirected] = ver.present? && ver != form
  url_hash
end

#check_timeObject



17
18
19
# File 'lib/url_verifier/run_curler.rb', line 17

def check_time
  @time_now = Time.now unless @time_now == 'rspec_time'
end

#evaluate_curl_err(curl_err) ⇒ Object



74
75
76
# File 'lib/url_verifier/run_curler.rb', line 74

def evaluate_curl_err(curl_err)
  curl_err == "Error: Timeout" || curl_err == "Error: Host" ? timeout = @timeout_limit : timeout = 0
end

#evaluate_formatted_url(url_hash) ⇒ Object



50
51
52
# File 'lib/url_verifier/run_curler.rb', line 50

def evaluate_formatted_url(url_hash)
  url_hash = url_hash.merge({url_sts: 'Invalid', wx_date: @time_now })
end

#merge_url_hash(url_hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/url_verifier/run_curler.rb', line 37

def merge_url_hash(url_hash)
  url_hash_fields = {
    verified_url: nil,
    url_redirected: false,
    response_code: nil,
    url_sts: nil,
    url_date: @time_now,
    wx_date: nil,
    timeout: 0
  }
  url_hash.merge(url_hash_fields)
end

#process_valid_curl_response(url_hash, curl_result) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/url_verifier/run_curler.rb', line 78

def process_valid_curl_response(url_hash, curl_result)
  curl_result[:verified_url].present? ? url_sts = 'Valid' : url_sts = "Error: Nil"

  valid_hash = {
    verified_url: curl_result[:verified_url],
    url_sts: url_sts,
    response_code: curl_result[:response_code],
    timeout: 0
  }
  url_hash = url_hash.merge(valid_hash)
end

#send_to_curl(url_hash) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/url_verifier/run_curler.rb', line 61

def send_to_curl(url_hash)
  curl_result = @curler.start_curl(url_hash[:url_f], @timeout_limit)
  curl_err = curl_result[:curl_err]

  if curl_err.present?
    url_hash = url_hash.merge({ url_sts: curl_err, timeout: evaluate_curl_err(curl_err) })
  else
    url_hash = process_valid_curl_response(url_hash, curl_result)
  end

  url_hash
end

#verify_url(url) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/url_verifier/run_curler.rb', line 21

def verify_url(url)
  check_time
  url_hash = @web_formatter.format_url(url)
  url_hash = merge_url_hash(url_hash)

  if url_hash[:url_f].present?
    url_hash = send_to_curl(url_hash)
    url_hash = check_for_redirect(url_hash)
  else
    url_hash = evaluate_formatted_url(url_hash)
  end

  puts url_hash.inspect
  url_hash
end

#verify_urls(urls = []) ⇒ Object



13
14
15
# File 'lib/url_verifier/run_curler.rb', line 13

def verify_urls(urls=[])
  url_hashes = urls.map { |url| verify_url(url) }
end