Class: VagrantPlugins::BoxUpdater::Util::Common

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-box-updater/util/common.rb

Class Method Summary collapse

Class Method Details

.add_box_stats(stat_file, box_attributes) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/vagrant-box-updater/util/common.rb', line 17

def self.add_box_stats(stat_file, box_attributes)
  YAML::ENGINE.yamler='syck'
	  content = YAML.load_file(stat_file)
	  content = content.merge(box_attributes)
  #env[:ui].info("Save to: #{stat_file}")
  File.open(stat_file, 'w+') {|f| f.write(content.to_yaml) }
end

.fetch_url(uri_str, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vagrant-box-updater/util/common.rb', line 47

def self.fetch_url(uri_str, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  uri = URI.parse(uri_str)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.port == 443)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  #request = Net::HTTP::Get.new(uri.request_uri, { 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31' })
  #response = Net::HTTP.start(uri.host, uri.port) { |http| http.request(request) }
  #response = http.request(request)
  response = http.head(uri.request_uri)
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then fetch_url(response['location'], limit - 1)
  else
    response.error!
  end
end

.get_local_file_modification_date?(url) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/vagrant-box-updater/util/common.rb', line 68

def self.get_local_file_modification_date?(url)
  mtime = File.mtime(url)
  return { 'Last-Modified' => mtime }
end

.get_modification_attribute(box_path) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-box-updater/util/common.rb', line 31

def self.get_modification_attribute(box_path)
  if !box_path.start_with? "http"
    ref_modification_attribute = method(:get_local_file_modification_date?)
  else
    ref_modification_attribute = method(:get_url_modification_attribute?)
  end
    modification_attribute = ref_modification_attribute.call(box_path)
    return modification_attribute
end

.get_path_box_stat_file(env, box_name) ⇒ Object



6
7
8
9
10
# File 'lib/vagrant-box-updater/util/common.rb', line 6

def self.get_path_box_stat_file(env, box_name)
  YAML::ENGINE.yamler='syck'
  stat_file = env[:home_path].join(box_name + ".stat")
	  return stat_file
end

.get_url_modification_attribute?(url) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/vagrant-box-updater/util/common.rb', line 41

def self.get_url_modification_attribute?(url)
  response = fetch_url(url)
  return { 'Last-Modified' => response['Last-Modified'] } if response['Last-Modified']
  return { 'Etag' => response['ETag'].delete("\"") } if response['Etag']
end

.read_box_stats(stat_file, box_name) ⇒ Object



25
26
27
28
29
# File 'lib/vagrant-box-updater/util/common.rb', line 25

def self.read_box_stats(stat_file, box_name)
  YAML::ENGINE.yamler='syck'
	  content = YAML.load_file(stat_file)
	  return content
end

.save_box_stats(stat_file, box_attributes) ⇒ Object



12
13
14
15
# File 'lib/vagrant-box-updater/util/common.rb', line 12

def self.save_box_stats(stat_file, box_attributes)
  YAML::ENGINE.yamler='syck'
  File.open(stat_file, 'w+') {|f| f.write(box_attributes.to_yaml) }
end