Class: Dragonfly::Job

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

Defined Under Namespace

Modules: OverrideInstanceMethods Classes: AppDoesNotMatch, Encode, Fetch, FetchFile, FetchUrl, Generate, IncorrectSHA, InvalidArray, JobAlreadyApplied, NoSHAGiven, NothingToAnalyse, NothingToEncode, NothingToProcess, Process, Step

Constant Summary collapse

STEPS =
[
  Fetch,
  Process,
  Encode,
  Generate,
  FetchFile,
  FetchUrl
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, content = nil, meta = {}) ⇒ Job

Returns a new instance of Job.



199
200
201
202
203
204
205
# File 'lib/dragonfly/job.rb', line 199

def initialize(app, content=nil, meta={})
  @app = app
  @steps = []
  @next_step_index = 0
  @meta = {}
  update(content, meta)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app



215
216
217
# File 'lib/dragonfly/job.rb', line 215

def app
  @app
end

#metaObject

Name and stuff



392
393
394
# File 'lib/dragonfly/job.rb', line 392

def meta
  @meta
end

#stepsObject

Returns the value of attribute steps



215
216
217
# File 'lib/dragonfly/job.rb', line 215

def steps
  @steps
end

#temp_objectObject

Returns the value of attribute temp_object



214
215
216
# File 'lib/dragonfly/job.rb', line 214

def temp_object
  @temp_object
end

Class Method Details

.deserialize(string, app) ⇒ Object



161
162
163
# File 'lib/dragonfly/job.rb', line 161

def deserialize(string, app)
  from_a(Serializer.marshal_decode(string), app)
end

.from_a(steps_array, app) ⇒ Object

Raises:



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dragonfly/job.rb', line 148

def from_a(steps_array, app)
  unless steps_array.is_a?(Array) &&
         steps_array.all?{|s| s.is_a?(Array) && step_abbreviations[s.first] }
    raise InvalidArray, "can't define a job from #{steps_array.inspect}"
  end
  job = app.new_job
  steps_array.each do |step_array|
    step_class = step_abbreviations[step_array.shift]
    job.steps << step_class.new(job, *step_array)
  end
  job
end

.step_abbreviationsObject



165
166
167
# File 'lib/dragonfly/job.rb', line 165

def step_abbreviations
  @step_abbreviations ||= STEPS.inject({}){|hash, step_class| hash[step_class.abbreviation] = step_class; hash }
end

.step_namesObject



169
170
171
# File 'lib/dragonfly/job.rb', line 169

def step_names
  @step_names ||= STEPS.map{|step_class| step_class.step_name }
end

Instance Method Details

#analyse(method, *args) ⇒ Object



233
234
235
236
237
238
# File 'lib/dragonfly/job.rb', line 233

def analyse(method, *args)
  unless result
    raise NothingToAnalyse, "Can't analyse because temp object has not been initialized. Need to fetch first?"
  end
  analyser.analyse(result, method, *args)
end

#applied?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/dragonfly/job.rb', line 248

def applied?
  next_step_index == steps.length
end

#applied_stepsObject



257
258
259
# File 'lib/dragonfly/job.rb', line 257

def applied_steps
  steps[0...next_step_index]
end

#applyObject

Applying, etc.



242
243
244
245
246
# File 'lib/dragonfly/job.rb', line 242

def apply
  pending_steps.each{|step| step.apply }
  self.next_step_index = steps.length
  self
end

#attributes_for_urlObject



415
416
417
418
419
420
421
422
# File 'lib/dragonfly/job.rb', line 415

def attributes_for_url
  attrs = meta.reject{|k, v| !server.params_in_url.include?(k.to_s) }
  attrs[:basename] ||= basename if server.params_in_url.include?('basename')
  attrs[:ext] ||= ext if server.params_in_url.include?('ext')
  attrs[:format] = (attrs[:format] || format_from_meta).to_s if server.params_in_url.include?('format')
  attrs.delete_if{|k, v| v.blank? }
  attrs
end

#b64_dataObject



306
307
308
# File 'lib/dragonfly/job.rb', line 306

def b64_data
  "data:#{mime_type};base64,#{Base64.encode64(data)}"
end

#basenameObject



407
408
409
# File 'lib/dragonfly/job.rb', line 407

def basename
  meta[:basename] || (File.basename(name, '.*') if name)
end

#encode_stepObject



366
367
368
# File 'lib/dragonfly/job.rb', line 366

def encode_step
  last_step_of_type(Encode)
end

#encoded_extnameObject



375
376
377
378
# File 'lib/dragonfly/job.rb', line 375

def encoded_extname
  format = encoded_format
  ".#{format}" if format
end

#encoded_formatObject



370
371
372
373
# File 'lib/dragonfly/job.rb', line 370

def encoded_format
  step = encode_step
  step.format if step
end

#extObject



411
412
413
# File 'lib/dragonfly/job.rb', line 411

def ext
  meta[:ext] || (File.extname(name)[/\.(.*)/, 1] if name)
end

#fetch_file_stepObject



354
355
356
# File 'lib/dragonfly/job.rb', line 354

def fetch_file_step
  last_step_of_type(FetchFile)
end

#fetch_stepObject

Step inspection



