Class: CompactIndexClient::Updater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fetcher) ⇒ Updater

Returns a new instance of Updater.



4
5
6
# File 'lib/compact_index_client/updater.rb', line 4

def initialize(fetcher)
  @fetcher = fetcher
end

Instance Attribute Details

#fetcherObject (readonly)

Returns the value of attribute fetcher.



3
4
5
# File 'lib/compact_index_client/updater.rb', line 3

def fetcher
  @fetcher
end

Instance Method Details

#checksum_for_file(path) ⇒ Object



24
25
26
27
# File 'lib/compact_index_client/updater.rb', line 24

def checksum_for_file(path)
  return nil unless path.file?
  Digest::MD5.file(path).hexdigest
end

#update(local_path, remote_path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/compact_index_client/updater.rb', line 8

def update(local_path, remote_path)
  headers = {}
  if local_path.file?
    headers["If-None-Match"] = checksum_for_file(local_path)
    headers["Range"] = "bytes=#{local_path.size}-"
  end
  response = fetcher.call(remote_path, headers)
  return if response.is_a?(Net::HTTPNotModified)
  mode = response.is_a?(Net::HTTPPartialContent) ? "a" : "w"
  local_path.open(mode) {|f| f << response.body }

  return if checksum_for_file(local_path) == response["ETag"]
  local_path.delete
  update(local_path, remote_path)
end