class Easywins::Target
Constants
- REDIRECT_CODES
- URL_REGEX
Attributes
base_url[R]
Public Class Methods
new(base_url, options = {})
click to toggle source
# File lib/easywins/target.rb, line 14 def initialize(base_url, options = {}) @base_url = validate_and_normalize_url!(base_url) @options = options end
Public Instance Methods
alive?()
click to toggle source
# File lib/easywins/target.rb, line 28 def alive? response = request('/', true) rescue *HttpClient::HANDLED_EXCEPTIONS false end
redirects?()
click to toggle source
# File lib/easywins/target.rb, line 34 def redirects? final_url = validate_and_normalize_url!(follow_redirects!("#{@base_url}/")) if @base_url != final_url @base_url = final_url true else false end end
request(path, use_get = false)
click to toggle source
# File lib/easywins/target.rb, line 19 def request(path, use_get = false) url = "#{@base_url}#{path}" if use_get http_client.do_get(url, {}, :verify => @options[:verify]) else http_client.do_head(url, {}, :verify => @options[:verify]) end end
Private Instance Methods
follow_redirects!(url, redirects = 5)
click to toggle source
# File lib/easywins/target.rb, line 59 def follow_redirects!(url, redirects = 5) raise TooManyRedirectsError.new() if redirects.zero? response = http_client.do_get(url, {}, :verify => @options[:verify]) if REDIRECT_CODES.include?(response.code) follow_redirects!(response.headers['location'], redirects-1) else url end end
http_client()
click to toggle source
# File lib/easywins/target.rb, line 73 def http_client @http_client ||= HttpClient.new(@options) end
valid_url?(url)
click to toggle source
# File lib/easywins/target.rb, line 69 def valid_url?(url) url =~ URL_REGEX end
validate_and_normalize_url!(url)
click to toggle source
# File lib/easywins/target.rb, line 46 def validate_and_normalize_url!(url) url = "http://#{url}" unless url =~ /^https?:\/\//i raise InvalidBaseUrlError.new("#{url} is an invalid URL") unless valid_url?(url) normalized_url = url.dup use_ssl = (normalized_url =~ /^https/) || (normalized_url =~ /:443\b/) normalized_url.chop! if normalized_url.end_with?('?') normalized_url.chop! if normalized_url.end_with?('/') normalized_url.gsub!(/^https?:\/\//i, '') "http#{'s' if use_ssl}://#{normalized_url}".downcase end