Class: Pinger

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expected_redirect_urlObject

Returns the value of attribute expected_redirect_url.



3
4
5
# File 'lib/pinger.rb', line 3

def expected_redirect_url
  @expected_redirect_url
end

#expected_status_codeObject

Returns the value of attribute expected_status_code.



3
4
5
# File 'lib/pinger.rb', line 3

def expected_status_code
  @expected_status_code
end

#follow_locationObject

Returns the value of attribute follow_location.



3
4
5
# File 'lib/pinger.rb', line 3

def follow_location
  @follow_location
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/pinger.rb', line 3

def url
  @url
end

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pinger.rb', line 8

def perform
  if response_code.to_i == @expected_status_code
    # Everything's fine
    puts "Sucessfully ping #{url} with expected status code #{@expected_status_code}"
    if @expected_status_code == 301 and @expected_redirect_url
      if redirect_url == @expected_redirect_url
        # Everything's fine
        puts "Sucessfully redirect #{url} to #{@expected_redirect_url} with status #{response_code}"
      else
        message = "Unable to redirect #{url} to #{@expected_redirect_url} with expected status code #{@expected_status_code} - Received location #{redirect_url} with #{response_code} instead"   
        puts "Something went wrong, we must notify someone"
        Notifier.new(message).perform
      end
    end
  else
    message = "Unable to ping #{url} with expected status code #{@expected_status_code} - Received #{response_code} instead"
    puts "Something went wrong, we must notify someone"
    Notifier.new(message).perform
  end
end

#redirect_urlObject



34
35
36
# File 'lib/pinger.rb', line 34

def redirect_url
  request.redirect_url
end

#requestObject



38
39
40
41
42
43
44
# File 'lib/pinger.rb', line 38

def request
  @request ||= Curl::Easy.new(@url) do |curl|
    curl.follow_location = @follow_location
  end
  @request.perform
  @request
end

#response_codeObject



30
31
32
# File 'lib/pinger.rb', line 30

def response_code
  request.response_code
end