Class: Wesabe::Job

Inherits:
BaseModel show all
Defined in:
lib/wesabe/job.rb

Instance Attribute Summary collapse

Attributes inherited from BaseModel

#wesabe

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#get, #post

Methods included from Util

#all_or_one

Constructor Details

#initialize {|job| ... } ⇒ Job

Initializes a Wesabe::Job and yields itself.

Yield Parameters:



18
19
20
# File 'lib/wesabe/job.rb', line 18

def initialize
  yield self if block_given?
end

Instance Attribute Details

#created_atObject

When this job was created.



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

def created_at
  @created_at
end

#credentialObject

The credential that this job belongs to.



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

def credential
  @credential
end

#idObject

The globally unique identifier for this job.



3
4
5
# File 'lib/wesabe/job.rb', line 3

def id
  @id
end

#resultObject

The result of this job, which gives more specific information for “pending” and “failed” statuses.



8
9
10
# File 'lib/wesabe/job.rb', line 8

def result
  @result
end

#statusObject

The status of this job (pending|successful|failed).



5
6
7
# File 'lib/wesabe/job.rb', line 5

def status
  @status
end

Class Method Details

.from_xml(xml) ⇒ Wesabe::Job

Returns a Wesabe::Job generated from Wesabe’s API XML.

Parameters:

  • xml (Hpricot::Element)

    The <job> element from the API.

Returns:

  • (Wesabe::Job)

    The newly-created job populated by xml.



79
80
81
82
83
84
85
86
# File 'lib/wesabe/job.rb', line 79

def self.from_xml(xml)
  new do |job|
    job.id = xml.at('id').inner_text
    job.status = xml.at('status').inner_text
    job.result = xml.at('result').inner_text
    job.created_at = Time.parse(xml.at('created-at').inner_text)
  end
end

Instance Method Details

#complete?Boolean

Determines whether this job is finished.

Returns:

  • (Boolean)

    Whether the job is finished running.



52
53
54
# File 'lib/wesabe/job.rb', line 52

def complete?
  !pending?
end

#failed?Boolean

Determines whether this job failed.

Returns:

  • (Boolean)

    Whether this job has completed and, if so, whether it failed.



68
69
70
# File 'lib/wesabe/job.rb', line 68

def failed?
  status == 'failed'
end

#inspectObject



88
89
90
# File 'lib/wesabe/job.rb', line 88

def inspect
  inspect_these :id, :status, :result, :created_at
end

#pending?Boolean

Determines whether this job is still running.

Returns:

  • (Boolean)

    Whether the job is still running.



45
46
47
# File 'lib/wesabe/job.rb', line 45

def pending?
  status == 'pending'
end

#reloadWesabe::Job

Reloads this job from the server, useful when polling for updates.

job = credential.start_job
until job.complete?
  print '.'
  job.reload
  sleep 1
end
puts
puts "Job finished with status=#{job.status}, result=#{job.result}"

Returns:



34
35
36
37
38
39
40
# File 'lib/wesabe/job.rb', line 34

def reload
  replace(
    Wesabe::Job.from_xml(
      Hpricot.XML(
        get(:url => "/credentials/#{credential.id}/jobs/#{id}.xml"))))
  return self
end

#successful?Boolean

Determines whether this job is successful.

Returns:

  • (Boolean)

    Whether this job has completed and, if so, whether it was successful.



60
61
62
# File 'lib/wesabe/job.rb', line 60

def successful?
  status == 'successful'
end