Module: Box::Release::Base

Included in:
Memory
Defined in:
lib/box/release/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reference_urlObject

def presenter

@presenter ||= ReleasePresenter.new self

end



76
77
78
# File 'lib/box/release/base.rb', line 76

def reference_url
  @reference_url
end

Instance Method Details

#downloadObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/box/release/base.rb', line 86

def download
  logger.info "Download #{url} into #{file}"
  # return if self.status.downloaded?

  # update_attribute :status, :download_pending

  File.open(file, "w") do |file|
    Box::Release::Downloader.open(url) do |data, download_size, url_size|
      file.write data

      # self.url_size ||= url_size
      # self.download_size = download_size
      # save! if 10.seconds.ago > self.updated_at
    end
  end

  raise "Invalid checksum after download" unless valid_checksum?
  # self.status = :downloaded
ensure
  # self.status = :download_failed unless self.status.downloaded?
  # save!
end

#fileObject

def status=(status, update_timestamp = true)

write_attribute :status, status ? status.to_s : nil
self.status_updated_at = Time.now unless new_record?

end



64
65
66
# File 'lib/box/release/base.rb', line 64

def file
  "#{download_directory}/release.tar"
end

#file_checksumObject



145
146
147
# File 'lib/box/release/base.rb', line 145

def file_checksum
  Digest::SHA256.file(file).hexdigest if file_exists?
end

#file_exists?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/box/release/base.rb', line 149

def file_exists?
  File.exists?(file)
end

#install(options = {}) ⇒ Object

self end



120
121
122
123
124
125
126
127
# File 'lib/box/release/base.rb', line 120

def install(options = {})
  options = {
    :install_command => install_command
  }.merge(options)

  logger.info "Install #{name}"
  Box::Release.execute! "#{options[:install_command]} #{file} #{yaml_file}"
end

#newer?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/box/release/base.rb', line 68

def newer?(other)
  other.nil? or (self.name and self.name > other.name)
end

#normalize_url(url) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/box/release/base.rb', line 78

def normalize_url(url)
  if reference_url and (uri = URI(url)).relative?
    URI(reference_url).merge(uri).to_s
  else
    url
  end
end

#to_yamlObject



153
154
155
# File 'lib/box/release/base.rb', line 153

def to_yaml
  { "name" => name, "description_url" => description_url, "status_updated_at" => status_updated_at }.to_yaml
end

#valid_checksum?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/box/release/base.rb', line 141

def valid_checksum?
  checksum.nil? or checksum == file_checksum
end

#yaml_fileObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/box/release/base.rb', line 129

def yaml_file
  @yaml_file ||= Tempfile.new("release").tap do |yaml_file|
    begin
      yaml_file.write to_yaml
    ensure
      yaml_file.close
    end
  end

  @yaml_file.path
end