Class: CMSScanner::WebSite

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

Overview

WebSite Implementation

Direct Known Subclasses

Target

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_url, opts = {}) ⇒ WebSite

Returns a new instance of WebSite.

Parameters:

  • site_url (String)
  • opts (Hash) (defaults to: {})


8
9
10
11
# File 'lib/cms_scanner/web_site.rb', line 8

def initialize(site_url, opts = {})
  self.url = site_url.dup
  @opts    = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/cms_scanner/web_site.rb', line 4

def opts
  @opts
end

#uriObject (readonly)

Returns the value of attribute uri.



4
5
6
# File 'lib/cms_scanner/web_site.rb', line 4

def uri
  @uri
end

Instance Method Details

#access_forbidden?(path = nil) ⇒ Boolean

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


58
59
60
# File 'lib/cms_scanner/web_site.rb', line 58

def access_forbidden?(path = nil)
  NS::Browser.get(url(path)).code == 403
end

#http_auth?(path = nil) ⇒ Boolean

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


51
52
53
# File 'lib/cms_scanner/web_site.rb', line 51

def http_auth?(path = nil)
  NS::Browser.get(url(path)).code == 401
end

#online?(path = nil) ⇒ Boolean

Checks if the remote website is up.

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


44
45
46
# File 'lib/cms_scanner/web_site.rb', line 44

def online?(path = nil)
  NS::Browser.get(url(path)).code != 0
end

#proxy_auth?(path = nil) ⇒ Boolean

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (Boolean)


65
66
67
# File 'lib/cms_scanner/web_site.rb', line 65

def proxy_auth?(path = nil)
  NS::Browser.get(url(path)).code == 407
end

#redirection(url = nil) ⇒ String

As webmock does not support redirects mocking, coverage is ignored :nocov:

Parameters:

  • url (String) (defaults to: nil)

Returns:

  • (String)

    The redirection url or nil



75
76
77
78
79
80
# File 'lib/cms_scanner/web_site.rb', line 75

def redirection(url = nil)
  url ||= @uri.to_s
  res   = NS::Browser.get(url, followlocation: true)

  res.effective_url == url ? nil : res.effective_url
end

#url(path = nil) ⇒ String

Used for convenience

URI.encode is preferered over Addressable::URI.encode as it will encode leading # character: URI.encode(‘#t#’) => %23t%23 Addressable::URI.encode(‘#t#’) => #t%23

Parameters:

  • path (String) (defaults to: nil)

    Optional path to merge with the uri

Returns:

  • (String)


33
34
35
36
37
# File 'lib/cms_scanner/web_site.rb', line 33

def url(path = nil)
  return @uri.to_s unless path

  @uri.join(URI.encode(path)).to_s
end

#url=(site_url) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/cms_scanner/web_site.rb', line 13

def url=(site_url)
  # Add a trailing slash to the site url
  site_url << '/' if site_url[-1, 1] != '/'

  # Use the validator to ensure the site_url has a correct format
  OptParseValidator::OptURL.new([]).validate(site_url)

  @uri = Addressable::URI.parse(site_url)
end