Class: WebUpdateChecker::Checker

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

Instance Method Summary collapse

Constructor Details

#initialize(url, regex = nil, mail = nil) ⇒ Checker

Returns a new instance of Checker.

Parameters:

  • url (String)

    to Checker

  • Regex (Regex)

    of to match comparing text

  • instance (Mail)

    of Mail



20
21
22
23
24
25
26
# File 'lib/web_update_checker.rb', line 20

def initialize(url, regex = nil, mail = nil)
  @url  = url
  @regex = regex
  @mail = mail
  @tmp_file_last    = '/tmp/site_last.txt'
  @tmp_file_current = '/tmp/site_current.txt'
end

Instance Method Details

#cleanupObject



87
88
89
90
# File 'lib/web_update_checker.rb', line 87

def cleanup
  File.unlink(@tmp_file_last)    if File.exists?(@tmp_file_last)
  File.unlink(@tmp_file_current) if File.exists?(@tmp_file_current)
end

#executeObject

Parameters:

  • true (Bool)

    if changed, false if no change, nil if first time.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/web_update_checker.rb', line 32

def execute

  uri = URI.parse(@url)


  uri = URI(@url)
  req = Net::HTTP::Get.new(uri.path)

  response = Net::HTTP.start(
    uri.host, uri.port, 
    :use_ssl => uri.scheme == 'https', 
    :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
      https.request(req)
    end



    matched_body = nil
    if @regex
      match = @regex.match(response.body)
      if match.nil?
        raise "Regex does not match"
      end
      matched_body = match[1]
    else
      matched_body = response.body
    end


    unless File.exist?(@tmp_file_last)
      # first time
      File.write(@tmp_file_last,    matched_body)
      File.write(@tmp_file_current, matched_body)
      return nil
    else
      # 2nd time
      File.write(@tmp_file_current, matched_body)
    end


    result = nil
    unless FileUtils.compare_file(@tmp_file_last, @tmp_file_current)
      @mail.deliver! if @mail
      result = true
    else
      result = false
    end

    File.write(@tmp_file_last,    matched_body)

    return result

end