Class: Sauce::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/sauce/job.rb

Overview

Interact with a Sauce Labs selenium jobs as if it were a ruby object

Defined Under Namespace

Classes: CannotDeleteJobError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Job

Creates an instance representing a job.



78
79
80
81
# File 'lib/sauce/job.rb', line 78

def initialize(options)
  build!(options)
  @client = Sauce::Client.new
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



10
11
12
# File 'lib/sauce/job.rb', line 10

def browser
  @browser
end

#browser_versionObject

Returns the value of attribute browser_version.



10
11
12
# File 'lib/sauce/job.rb', line 10

def browser_version
  @browser_version
end

#creation_timeObject

Returns the value of attribute creation_time.



11
12
13
# File 'lib/sauce/job.rb', line 11

def creation_time
  @creation_time
end

#custom_dataObject

Returns the value of attribute custom_data.



13
14
15
# File 'lib/sauce/job.rb', line 13

def custom_data
  @custom_data
end

#end_timeObject

Returns the value of attribute end_time.



11
12
13
# File 'lib/sauce/job.rb', line 11

def end_time
  @end_time
end

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/sauce/job.rb', line 9

def error
  @error
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/sauce/job.rb', line 9

def id
  @id
end

#log_urlObject

Returns the value of attribute log_url.



12
13
14
# File 'lib/sauce/job.rb', line 12

def log_url
  @log_url
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/sauce/job.rb', line 10

def name
  @name
end

#osObject

Returns the value of attribute os.



10
11
12
# File 'lib/sauce/job.rb', line 10

def os
  @os
end

#ownerObject

Returns the value of attribute owner.



9
10
11
# File 'lib/sauce/job.rb', line 9

def owner
  @owner
end

#passedObject

Returns the value of attribute passed.



13
14
15
# File 'lib/sauce/job.rb', line 13

def passed
  @passed
end

#publicObject

Returns the value of attribute public.



12
13
14
# File 'lib/sauce/job.rb', line 12

def public
  @public
end

#start_timeObject

Returns the value of attribute start_time.



11
12
13
# File 'lib/sauce/job.rb', line 11

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/sauce/job.rb', line 9

def status
  @status
end

#tagsObject

Returns the value of attribute tags.



12
13
14
# File 'lib/sauce/job.rb', line 12

def tags
  @tags
end

#video_urlObject

Returns the value of attribute video_url.



12
13
14
# File 'lib/sauce/job.rb', line 12

def video_url
  @video_url
end

Class Method Details

.accountObject

Get the class @@client. TODO: Consider metaprogramming this away



29
30
31
# File 'lib/sauce/job.rb', line 29

def self.
  @@account
end

.account=(account) ⇒ Object

Set the class @@client. TODO: Consider metaprogramming this away



35
36
37
# File 'lib/sauce/job.rb', line 35

def self.account=()
  @@account = 
end

.all(options = {}) ⇒ Object

Misnomer: Gets the most recent 100 jobs TODO: Allow/automate paging



49
50
51
52
53
54
55
# File 'lib/sauce/job.rb', line 49

def self.all(options={})
  url = "jobs"
  url += "?full=true" if options[:full] #unless options[:id_only]
  responses = @@client[url].get
  responses = JSON.parse responses.to_s
  return responses.collect{|response| Sauce::Job.new(response)}
end

.clientObject

Get the class @@client. TODO: Consider metaprogramming this away



17
18
19
# File 'lib/sauce/job.rb', line 17

def self.client
  @@client
end

.client=(client) ⇒ Object

Set the class @@client. TODO: Consider metaprogramming this away



23
24
25
# File 'lib/sauce/job.rb', line 23

def self.client=(client)
  @@client = client
end

.destroyObject



57
58
59
# File 'lib/sauce/job.rb', line 57

def self.destroy
  self.all.each { |tunnel| tunnel.destroy }
end

.find(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sauce/job.rb', line 61

def self.find(options={})
  if options.class == String
    id = options
  elsif options.class == Hash
    id = options[:id]
  end

  @@client ||= Sauce::Client.new

  #puts "GET-URL: #{@@client.url}jobs/#{id}"
  response = @@client["jobs/#{id}"].get

  # TODO: Return nil if bad response
  Sauce::Job.new JSON.parse(response.to_s)
end

.firstObject



39
40
41
# File 'lib/sauce/job.rb', line 39

def self.first
  self.all.first
end

.lastObject



43
44
45
# File 'lib/sauce/job.rb', line 43

def self.last
  self.all.last
end

Instance Method Details

#deleteObject



127
128
129
# File 'lib/sauce/job.rb', line 127

def delete
  raise CannonDeleteJobError("Cannot delete jobs via Sauce Labs'  REST API currently")
end

#refresh!Object

Retrieves the latest information on this job from the Sauce Labs’ server



84
85
86
87
88
89
# File 'lib/sauce/job.rb', line 84

def refresh!
  response = JSON.parse @@client["jobs/#{@id}"].get.body
  #puts "\tjob refresh with: #{response}"
  build! response
  self
end

#saveObject

Save/update the current information for the job



92
93
94
95
96
97
98
# File 'lib/sauce/job.rb', line 92

def save
  #puts "Saving job:\n -X PUT #{@@client['jobs']}/#{@id} -H 'Content-Type: application/json' -d '#{self.to_json}'"
  response = @client["jobs/#{@id}"].put(self.to_json,
                                         {:content_type => :json,
                                           :accept => :json}).body
  JSON.parse(response)
end

#to_json(options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sauce/job.rb', line 100

def to_json(options={})
  json = {
    :id =>              @id,
    :'custom-data' =>   @custom_data,
    :owner =>           @owner,
    :status =>          @status,
    :error =>           @error,
    :name =>            @name,
    :browser =>         @browser,
    :browser_version => @browser_version,
    :os =>              @os,
    :creation_time =>   @creation_time,
    :start_time =>      @start_time,
    :end_time =>        @end_time,
    :video_url =>       @video_url,
    :log_url =>         @log_url,
    :public =>          @public,
    :tags =>            @tags,
    :passed =>          @passed
  }

  options[:except].each { |key| json.delete(key) } if options[:except]
  json = json.select { |key,value| options[:only].include? key } if options[:only]

  return json.to_json
end