How to get a remote-file's mtime before downloading it in Ruby?

后端 未结 3 511
名媛妹妹
名媛妹妹 2021-02-06 14:57

I have the below code, which simply downloads a file and saves it. I want to run it every 30 seconds and check if the remote-file\'s mtime has changed and download it if it has.

3条回答
  •  半阙折子戏
    2021-02-06 15:35

    Before you do your http.get do an http.head which requests just the headers without downloading the body (i.e. the file contents) then check if the value of the Last Modified header has changed.

    e.g.

    resp = http.head(($xmlServerPath+"levels.xml")
    last_modified = resp['last-modified']
    if last_modified != previous_last_modified
      # file has changed
    end
    

提交回复
热议问题