Class: Dragonfly::Job

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

Defined Under Namespace

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, temp_object = nil) ⇒ Job

Instance methods



157
158
159
160
161
162
163
164
# File 'lib/dragonfly/job.rb', line 157

def initialize(app, temp_object=nil)
  @app = app
  self.extend app.analyser.analysis_methods
  self.extend app.job_definitions
  @steps = []
  @next_step_index = 0
  @temp_object = temp_object
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app



174
175
176
# File 'lib/dragonfly/job.rb', line 174

def app
  @app
end

#stepsObject

Returns the value of attribute steps



174
175
176
# File 'lib/dragonfly/job.rb', line 174

def steps
  @steps
end

#temp_objectObject

Returns the value of attribute temp_object



173
174
175
# File 'lib/dragonfly/job.rb', line 173

def temp_object
  @temp_object
end

Class Method Details

.deserialize(string, app) ⇒ Object



141
142
143
# File 'lib/dragonfly/job.rb', line 141

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

.from_a(steps_array, app) ⇒ Object

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dragonfly/job.rb', line 121

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(*step_array)
  end
  job
end

.from_path(path, app) ⇒ Object



134
135
136
137
138
139
# File 'lib/dragonfly/job.rb', line 134

def from_path(path, app)
  path = path.dup
  path.sub!(app.url_path_prefix, '') if app.url_path_prefix
  path.sub!('/', '')
  deserialize(path, app)
end

.step_abbreviationsObject



145
146
147
# File 'lib/dragonfly/job.rb', line 145

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

.step_namesObject



149
150
151
# File 'lib/dragonfly/job.rb', line 149

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

Instance Method Details

#+(other_job) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/dragonfly/job.rb', line 204

def +(other_job)
  unless app == other_job.app
    raise AppDoesNotMatch, "Cannot add jobs belonging to different apps (#{app} is not #{other_job.app})"
  end
  unless other_job.applied_steps.empty?
    raise JobAlreadyApplied, "Cannot add jobs when the second one has already been applied (#{other_job})"
  end
  new_job = self.class.new(app, temp_object)
  new_job.steps = steps + other_job.steps
  new_job.next_step_index = next_step_index
  new_job
end

#analyse(method, *args) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/dragonfly/job.rb', line 192

def analyse(method, *args)
  unless result
    raise NothingToAnalyse, "Can't analyse because temp object has not been initialized. Need to fetch first?"
  end
  # Hacky - wish there was a nicer way to do this without extending with yet another module
  if method == :format
    _format || analyser.analyse(result, method, *args)
  else
    analyser.analyse(result, method, *args)
  end
end

#applied_stepsObject



230
231
232
# File 'lib/dragonfly/job.rb', line 230

def applied_steps
  steps[0...next_step_index]
end

#applyObject

Applying, etc.



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

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

#b64_dataObject



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

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

#encode_stepObject



331
332
333
# File 'lib/dragonfly/job.rb', line 331

def encode_step
  last_step_of_type(Encode)
end

#encoded_extnameObject



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

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

#encoded_formatObject



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

def encoded_format
  step = encode_step
  step.format if step
end

#fetch_file_stepObject



323
324
325
# File 'lib/dragonfly/job.rb', line 323

def fetch_file_step
  last_step_of_type(FetchFile)
end

#fetch_stepObject

Step inspection



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

def fetch_step
  last_step_of_type(Fetch)
end

#generate_stepObject



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

def generate_step
  last_step_of_type(Generate)
end

#initialize_copy(other) ⇒ Object

Used by ‘dup’ and ‘clone’



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

def initialize_copy(other)
  self.steps = other.steps.dup
  self.extend app.analyser.analysis_methods
  self.extend app.job_definitions
end

#inspectObject



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

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

#pending_stepsObject



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

def pending_steps
  steps[next_step_index..-1]
end

#process_stepsObject



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

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

#resolve_mime_typeObject



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

def resolve_mime_type
  app.resolve_mime_type(result)
end

#resultObject



225
226
227
228
# File 'lib/dragonfly/job.rb', line 225

def result
  apply
  temp_object
end

#serializeObject

Serializing, etc.



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

def serialize
  Serializer.marshal_encode(to_a)
end

#shaObject



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

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

#store(opts = {}) ⇒ Object

Misc



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

def store(opts={})
  app.store(result, opts)
end

#to_aObject



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

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

#to_appObject

to_stuff…



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

def to_app
  JobEndpoint.new(self)
end

#to_fetched_job(uid) ⇒ Object



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

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

#to_pathObject



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

def to_path
  "/#{serialize}"
end

#to_response(env = {}) ⇒ Object



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

def to_response(env={})
  to_app.call(env)
end

#uidObject



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

def uid
  step = fetch_step
  step.uid if step
end

#uid_basenameObject



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

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

#uid_extnameObject



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

def uid_extname
  File.extname(uid) if uid
end

#unique_signatureObject



250
251
252
# File 'lib/dragonfly/job.rb', line 250

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

#url(*args) ⇒ Object

URLs, etc.



271
272
273
# File 'lib/dragonfly/job.rb', line 271

def url(*args)
  app.url_for(self, *args) unless steps.empty?
end

#validate_sha!(sha) ⇒ Object



258
259
260
261
262
263
264
265
266
267
# File 'lib/dragonfly/job.rb', line 258

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