Class: Simplificator::Webthumb::Job

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

Constant Summary collapse

STATUS_PROCESSING =

Constant for the status attribute when job is beeing processed

100
STATUS_PICKUP =

Constant for the status attribute when job is done

200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, job_id, url, submission_datetime, duration_estimate, cost, status = STATUS_PROCESSING) ⇒ Job

Constructor. *api_key: webthumb API key. Required by all the operations which query the server *job_id: id of the job. Required. *url: the url of the site to snapshot. Optional *submission_datetime: UTC Datetime of job submission *duration_estimate: integer value indicating estimated job duration in seconds *cost: integer value indicating how many credit this request costet. Optional *status: one of the STATUS_XXX constants defined in Base. Defaults to STATUS_PROCESSING



35
36
37
38
39
40
41
42
43
44
# File 'lib/rwebthumb/job.rb', line 35

def initialize(api_key, job_id, url, submission_datetime, duration_estimate, cost, status = STATUS_PROCESSING)
  super(api_key)
  @job_id = job_id
  @url = url
  @submission_datetime = submission_datetime
  @duration_estimate = duration_estimate
  @cost = cost
  @status = status
  @cache = {}
end

Instance Attribute Details

#costObject (readonly)

Returns the value of attribute cost.



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

def cost
  @cost
end

#duration_estimateObject (readonly)

Returns the value of attribute duration_estimate.



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

def duration_estimate
  @duration_estimate
end

#job_idObject (readonly)

Returns the value of attribute job_id.



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

def job_id
  @job_id
end

#submission_datetimeObject (readonly)

Returns the value of attribute submission_datetime.



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

def submission_datetime
  @submission_datetime
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.from_status_xml(api_key, xml) ⇒ Object

Factory method to create a Job object from a status XML. this does not set all attributes of the Job (url, duration_estimate, cost) since the API of webthumb does not return the same information on job creation and status requests.



21
22
23
24
25
26
# File 'lib/rwebthumb/job.rb', line 21

def self.from_status_xml(api_key, xml)
  status_element = REXML::XPath.first(xml, '/webthumb/jobStatus/status')
  submission_datetime = self.parse_webthumb_datetime(status_element.attributes['submissionTime'])
  job = Job.new(api_key, status_element.attributes['id'], nil, submission_datetime, 5, nil, 
    status_element.text == 'Complete' ? STATUS_PICKUP : STATUS_PROCESSING)
end

.from_thumbnail_xml(api_key, xml) ⇒ Object

Factory method to build a Job object from a REXML xml element



13
14
15
16
17
# File 'lib/rwebthumb/job.rb', line 13

def self.from_thumbnail_xml(api_key, xml)
  job_element = REXML::XPath.first(xml, '/webthumb/jobs/job')
  submission_datetime = self.parse_webthumb_datetime(job_element.attributes['time'])
  Job.new(api_key, job_element.text, job_element.attributes['url'], submission_datetime, job_element.attributes['estimate'].to_i, job_element.attributes['cost'].to_i)
end

Instance Method Details

#check_statusObject

Checks the status of the job on webthumb server. Returns one of the STATUS_XXX constants from Base. A call to this method updates the @status attribute.



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

def check_status
  response = do_request(build_status_xml())
  @status = REXML::XPath.first(response, '/webthumb/jobStatus/status').text == 'Complete' ? STATUS_PICKUP : STATUS_PROCESSING
  if pickup?
    @completion_time = response.attributes['completionTime']
  end
  @status
end

#fetch(size = :small) ⇒ Object

Fetch an image from the webthumb server. If the job has not yet finished then the server will return an error so check status first or use fetch_when_complete() Images are cached in the context of this Job so consequent calls are not requested from server again. Cache is experimental, dont know if it is a good idea.



70
71
72
73
74
75
76
# File 'lib/rwebthumb/job.rb', line 70

def fetch(size = :small)
  unless @cache.has_key?(size)
    response = do_request(build_fetch_xml(size))
    @cache[size] = response
  end
  @cache[size]
end

#fetch_when_complete(size = :small) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rwebthumb/job.rb', line 58

def fetch_when_complete(size = :small)
  while not pickup?
    sleep @duration_estimate
    check_status
  end
  fetch(size)
end

#pickup?Boolean

Is the status attribute set to STATUS_PICKUP ?

Returns:

  • (Boolean)


92
93
94
# File 'lib/rwebthumb/job.rb', line 92

def pickup?
  @status == STATUS_PICKUP
end

#processing?Boolean

Is the status attribute set to STATUS_PROCESSING ?

Returns:

  • (Boolean)


96
97
98
# File 'lib/rwebthumb/job.rb', line 96

def processing?
  @status == STATUS_PROCESSING
end

#write_file(data, name) ⇒ Object

Write the data to disk. *data: the bytes of the image as returned by fetch/fetch_when_complete *name: a filename Will return a File object

Raises:

  • (WebthumbException)


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

def write_file(data, name)
  raise WebthumbException.new('NO data given') if data == nil || data.size == 0
  File.open(name, 'wb+') do |file|
    file.write(data)
    file.close
    file
  end
end