Class: Dragonfly::Job

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/dragonfly/job.rb,
lib/dragonfly/job/step.rb,
lib/dragonfly/job/fetch.rb,
lib/dragonfly/job/process.rb,
lib/dragonfly/job/generate.rb,
lib/dragonfly/job/fetch_url.rb,
lib/dragonfly/job/fetch_file.rb

Defined Under Namespace

Classes: AppDoesNotMatch, CannotGenerateSha, Fetch, FetchFile, FetchUrl, Generate, IncorrectSHA, InvalidArray, JobAlreadyApplied, NoSHAGiven, NothingToProcess, Process, Step

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Job.



81
82
83
84
85
86
87
# File 'lib/dragonfly/job.rb', line 81

def initialize(app, content="", meta={})
  @app = app
  @next_step_index = 0
  @steps = []
  @content = Content.new(app, content, meta)
  @url_attributes = UrlAttributes.new
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#stepsObject

Returns the value of attribute steps.



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

def steps
  @steps
end

#url_attributesObject (readonly)

Returns the value of attribute url_attributes.



179
180
181
# File 'lib/dragonfly/job.rb', line 179

def url_attributes
  @url_attributes
end

Class Method Details

.deserialize(string, app) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dragonfly/job.rb', line 58

def deserialize(string, app)
  array = begin
    Serializer.json_b64_decode(string)
  rescue Serializer::BadString
    if app.allow_legacy_urls
      Serializer.marshal_b64_decode(string) # legacy strings
    else
      raise
    end
  end
  from_a(array, app)
end

.from_a(steps_array, app) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dragonfly/job.rb', line 45

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



71
72
73
# File 'lib/dragonfly/job.rb', line 71

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

.step_namesObject



75
76
77
# File 'lib/dragonfly/job.rb', line 75

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

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/dragonfly/job.rb', line 124

def applied?
  next_step_index == steps.length
end

#applied_stepsObject



128
129
130
# File 'lib/dragonfly/job.rb', line 128

def applied_steps
  steps[0...next_step_index]
end

#applyObject

Applying, etc.



118
119
120
121
122
# File 'lib/dragonfly/job.rb', line 118

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

#fetch_file_stepObject



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

def fetch_file_step
  last_step_of_type(FetchFile)
end

#fetch_stepObject



212
213
214
# File 'lib/dragonfly/job.rb', line 212

def fetch_step
  last_step_of_type(Fetch)
end

#fetch_url_stepObject



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

def fetch_url_step
  last_step_of_type(FetchUrl)
end

#generate_stepObject



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

def generate_step
  last_step_of_type(Generate)
end

#initialize_copy(other) ⇒ Object

Used by ‘dup’ and ‘clone’



90
91
92
93
94
95
96
# File 'lib/dragonfly/job.rb', line 90

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

#inspectObject

Misc



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

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

#pending_stepsObject



132
133
134
# File 'lib/dragonfly/job.rb', line 132

def pending_steps
  steps[next_step_index..-1]
end

#process_stepsObject



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

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

#serializeObject



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

def serialize
  Serializer.json_b64_encode(to_a)
end

#shaObject



154
155
156
157
158
159
160
# File 'lib/dragonfly/job.rb', line 154

def sha
  unless app.secret
    raise CannotGenerateSha, "A secret is required to sign and verify Dragonfly job requests. "\
                             "Use `secret '...'` or `verify_urls false` (not recommended!) in your config."
  end
  OpenSSL::HMAC.hexdigest('SHA256', app.secret, to_unique_s)[0,16]
end

#signatureObject



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

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

#step_typesObject



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

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

#to_aObject



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

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

#to_appObject

to_stuff…



189
190
191
# File 'lib/dragonfly/job.rb', line 189

def to_app
  JobEndpoint.new(self)
end

#to_fetched_job(uid) ⇒ Object



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

def to_fetched_job(uid)
  new_job = dup
  new_job.steps = []
  new_job.fetch!(uid)
  new_job.next_step_index = 1
  new_job
end

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



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

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

#to_unique_sObject

Serializing, etc.



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

def to_unique_s
  to_a.to_dragonfly_unique_s
end

#uidObject

Step inspection



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

def uid
  step = fetch_step
  step.uid if step
end

#update_url_attributes(hash) ⇒ Object



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

def update_url_attributes(hash)
  hash.each do |key, value|
    url_attributes.send("#{key}=", value)
  end
end

#url(opts = {}) ⇒ Object

URLs, etc.



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

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

#validate_sha!(sha) ⇒ Object



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

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