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

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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



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

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

- (Object) app (readonly)

Returns the value of attribute app



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

def app
  @app
end

- (Object) content (readonly)

Returns the value of attribute content



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

def content
  @content
end

- (Object) steps

Returns the value of attribute steps



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

def steps
  @steps
end

- (Object) url_attributes (readonly)

Returns the value of attribute url_attributes



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

def url_attributes
  @url_attributes
end

Class Method Details

+ (Object) deserialize(string, app)



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

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

+ (Object) from_a(steps_array, app)



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

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



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

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

+ (Object) step_names



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

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

Instance Method Details

- (Boolean) applied?



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

def applied?
  next_step_index == steps.length
end

- (Object) applied_steps



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

def applied_steps
  steps[0...next_step_index]
end

- (Object) apply

Applying, etc.



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

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

- (Object) fetch_file_step



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

def fetch_file_step
  last_step_of_type(FetchFile)
end

- (Object) fetch_step



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

def fetch_step
  last_step_of_type(Fetch)
end

- (Object) fetch_url_step



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

def fetch_url_step
  last_step_of_type(FetchUrl)
end

- (Object) generate_step



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

def generate_step
  last_step_of_type(Generate)
end

- (Object) initialize_copy(other)

Used by 'dup' and 'clone'



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

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

- (Object) inspect

Misc



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

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

- (Object) pending_steps



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

def pending_steps
  steps[next_step_index..-1]
end

- (Object) process_steps



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

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

- (Object) serialize



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

def serialize
  Serializer.json_b64_encode(to_a)
end

- (Object) sha



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

def sha
  unless app.secret
    raise CannotGenerateSha, "A secret is required to sign and verify Dragonfly job requests. "\
                             "Use `secret '...'` or disable `protect_from_dos_attacks` in your config."
  end
  Digest::SHA1.hexdigest("#{to_unique_s}#{app.secret}")[0...8]
end

- (Object) signature



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

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

- (Object) step_types



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

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

- (Object) to_a



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

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

- (Object) to_app

to_stuff…



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

def to_app
  JobEndpoint.new(self)
end

- (Object) to_fetched_job(uid)



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

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

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



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

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

- (Object) to_unique_s

Serializing, etc.



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

def to_unique_s
  to_a.to_dragonfly_unique_s
end

- (Object) uid

Step inspection



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

def uid
  step = fetch_step
  step.uid if step
end

- (Object) update_url_attributes(hash)



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

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

- (Object) url(opts = {})

URLs, etc.



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

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

- (Object) validate_sha!(sha)



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

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