Class: Dragonfly::Job

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

Defined Under Namespace

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

Constant Summary

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Job) initialize(app, content = nil, meta = {}, url_attrs = {})

Returns a new instance of Job



228
229
230
231
232
233
234
235
# File 'lib/dragonfly/job.rb', line 228

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

Instance Attribute Details

- (Object) app (readonly)

Returns the value of attribute app



245
246
247
# File 'lib/dragonfly/job.rb', line 245

def app
  @app
end

- (Object) steps

Returns the value of attribute steps



245
246
247
# File 'lib/dragonfly/job.rb', line 245

def steps
  @steps
end

- (Object) temp_object

Returns the value of attribute temp_object



246
247
248
# File 'lib/dragonfly/job.rb', line 246

def temp_object
  @temp_object
end

- (Object) url_attrs

Returns the value of attribute url_attrs



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

def url_attrs
  @url_attrs
end

Class Method Details

+ (Object) deserialize(string, app)



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dragonfly/job.rb', line 181

def deserialize(string, app)
  array = begin
    Serializer.json_decode(string)
  rescue Serializer::BadString
    if app.allow_legacy_urls
      Serializer.marshal_decode(string, :check_malicious => true) # legacy strings
    else
      raise
    end
  end
  from_a(array, app)
end

+ (Object) from_a(steps_array, app)

Raises:



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/dragonfly/job.rb', line 168

def from_a(steps_array, app)
  unless steps_array.is_a?(Array) &&
         steps_array.all?{|s| s.is_a?(Array) && step_abbreviations[s.first.to_s] }
    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.to_s]
    job.steps << step_class.new(job, *step_array)
  end
  job
end

+ (Object) step_abbreviations



194
195
196
# File 'lib/dragonfly/job.rb', line 194

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

+ (Object) step_names



198
199
200
# File 'lib/dragonfly/job.rb', line 198

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

Instance Method Details

- (Object) analyse(method, *args)



269
270
271
# File 'lib/dragonfly/job.rb', line 269

def analyse(method, *args)
  analyser.analyse(result, method, *args)
end

- (Boolean) applied?

Returns:

  • (Boolean)


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

def applied?
  next_step_index == steps.length
end

- (Object) applied_steps



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

def applied_steps
  steps[0...next_step_index]
end

- (Object) apply

Applying, etc.



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

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

- (Object) b64_data



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

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

- (Object) close



413
414
415
416
# File 'lib/dragonfly/job.rb', line 413

def close
  previous_temp_objects.each{|temp_object| temp_object.close }
  temp_object.close if temp_object
end

- (Object) encode_step



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

def encode_step
  last_step_of_type(Encode)
end

- (Object) fetch_file_step



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

def fetch_file_step
  last_step_of_type(FetchFile)
end

- (Object) fetch_step



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

def fetch_step
  last_step_of_type(Fetch)
end

- (Object) fetch_url_step



378
379
380
# File 'lib/dragonfly/job.rb', line 378

def fetch_url_step
  last_step_of_type(FetchUrl)
end

- (Object) generate_step



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

def generate_step
  last_step_of_type(Generate)
end

- (Object) initialize_copy(other)

Used by 'dup' and 'clone'



238
239
240
241
242
243
# File 'lib/dragonfly/job.rb', line 238

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

- (Object) inspect



401
402
403
# File 'lib/dragonfly/job.rb', line 401

def inspect
  "<Dragonfly::Job app=#{app.name.inspect}, steps=#{steps.inspect}, temp_object=#{temp_object.inspect}, steps applied:#{applied_steps.length}/#{steps.length} >"
end

- (Object) pending_steps



289
290
291
# File 'lib/dragonfly/job.rb', line 289

def pending_steps
  steps[next_step_index..-1]
end

- (Object) process_steps



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

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

- (Object) serialize



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

def serialize
  Serializer.json_encode(to_a)
end

- (Object) sha



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

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

- (Object) step_types



390
391
392
# File 'lib/dragonfly/job.rb', line 390

def step_types
  steps.map{|s| s.class.step_name }
end

- (Object) store(opts = {})

Misc



396
397
398
399
# File 'lib/dragonfly/job.rb', line 396

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

- (Object) to_a



293
294
295
# File 'lib/dragonfly/job.rb', line 293

def to_a
  steps.map{|step| step.to_a }
end

- (Object) to_app

to_stuff...



344
345
346
# File 'lib/dragonfly/job.rb', line 344

def to_app
  JobEndpoint.new(self)
end

- (Object) to_fetched_job(uid)



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

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

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



348
349
350
# File 'lib/dragonfly/job.rb', line 348

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

- (Object) to_unique_s

Serializing, etc.



299
300
301
# File 'lib/dragonfly/job.rb', line 299

def to_unique_s
  to_a.to_dragonfly_unique_s
end

- (Object) uid

Step inspection



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

def uid
  step = fetch_step
  step.uid if step
end

- (Object) unique_signature



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

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

- (Object) update(content, new_meta)



405
406
407
408
409
410
411
# File 'lib/dragonfly/job.rb', line 405

def update(content, new_meta)
  if new_meta
    new_meta.merge!(new_meta.delete(:meta)) if new_meta[:meta] # legacy data etc. may have nested meta hash - deprecate gracefully here
  end
  old_meta = temp_object ? temp_object.meta : {}
  self.temp_object = TempObject.new(content, old_meta.merge(new_meta || {}))
end

- (Object) url(opts = {})

URLs, etc.



328
329
330
# File 'lib/dragonfly/job.rb', line 328

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

- (Object) validate_sha!(sha)



315
316
317
318
319
320
321
322
323
324
# File 'lib/dragonfly/job.rb', line 315

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