333
334
335
# File 'lib/dragonfly/job.rb', line 333

def fetch_step
  last_step_of_type(Fetch)
end

#fetch_url_stepObject



358
359
360
# File 'lib/dragonfly/job.rb', line 358

def fetch_url_step
  last_step_of_type(FetchUrl)
end

#generate_stepObject



350
351
352
# File 'lib/dragonfly/job.rb', line 350

def generate_step
  last_step_of_type(Generate)
end

#initialize_copy(other) ⇒ Object

Used by ‘dup’ and ‘clone’



208
209
210
211
212
# File 'lib/dragonfly/job.rb', line 208

def initialize_copy(other)
  self.steps = other.steps.map do |step|
    step.class.new(self, *step.args)
  end
end

#inspectObject



386
387
388
# File 'lib/dragonfly/job.rb', line 386

def inspect
  to_s.sub(/>$/, " app=#{app}, steps=#{steps.inspect}, temp_object=#{temp_object.inspect}, steps applied:#{applied_steps.length}/#{steps.length} >")
end

#nameObject



399
400
401
# File 'lib/dragonfly/job.rb', line 399

def name
  meta[:name]
end

#name=(name) ⇒ Object



403
404
405
# File 'lib/dragonfly/job.rb', line 403

def name=(name)
  meta[:name] = name
end

#pending_stepsObject



261
262
263
# File 'lib/dragonfly/job.rb', line 261

def pending_steps
  steps[next_step_index..-1]
end

#process_stepsObject



362
363
364
# File 'lib/dragonfly/job.rb', line 362

def process_steps
  steps.select{|s| s.is_a?(Process) }
end

#resultObject



252
253
254
255
# File 'lib/dragonfly/job.rb', line 252

def result
  apply
  temp_object
end

#serializeObject



277
278
279
# File 'lib/dragonfly/job.rb', line 277

def serialize
  Serializer.marshal_encode(to_a)
end

#shaObject



285
286
287
# File 'lib/dragonfly/job.rb', line 285

def sha
  Digest::SHA1.hexdigest("#{to_unique_s}#{app.secret}")[0...8]
end

#store(opts = {}) ⇒ Object

Misc



382
383
384
# File 'lib/dragonfly/job.rb', line 382

def store(opts={})
  app.store(result, opts.merge(:meta => meta))
end

#to_aObject



265
266
267
268
269
# File 'lib/dragonfly/job.rb', line 265

def to_a
  steps.map{|step|
    [step.class.abbreviation, *step.args]
  }
end

#to_appObject

to_stuff…



312
313
314
# File 'lib/dragonfly/job.rb', line 312

def to_app
  JobEndpoint.new(self)
end

#to_fetched_job(uid) ⇒ Object



324
325
326
327
328
329
# File 'lib/dragonfly/job.rb', line 324

def to_fetched_job(uid)
  new_job = self.class.new(app, temp_object, meta)
  new_job.fetch!(uid)
  new_job.next_step_index = 1
  new_job
end

#to_pathObject



320
321
322
# File 'lib/dragonfly/job.rb', line 320

def to_path
  "/#{serialize}"
end

#to_response(env = {"REQUEST_METHOD" => "GET"}) ⇒ Object



316
317
318
# File 'lib/dragonfly/job.rb', line 316

def to_response(env={"REQUEST_METHOD" => "GET"})
  to_app.call(env)
end

#to_unique_sObject

Serializing, etc.



273
274
275
# File 'lib/dragonfly/job.rb', line 273

def to_unique_s
  to_a.to_dragonfly_unique_s
end

#uidObject



337
338
339
340
# File 'lib/dragonfly/job.rb', line 337

def uid
  step = fetch_step
  step.uid if step
end

#uid_basenameObject



342
343
344
# File 'lib/dragonfly/job.rb', line 342

def uid_basename
  File.basename(uid, '.*') if uid
end

#uid_extnameObject



346
347
348
# File 'lib/dragonfly/job.rb', line 346

def uid_extname
  File.extname(uid) if uid
end

#unique_signatureObject



281
282
283
# File 'lib/dragonfly/job.rb', line 281

def unique_signature
  Digest::SHA1.hexdigest(to_unique_s)
end

#update(content, meta) ⇒ Object



424
425
426
427
428
429
430
431
432
433
# File 'lib/dragonfly/job.rb', line 424

def update(content, meta)
  if meta
    meta.merge!(meta.delete(:meta)) if meta[:meta] # legacy data etc. may have nested meta hash - deprecate gracefully here
    self.meta.merge!(meta)
  end
  if content
    self.temp_object = TempObject.new(content)
    self.name = temp_object.original_filename if name.nil? && temp_object.original_filename
  end
end

#url(opts = {}) ⇒ Object

URLs, etc.



302
303
304
# File 'lib/dragonfly/job.rb', line 302

def url(opts={})
  app.url_for(self, attributes_for_url.merge(opts)) unless steps.empty?
end

#validate_sha!(sha) ⇒ Object



289
290
291
292
293
294
295
296
297
298
# File 'lib/dragonfly/job.rb', line 289

def validate_sha!(sha)
  case sha
  when nil
    raise NoSHAGiven
  when self.sha
    self
  else
    raise IncorrectSHA, sha
  end
end