-
Notifications
You must be signed in to change notification settings - Fork 0
/
sauce_storage.rb
64 lines (53 loc) · 1.64 KB
/
sauce_storage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
=begin
See docs @ http://saucelabs.com/docs/rest#storage
s = SauceStorage.new username: 'my_user_name', key: '00', debug: true
# or if you have SAUCE_USERNAME and SAUCE_ACCESS_KEY in env already
s = SauceStorage.new debug: true
# list all files
s.files
# upload a file
s.upload '/tmp/sauce/test.zip'
> s.files
=> [{"size"=>8,
"mtime"=>1367700857.1011374,
"name"=>"test.zip",
"md5"=>"d8064dccc0b399d37c73ffa9661a79b6"}]
> s.upload '/tmp/sauce/test.zip'
Uploaded /tmp/sauce/test.zip
local_md5: 764efa883dda1e11db47671c4a3bbd9e
remote_md5: 764efa883dda1e11db47671c4a3bbd9e
=end
require 'rubygems'
require 'restclient'
require 'json'
class SauceStorage
attr_reader :username, :key, :url, :debug
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
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
def files
JSON.parse( RestClient.get @url )['files']
end
end