Class: SauceWhisk::Storage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Storage

Returns a new instance of Storage.



9
10
11
12
13
14
# File 'lib/sauce_whisk/storage.rb', line 9

def initialize opts={}
  @username = opts.fetch :username, ENV['SAUCE_USERNAME']
  @key      = opts.fetch :key, ENV['SAUCE_ACCESS_KEY']
  @url      = "https://#{@username}:#{@key}@saucelabs.com/rest/v1/storage/#{@username}"
  @debug    = opts.fetch :debug, false
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



7
8
9
# File 'lib/sauce_whisk/storage.rb', line 7

def debug
  @debug
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/sauce_whisk/storage.rb', line 7

def key
  @key
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/sauce_whisk/storage.rb', line 7

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/sauce_whisk/storage.rb', line 7

def username
  @username
end

Instance Method Details

#filesObject



38
39
40
# File 'lib/sauce_whisk/storage.rb', line 38

def files
  JSON.parse(RestClient.get @url)['files']
end

#upload(file_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sauce_whisk/storage.rb', line 16

def upload file_path
  file_name = File.basename file_path
  file      = File.new file_path
  local_md5 = Digest::MD5.hexdigest File.read file_path

  self.files.each do |file|
    if file['md5'] == local_md5
      puts 'File already uploaded' if @debug
      return true
    end
  end

  url        = "#{@url}/#{file_name}?overwrite=true"
  remote_md5 = JSON.parse(RestClient.post url, file, content_type: 'application/octet-stream')['md5']
  if @debug
    puts "Uploaded #{file_path}"
    puts " local_md5: #{local_md5}"
    puts "remote_md5: #{remote_md5}"
  end
  local_md5 == remote_md5
end