Class: Lazy::Checker::Url

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Url

Instanciation d’un test

Parameters:

  • uri (String)

    URL ou code



14
15
16
# File 'lib/lazy/check/checker_url.rb', line 14

def initialize(uri)
  @uri_string = uri.strip
end

Instance Attribute Details

#uri_stringObject (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_htmlObject



50
51
52
# File 'lib/lazy/check/checker_url.rb', line 50

def code_html
  @code_html ||= readit
end

#nokogiriObject

Returns Nokogiri Document.

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.

Returns:

  • (Boolean)

    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

#readitObject



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.message.match(/([4][0-9][0-9])/).to_a[1].to_i
      return
    rescue Net::HTTPServerException => e
      @rvalue = e.message.match(/([4][0-9][0-9])/).to_a[1].to_i
      return
    rescue Net::HTTPClientException => e
      @rvalue = e.message.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.message.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_toObject

Note:

Il faut avoir appelé #code_html ou #read avant de

pouvoir l’utiliser.

Returns:

  • la redirection



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.

Returns:

  • (Boolean)

    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

#rvalueObject

Note:

Il faut que #code_html ou #read ait été appelé avant

Returns la response.value.

Returns:

  • la response.value



46
47
48
# File 'lib/lazy/check/checker_url.rb', line 46

def rvalue
  @rvalue
end