Class: SauceWhisk::Jobs

Inherits:
Object
  • Object
show all
Extended by:
RestRequestBuilder
Defined in:
lib/sauce_whisk/jobs.rb

Class Method Summary collapse

Methods included from RestRequestBuilder

auth_details, delete, fully_qualified_resource, get, make_request, post, put, request_from_rest_client

Class Method Details

.allObject



13
14
15
16
# File 'lib/sauce_whisk/jobs.rb', line 13

def self.all
  all_jobs = JSON.parse get
  all_jobs.map {|job| Job.new(job)}
end

.change_status(job_id, status) ⇒ Object



18
19
20
21
22
# File 'lib/sauce_whisk/jobs.rb', line 18

def self.change_status(job_id, status)
  put job_id, {"passed" => status}.to_json
rescue StandardError => e
  SauceWhisk.logger.error "Unable to change_status for #{job_id} to #{status}\nReason: #{e.to_s}"
end

.delete_job(job_id) ⇒ Object



67
68
69
# File 'lib/sauce_whisk/jobs.rb', line 67

def self.delete_job(job_id)
  delete "#{job_id}"
end

.fail_job(job_id) ⇒ Object



28
29
30
# File 'lib/sauce_whisk/jobs.rb', line 28

def self.fail_job(job_id)
  change_status(job_id, false)
end

.fetch(job_id) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sauce_whisk/jobs.rb', line 43

def self.fetch(job_id)
  job_hash = {}
  begin
    job_hash = JSON.parse(get job_id)
    assets = job_assets job_id
    job_hash.merge! assets
  rescue SauceWhisk::JobNotComplete
    # Always succeed
  end

  return Job.new(job_hash)
end

.fetch!(job_id) ⇒ Object



56
57
58
59
60
61
# File 'lib/sauce_whisk/jobs.rb', line 56

def self.fetch!(job_id)
  job_hash = JSON.parse(get job_id)
  assets = job_assets job_id
  job_hash.merge! assets
  Job.new job_hash
end

.fetch_asset(job_id, asset) ⇒ Object



71
72
73
# File 'lib/sauce_whisk/jobs.rb', line 71

def self.fetch_asset(job_id, asset)
  asset = get "#{job_id}/assets/#{asset}"
end

.job_assets(job_id) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sauce_whisk/jobs.rb', line 75

def self.job_assets(job_id)
  assets = JSON.parse get "#{job_id}/assets"
  screenshots = assets["screenshots"]

  {"screenshot_urls" => screenshots}
rescue RestClient::BadRequest => e
  if (/Job hasn't finished running/.match e.response)
    SauceWhisk.logger.debug("Exception fetching assets: #{e.message} - #{e.response}")
    raise SauceWhisk::JobNotComplete
  else
    SauceWhisk.logger.error("Exception fetching assets: #{e.message} - #{e.response}")
    raise e
  end
end

.pass_job(job_id) ⇒ Object



24
25
26
# File 'lib/sauce_whisk/jobs.rb', line 24

def self.pass_job(job_id)
  change_status(job_id, true)
end

.resourceObject



9
10
11
# File 'lib/sauce_whisk/jobs.rb', line 9

def self.resource
  "#{SauceWhisk.username}/jobs"
end

.save(job) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/sauce_whisk/jobs.rb', line 32

def self.save(job)
  fields_to_save = job.updated_fields.each_with_object(Hash.new) do |field, hsh|
    if field == :custom_data
      hsh[:'custom-data'] = job.send(field.to_s)
    else
      hsh[field] = job.send(field.to_s)
    end
  end
  put job.id, fields_to_save.to_json
end

.stop(job_id) ⇒ Object



63
64
65
# File 'lib/sauce_whisk/jobs.rb', line 63

def self.stop(job_id)
  put "#{job_id}/stop", {}
end