Class: Lazy::Checker::Url
- Inherits:
-
Object
- Object
- Lazy::Checker::Url
- Defined in:
- lib/lazy/check/checker_url.rb
Constant Summary collapse
- REG_REDIRECTION =
/<meta.+http-equiv="refresh".+content="[0-9]+;(.+)">/.freeze
Instance Attribute Summary collapse
-
#uri_string ⇒ Object
readonly
Returns the value of attribute uri_string.
Instance Method Summary collapse
- #code_html ⇒ Object
-
#initialize(uri) ⇒ Url
constructor
Instanciation d’un test.
-
#nokogiri ⇒ Object
Nokogiri Document.
-
#ok? ⇒ Boolean
True si la page a pu être chargée correctement.
- #readit ⇒ Object
-
#redirect_to ⇒ Object
pouvoir l’utiliser.
-
#redirection? ⇒ Boolean
True si la page est une redirection.
-
#rvalue ⇒ Object
La response.value.
Constructor Details
#initialize(uri) ⇒ Url
Instanciation d’un test
14 15 16 |
# File 'lib/lazy/check/checker_url.rb', line 14 def initialize(uri) @uri_string = uri.strip end |
Instance Attribute Details
#uri_string ⇒ Object (readonly)
Returns the value of attribute uri_string.
8 9 10 |
# File 'lib/lazy/check/checker_url.rb', line 8 def uri_string @uri_string end |
Instance Method Details
#code_html ⇒ Object
50 51 52 |
# File 'lib/lazy/check/checker_url.rb', line 50 def code_html @code_html ||= readit end |
#nokogiri ⇒ Object
Returns Nokogiri Document.
19 20 21 |
# File 'lib/lazy/check/checker_url.rb', line 19 def nokogiri @nokogiri ||= Nokogiri::XML(code_html)#.tap { |n| dbg("Classe : #{n.class}".bleu)} end |
#ok? ⇒ Boolean
Returns true si la page a pu être chargée correctement.
26 27 28 |
# File 'lib/lazy/check/checker_url.rb', line 26 def ok? not(code_html.nil?) end |
#readit ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lazy/check/checker_url.rb', line 54 def readit if uri_string.start_with?('http') uri = URI(uri_string) begin response = Net::HTTP.get_response(uri) rescue SocketError => e @rvalue = e..match(/([4][0-9][0-9])/).to_a[1].to_i return rescue Net::HTTPServerException => e @rvalue = e..match(/([4][0-9][0-9])/).to_a[1].to_i return rescue Net::HTTPClientException => e @rvalue = e..match(/([4][0-9][0-9])/).to_a[1].to_i return end begin @rvalue = response.value # rescue Net::HTTPServerException => e # @rvalue = e.message.match(/([4][0-9][0-9])/).to_a[1].to_i # puts "rvalue: #{@rvalue.inspect}".jaune # exit # return rescue Net::HTTPClientException => e @rvalue = e..match(/([4][0-9][0-9])/).to_a[1].to_i return end case response when Net::HTTPSuccess body = response.body # toute la page html @rvalue = response.code.to_i # dbg("response.value = #{response.methods.inspect}".bleu) # dbg("response.code = #{response.code.inspect}".bleu) if body.match?(REG_REDIRECTION) # # -- la page html définit une redirection par # balise meta -- # @redirect_to = body.match(REG_REDIRECTION).to_a[1].strip return nil else # # Un corps de page normal (note : <html>...</html>) # return body end when Net::HTTPRedirect @redirect_to = response['location'] return nil else return nil end elsif uri_string.start_with?('<') and uri_string.end_with?('>') uri_string else raise ArgumentError.new(ERRORS[201] % {a:uri_string.inspect}) end end |
#redirect_to ⇒ Object
Note:
Il faut avoir appelé #code_html ou #read avant de
pouvoir l’utiliser.
40 41 42 |
# File 'lib/lazy/check/checker_url.rb', line 40 def redirect_to @redirect_to end |
#redirection? ⇒ Boolean
Note:
la redirection se trouve dans @redirect_to
Returns true si la page est une redirection.
32 33 34 |
# File 'lib/lazy/check/checker_url.rb', line 32 def redirection? code_html.nil? && not(@redirect_to.nil?) end |
#rvalue ⇒ Object
Note:
Il faut que #code_html ou #read ait été appelé avant
Returns la response.value.
46 47 48 |
# File 'lib/lazy/check/checker_url.rb', line 46 def rvalue @rvalue end |