Class: PeepingTom::Site
- Inherits:
-
Object
- Object
- PeepingTom::Site
- Defined in:
- lib/peeping_tom/site.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize(name, url, opts = {}) ⇒ Site
constructor
A new instance of Site.
- #ping(method = :head) ⇒ Object
- #url ⇒ Object
- #verify(regex = //) ⇒ Object
- #with(*args) ⇒ Object
- #with_args ⇒ Object
Constructor Details
#initialize(name, url, opts = {}) ⇒ Site
Returns a new instance of Site.
5 6 7 8 9 |
# File 'lib/peeping_tom/site.rb', line 5 def initialize(name, url, opts = {}) @name = name @url = url @timeout = opts[:timeout] || 10 end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/peeping_tom/site.rb', line 3 def name @name end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
3 4 5 |
# File 'lib/peeping_tom/site.rb', line 3 def timeout @timeout end |
Instance Method Details
#error? ⇒ Boolean
58 59 60 |
# File 'lib/peeping_tom/site.rb', line 58 def error? @error end |
#ping(method = :head) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/peeping_tom/site.rb', line 38 def ping(method = :head) method = method.to_s.downcase method = 'head' if method == '' raise ArgumentError, "only accepts head or get: #{method}" unless ['head', 'get'].include?(method) Timeout::timeout(self.timeout) do url = URI.parse(self.url) res = Net::HTTP.start(url.host, url.port) do |http| file = "/" + url.to_s.sub(/http:\/\/.*?\//, '') http.send(method, file) end return res.code.to_i >= 200 && res.code.to_i < 400 end @error = true return false rescue Timeout::Error, Errno::ECONNREFUSED, SocketError @error = true return false end |
#url ⇒ Object
11 12 13 14 15 |
# File 'lib/peeping_tom/site.rb', line 11 def url @url = @url + "/" unless @url =~ /\// @url = "http://#{@url}" unless @url =~ /^http:\/\// @url end |
#verify(regex = //) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/peeping_tom/site.rb', line 17 def verify(regex = //) Timeout::timeout(self.timeout) do url = URI.parse(self.url) res = Net::HTTP.start(url.host, url.port) do |http| file = "/" + url.to_s.sub(/http:\/\/.*?\//, '') http.get(file) end if res.body =~ regex return true else @error = true return false end end @error = true return false rescue Timeout::Error, Errno::ECONNREFUSED, SocketError @error = true return false end |
#with(*args) ⇒ Object
62 63 64 |
# File 'lib/peeping_tom/site.rb', line 62 def with(*args) @with = args end |
#with_args ⇒ Object
66 67 68 |
# File 'lib/peeping_tom/site.rb', line 66 def with_args @with end |