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, NoContent, NoSHAGiven, 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 = {}, url_attrs = {}) ⇒ Job

Returns a new instance of Job.



217
218
219
220
221
222
223
224
# File 'lib/dragonfly/job.rb', line 217

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

#appObject (readonly)

Returns the value of attribute app



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

def app
  @app
end

#stepsObject

Returns the value of attribute steps



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

def steps
  @steps
end

#temp_objectObject

Returns the value of attribute temp_object



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

def temp_object
  @temp_object
end

#url_attrsObject

Returns the value of attribute url_attrs



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

def url_attrs
  @url_attrs
end

Class Method Details

.deserialize(string, app) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/dragonfly/job.rb', line 174

def deserialize(string, app)
  array = begin
    Serializer.json_decode(string)
  rescue Serializer::BadString
    Serializer.marshal_decode(string) # legacy strings
  end
  from_a(array, app)
end

.from_a(steps_array, app) ⇒ Object

Raises:



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dragonfly/job.rb', line 161

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

.step_abbreviationsObject



183
184
185
# File 'lib/dragonfly/job.rb', line 183

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

.step_namesObject



187
188
189
# File 'lib/dragonfly/job.rb', line 187

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

Instance Method Details

#analyse(method, *args) ⇒ Object



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

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

#applied?Boolean

Returns:

  • (Boolean)


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

def applied?
  next_step_index == steps.length
end

#applied_stepsObject



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

def applied_steps
  steps[0...next_step_index]
end

#applyObject

Applying, etc.



263
264
265
266
267
# File 'lib/dragonfly/job.rb', line 263

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

#b64_dataObject



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

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

#closeObject



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

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

#encode_stepObject



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

def encode_step
  last_step_of_type(Encode)
end

#fetch_file_stepObject



364
365
366
# File 'lib/dragonfly/job.rb', line 364

def fetch_file_step
  last_step_of_type(FetchFile)
end

#fetch_stepObject



356
357
358
# File 'lib/dragonfly/job.rb', line 356

def fetch_step
  last_step_of_type(Fetch)
end

#fetch_url_stepObject



368
369
370
# File 'lib/dragonfly/job.rb', line 368

def fetch_url_step
  last_step_of_type(FetchUrl)
end

#generate_stepObject



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

def generate_step
  last_step_of_type(Generate)
end

#initialize_copy(other) ⇒ Object

Used by 'dup' and 'clone'



227
228
229
230
231
# File 'lib/dragonfly/job.rb', line 227

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

#inspectObject



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

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

#pending_stepsObject



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

def pending_steps
  steps[next_step_index..-1]
end

#process_stepsObject



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

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

#serializeObject



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

def serialize
  Serializer.json_encode(to_a)
end

#shaObject



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

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

#step_typesObject



380
381
382
# File 'lib/dragonfly/job.rb', line 380

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

#store(opts = {}) ⇒ Object

Misc



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

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

#to_aObject



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

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

#to_appObject

to_stuff...



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

def to_app
  JobEndpoint.new(self)
end

#to_fetched_job(uid) ⇒ Object



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

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

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



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

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

#to_unique_sObject

Serializing, etc.



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

def to_unique_s
  to_a.to_dragonfly_unique_s
end

#uidObject

Step inspection



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

def uid
  step = fetch_step
  step.uid if step
end

#unique_signatureObject



297
298
299
# File 'lib/dragonfly/job.rb', line 297

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

#update(content, new_meta) ⇒ Object



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

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

#url(opts = {}) ⇒ Object

URLs, etc.



318
319
320
# File 'lib/dragonfly/job.rb', line 318

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

#validate_sha!(sha) ⇒ Object



305
306
307
308
309
310
311
312
313
314
# File 'lib/dragonfly/job.rb', line 305

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