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

Constants inherited from Base

Base::VALID_OUTPUT_TYPES, Base::VALID_SIZES

Instance Attribute Summary collapse

Attributes inherited from Base

#api_key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#build_root_node, #do_request, parse_webthumb_datetime

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



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

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.



23
24
25
26
27
28
# File 'lib/rwebthumb/job.rb', line 23

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
18
19
# File 'lib/rwebthumb/job.rb', line 13

def self.from_thumbnail_xml(api_key, xml)
  job_element = REXML::XPath.first(xml, '/webthumb/jobs/job')
  return nil if job_element.nil?

  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.



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

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.



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

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



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

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)


94
95
96
# File 'lib/rwebthumb/job.rb', line 94

def pickup?
  @status == STATUS_PICKUP
end

#processing?Boolean

Is the status attribute set to STATUS_PROCESSING ?

Returns:

  • (Boolean)


98
99
100
# File 'lib/rwebthumb/job.rb', line 98

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:



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

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