Class: UrlStatus::App

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

Instance Method Summary collapse

Instance Method Details

#default_config_fileObject



73
74
75
# File 'lib/url_status.rb', line 73

def default_config_file
	"#{ENV['HOME']}/sites.yaml"
end

#get_response(url) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/url_status.rb', line 77

def get_response(url)
	user_agent = "url-status/#{UrlStatus::VERSION}"
	url = 'http://' + url unless url.start_with? 'http'

	begin
		return RestClient.get(url, :user_agent => user_agent)
	rescue RestClient::ExceptionWithResponse => e
		raise e if e.response.nil?
		return e.response
	end
end

#mainObject



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
# File 'lib/url_status.rb', line 16

def main

	# we want to disable the text coloring if we are printing to a
	# file, or on a platform (like windows) that likely doesn't support
	# the colors
	String.disable_colorization = !$stdout.isatty

	success = true
	url_list.each do |url|
		begin
			response = get_response(url)
			success = success && response.ok?
			code = response.ok? ? response.code.to_s.green : response.code.to_s.red
			final_url = response.request.url

			text = "[#{code}] #{final_url}"
			text += " (requested #{url})".yellow unless final_url.include?(url)
			puts text					
		rescue StandardError => e
			success = false
			puts "[#{"---".red}] #{url} (#{e.to_s.red})"
		end

		$stdout.flush
	end

	# unless *every* request completed properly, return an error code
	#, then we can do something else, like send an email
	exit(false) unless success
end

#url_listObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/url_status.rb', line 47

def url_list
	opts = Trollop::options do
		version "url-status #{UrlStatus::VERSION} (c) 2016 @reednj ([email protected])"
		banner "Usage: url-status [options] [urls...]"
		opt :config, "YAML config file containing array of urls", :type => :string
	end

	if ARGV.empty?
		
		if opts[:config].nil? && !File.exist?(default_config_file)
			Trollop::educate
		end

		begin
			data_file_name = opts[:config] || default_config_file
			YAML.load_file(data_file_name)
		rescue => e
			puts "Could not open '#{data_file_name}' (#{e})"
			exit(false)
		end
	else
		ARGV.clone
	end

end